-
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.
* add hasher with Rescue Blake2 Keccak256 Keccak512 Sha256 Sha512 * blake3 hash function * fill zero when call rescue hash * update rescue hash * calc rootHash * change hashes to encoded + nonce * calc digest
- Loading branch information
1 parent
c42ff4f
commit 3da867d
Showing
24 changed files
with
1,034 additions
and
264 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { initCrypto } from '../initCrypto'; | ||
import { blake3AsU8a } from './asU8a'; | ||
|
||
describe('blake3AsU8a', (): void => { | ||
beforeEach(async (): Promise<void> => { | ||
await initCrypto(); | ||
}); | ||
|
||
it('returns a 64-bit value by default', (): void => { | ||
expect( | ||
blake3AsU8a('abc', 64, undefined) | ||
).toEqual( | ||
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51]) | ||
); | ||
}); | ||
|
||
it('returns a 128-bit value (as specified,)', (): void => { | ||
expect( | ||
blake3AsU8a('abc', 128, undefined) | ||
).toEqual( | ||
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51, 255, 182, 59, 117, 39, 58, 141, 181]) | ||
); | ||
}); | ||
|
||
it('returns a 256-bit value (as specified)', (): void => { | ||
expect( | ||
blake3AsU8a('abc', 256, undefined) | ||
).toEqual( | ||
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51, 255, 182, 59, 117, 39, 58, 141, 181, 72, 197, 88, 70, 93, 121, 219, 3, 253, 53, 156, 108, 213, 189, 157, 133]) | ||
); | ||
}); | ||
|
||
it('returns a 512-bit value (as specified)', (): void => { | ||
expect( | ||
blake3AsU8a('abc', 512, undefined) | ||
).toEqual( | ||
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51, 255, 182, 59, 117, 39, 58, 141, 181, 72, 197, 88, 70, 93, 121, 219, 3, 253, 53, 156, 108, 213, 189, 157, 133, 31, 178, 80, 174, 115, 147, 245, 208, 40, 19, 182, 93, 82, 26, 13, 73, 45, 155, 160, 156, 247, 206, 127, 76, 255, 217, 0, 242, 51, 116, 191, 11]) | ||
); | ||
}); | ||
}); |
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,36 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { HexString } from '../types'; | ||
|
||
import { blake3 } from '@noble/hashes/blake3'; | ||
import { u8aToHex, u8aToU8a } from '@polkadot/util'; | ||
|
||
/** | ||
* @name blake3AsU8a | ||
* @summary Creates a blake3 u8a from the input. | ||
* @description | ||
* From a `Uint8Array` input, create the blake3 and return the result as a u8a. | ||
* @example | ||
* <BR> | ||
* | ||
* ```javascript | ||
* import { blake3AsU8a } from '@zcloak/crypto'; | ||
* | ||
* blake3AsU8a('abcd1234'); // => [135,118,41,144,40,252,65,100,204,245,252,44,138,223,209,13,119,200,131,115,120,31,210,44,253,198,228,212,122,61,87,245] | ||
* ``` | ||
*/ | ||
export function blake3AsU8a(data: HexString | Uint8Array | string, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null): Uint8Array { | ||
const byteLength = Math.ceil(bitLength / 8); | ||
|
||
const u8a = u8aToU8a(data); | ||
|
||
return blake3(u8a, { dkLen: byteLength, key: key || undefined }); | ||
} | ||
|
||
/** | ||
* @description Creates a blake3 hex from the input. | ||
*/ | ||
export function blake3AsHex(data: HexString | Uint8Array | string): HexString { | ||
return u8aToHex(blake3AsU8a(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,4 @@ | ||
// Copyright 2021-2022 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { blake3AsHex, blake3AsU8a } from './asU8a'; |
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
Oops, something went wrong.