-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* blake2 * fix * fix name * fix * extension for data * rename
- Loading branch information
Showing
8 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
|
||
extension Data { | ||
init?(fromHexString hexString: String) { | ||
guard !hexString.isEmpty else { | ||
return nil | ||
} | ||
|
||
var data = Data() | ||
var index = hexString.startIndex | ||
|
||
while index < hexString.endIndex { | ||
guard let nextIndex = hexString.index(index, offsetBy: 2, limitedBy: hexString.endIndex), | ||
let byte = UInt8(hexString[index ..< nextIndex], radix: 16) | ||
else { | ||
return nil | ||
} | ||
|
||
data.append(byte) | ||
index = nextIndex | ||
} | ||
|
||
self.init(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Blake2 | ||
import Foundation | ||
|
||
/// Computes a Blake2b 256-bit hash of the input data and returns the result as a `Data32`. | ||
/// | ||
/// - Parameter: The input data to hash. | ||
/// - Throws: An error if the hashing process fails. | ||
/// - Returns: A `Data32` containing the hash result. | ||
public func blake2b256(_ data: Data) throws -> Data32 { | ||
let hash = try Blake2b.hash(size: 32, data: data) | ||
let data32 = Data32(hash)! | ||
return data32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import XCTest | ||
|
||
@testable import Utils | ||
|
||
final class Blake2Tests: XCTestCase { | ||
func testBlake2b256Test() throws { | ||
let testData = Data("test".utf8) | ||
let expected = Data(fromHexString: "928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202") | ||
let actual = try blake2b256(testData) | ||
XCTAssertEqual(expected, actual.data) | ||
} | ||
} |