diff --git a/Sources/Crypto/Util/PrettyBytes.swift b/Sources/Crypto/Util/PrettyBytes.swift index 2733f9ed..0d0ccc28 100644 --- a/Sources/Crypto/Util/PrettyBytes.swift +++ b/Sources/Crypto/Util/PrettyBytes.swift @@ -42,12 +42,10 @@ extension DataProtocol { var hexChars = [UInt8](repeating: 0, count: hexLen) var offset = 0 - self.regions.forEach { (_) in - for i in self { - hexChars[Int(offset * 2)] = itoh((i >> 4) & 0xF) - hexChars[Int(offset * 2 + 1)] = itoh(i & 0xF) - offset += 1 - } + for i in self { + hexChars[Int(offset * 2)] = itoh((i >> 4) & 0xF) + hexChars[Int(offset * 2 + 1)] = itoh(i & 0xF) + offset += 1 } return String(bytes: hexChars, encoding: .utf8)! diff --git a/Tests/CryptoTests/Utils/PrettyBytes.swift b/Tests/CryptoTests/Utils/PrettyBytes.swift index 2733f9ed..0d0ccc28 100644 --- a/Tests/CryptoTests/Utils/PrettyBytes.swift +++ b/Tests/CryptoTests/Utils/PrettyBytes.swift @@ -42,12 +42,10 @@ extension DataProtocol { var hexChars = [UInt8](repeating: 0, count: hexLen) var offset = 0 - self.regions.forEach { (_) in - for i in self { - hexChars[Int(offset * 2)] = itoh((i >> 4) & 0xF) - hexChars[Int(offset * 2 + 1)] = itoh(i & 0xF) - offset += 1 - } + for i in self { + hexChars[Int(offset * 2)] = itoh((i >> 4) & 0xF) + hexChars[Int(offset * 2 + 1)] = itoh(i & 0xF) + offset += 1 } return String(bytes: hexChars, encoding: .utf8)! diff --git a/Tests/CryptoTests/Utils/PrettyBytesTests.swift b/Tests/CryptoTests/Utils/PrettyBytesTests.swift new file mode 100644 index 00000000..2f98d3a0 --- /dev/null +++ b/Tests/CryptoTests/Utils/PrettyBytesTests.swift @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftCrypto open source project +// +// Copyright (c) 2019-2020 Apple Inc. and the SwiftCrypto project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.md for the list of SwiftCrypto project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +import Foundation +import XCTest + +// Not sure if NSString-bridged "String.init(format:_:)" is available on Linux/Windows. +#if (os(macOS) || os(iOS) || os(tvOS) || os(watchOS)) && CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +@testable import Crypto + +class PrettyBytesTests: XCTestCase { + func testHexString() { + let random = Data((1...64).map { _ in UInt8.random(in: UInt8.min...UInt8.max)}) + + let hexString = random.hexString + let target = random.map { String(format: "%02x", $0) }.joined() + + XCTAssertEqual(hexString, target) + } +} + +#endif diff --git a/Tests/_CryptoExtrasTests/Utils/BytesUtil.swift b/Tests/_CryptoExtrasTests/Utils/BytesUtil.swift index 1d6f7daf..17c5c14a 100644 --- a/Tests/_CryptoExtrasTests/Utils/BytesUtil.swift +++ b/Tests/_CryptoExtrasTests/Utils/BytesUtil.swift @@ -63,12 +63,10 @@ extension DataProtocol { let ptr = UnsafeMutablePointer.allocate(capacity: hexLen) var offset = 0 - self.regions.forEach { (_) in - for i in self { - ptr[Int(offset * 2)] = itoh((i >> 4) & 0xF) - ptr[Int(offset * 2 + 1)] = itoh(i & 0xF) - offset += 1 - } + for i in self { + ptr[Int(offset * 2)] = itoh((i >> 4) & 0xF) + ptr[Int(offset * 2 + 1)] = itoh(i & 0xF) + offset += 1 } return String(bytesNoCopy: ptr, length: hexLen, encoding: .utf8, freeWhenDone: true)!