Skip to content

Commit

Permalink
fix: use blakejs instead of blake2b
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Nov 14, 2024
1 parent 226e7c4 commit cdb05ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
40 changes: 5 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"@koush/wrtc": "^0.5.3",
"@radixdlt/semantic-release-config": "^1.1.0",
"@rollup/plugin-typescript": "^11.1.5",
"@types/blake2b": "^2.1.0",
"@types/elliptic": "^6.4.14",
"@types/jest": "^29.5.4",
"@types/lodash.chunk": "^4.2.7",
Expand All @@ -88,7 +87,7 @@
},
"dependencies": {
"@radixdlt/radix-connect-schemas": "^0.1.0",
"blake2b": "^2.1.4",
"blakejs": "^1.2.1",
"buffer": "^6.0.3",
"get-random-values": "^3.0.0",
"isomorphic-webcrypto": "^2.3.8",
Expand Down
15 changes: 9 additions & 6 deletions src/crypto/blake2b.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { Buffer } from 'buffer'
import blake2bHash from 'blake2b'
import blake from 'blakejs'
import type { ResultAsync } from 'neverthrow'
import { errAsync, okAsync } from 'neverthrow'

export const blake2b = (input: Buffer): ResultAsync<Buffer, Error> => {
const output = new Uint8Array(32)
try {
return okAsync(
blake2bHash(output.length).update(new Uint8Array(input)).digest('hex'),
).map((hex) => Buffer.from(hex, 'hex'))
blake.blake2b(new Uint8Array(input), undefined, output.length),
).map((hex) => Buffer.from(hex))
} catch (error) {
return errAsync(error as Error)
}
}

export const blakeHashHexSync = (data: string) =>
blake2bHash(32)
.update(new Uint8Array(Buffer.from(data, 'hex')))
.digest('hex')
blakeHashBufferToHex(Buffer.from(data, 'hex'))

export const blakeHashBufferToHex = (buffer: Buffer) =>
Buffer.from(blake.blake2b(new Uint8Array(buffer), undefined, 32)).toString(
'hex',
)

0 comments on commit cdb05ef

Please sign in to comment.