Skip to content

Commit

Permalink
fix: remove UnsafeRawBufferPointer and change hasher to var
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaBrest committed Nov 19, 2024
1 parent f550657 commit b501350
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Sources/blake3-swift/blake3_swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Foundation
import blake3_c

public final class Blake3 {
private let hasher: blake3_hasher
private static let BLAKE3_OUT_LENGTH: Int = Int(BLAKE3_OUT_LEN)

private var hasher: blake3_hasher

public init() {
hasher = blake3_hasher()
Expand All @@ -14,21 +16,19 @@ public final class Blake3 {
}

public func update(buffer: [UInt8]) {
update(bytes: buffer)
buffer.withUnsafeBufferPointer { ptr in
update(bytes: UnsafeRawBufferPointer(ptr))
}
}

public func update(data: Data) {
update(bytes: data.withUnsafeBytes { $0 })
}

public func finalizeBytes() -> UnsafeRawBufferPointer {
var out = [UInt8](repeating: 0, count: BLAKE3_OUT_LEN)
blake3_hasher_finalize(&hasher, &out, BLAKE3_OUT_LEN)
return UnsafeRawBufferPointer(start: out, count: BLAKE3_OUT_LEN)
}

public func finalizeBuffer() -> [UInt8] {
return finalizeBytes().bindMemory(to: UInt8.self).map { $0 }
public func finalizeBytes() -> [UInt8] {
var out = [UInt8](repeating: 0, count: Blake3.BLAKE3_OUT_LENGTH)
blake3_hasher_finalize(&hasher, &out, Blake3.BLAKE3_OUT_LENGTH)
return out
}

public func finalizeData() -> Data {
Expand Down

0 comments on commit b501350

Please sign in to comment.