Skip to content

Commit

Permalink
add blake2 function (#22)
Browse files Browse the repository at this point in the history
* blake2

* fix

* fix name

* fix

* extension for data

* rename
  • Loading branch information
qiweiii authored Jun 26, 2024
1 parent a1718fe commit eae4a71
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Blockchain/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"revision" : "679ee7d35e493e181be844dadbe78636cefaace4"
}
},
{
"identity" : "blake2.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tesseract-one/Blake2.swift.git",
"state" : {
"revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e",
"version" : "0.2.0"
}
},
{
"identity" : "scalecodec.swift",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 9 additions & 0 deletions Boka/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"revision" : "679ee7d35e493e181be844dadbe78636cefaace4"
}
},
{
"identity" : "blake2.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tesseract-one/Blake2.swift.git",
"state" : {
"revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e",
"version" : "0.2.0"
}
},
{
"identity" : "scalecodec.swift",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 9 additions & 0 deletions Node/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"revision" : "679ee7d35e493e181be844dadbe78636cefaace4"
}
},
{
"identity" : "blake2.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tesseract-one/Blake2.swift.git",
"state" : {
"revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e",
"version" : "0.2.0"
}
},
{
"identity" : "scalecodec.swift",
"kind" : "remoteSourceControl",
Expand Down
11 changes: 10 additions & 1 deletion Utils/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"originHash" : "20e3a243ff55bca06efe38725567e64612fcf69c4836f7018fbfb322bcf6d466",
"originHash" : "7f9d01d4c8c5ccb64f9b9f466f7f8e61b0fd8cd435aa5c4e587e5a6e459f5d77",
"pins" : [
{
"identity" : "blake2.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tesseract-one/Blake2.swift.git",
"state" : {
"revision" : "29c55c8fe42d6661e5a32cc5bbbad1fff64fd01e",
"version" : "0.2.0"
}
},
{
"identity" : "scalecodec.swift",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 2 additions & 0 deletions Utils/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/tesseract-one/ScaleCodec.swift.git", from: "0.3.0"),
.package(url: "https://github.com/tesseract-one/Blake2.swift.git", from: "0.2.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "4.0.0"),
],
targets: [
Expand All @@ -26,6 +27,7 @@ let package = Package(
name: "Utils",
dependencies: [
.product(name: "ScaleCodec", package: "ScaleCodec.swift"),
.product(name: "Blake2", package: "Blake2.swift"),
.product(name: "Crypto", package: "swift-crypto"),
]
),
Expand Down
25 changes: 25 additions & 0 deletions Utils/Sources/Utils/Extensions.swift
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)
}
}
13 changes: 13 additions & 0 deletions Utils/Sources/Utils/hashing.swift
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
}
12 changes: 12 additions & 0 deletions Utils/Tests/UtilsTests/Blake2Tests.swift
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)
}
}

0 comments on commit eae4a71

Please sign in to comment.