-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
226e7c4
commit cdb05ef
Showing
3 changed files
with
15 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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', | ||
) |