@twin.org/crypto 0.0.2-next.9 → 0.0.3-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/address/bech32.js +72 -0
- package/dist/es/address/bech32.js.map +1 -0
- package/dist/es/address/bip44.js +113 -0
- package/dist/es/address/bip44.js.map +1 -0
- package/dist/es/ciphers/chaCha20Poly1305.js +48 -0
- package/dist/es/ciphers/chaCha20Poly1305.js.map +1 -0
- package/dist/es/curves/ed25519.js +114 -0
- package/dist/es/curves/ed25519.js.map +1 -0
- package/dist/es/curves/secp256k1.js +82 -0
- package/dist/es/curves/secp256k1.js.map +1 -0
- package/dist/es/curves/x25519.js +34 -0
- package/dist/es/curves/x25519.js.map +1 -0
- package/dist/es/curves/zip215.js +33 -0
- package/dist/es/curves/zip215.js.map +1 -0
- package/dist/es/hashes/blake2b.js +90 -0
- package/dist/es/hashes/blake2b.js.map +1 -0
- package/dist/es/hashes/blake3.js +76 -0
- package/dist/es/hashes/blake3.js.map +1 -0
- package/dist/es/hashes/hmacSha1.js +56 -0
- package/dist/es/hashes/hmacSha1.js.map +1 -0
- package/dist/es/hashes/hmacSha256.js +83 -0
- package/dist/es/hashes/hmacSha256.js.map +1 -0
- package/dist/es/hashes/hmacSha512.js +132 -0
- package/dist/es/hashes/hmacSha512.js.map +1 -0
- package/dist/es/hashes/pbkdf2.js +45 -0
- package/dist/es/hashes/pbkdf2.js.map +1 -0
- package/dist/es/hashes/sha1.js +52 -0
- package/dist/es/hashes/sha1.js.map +1 -0
- package/dist/es/hashes/sha256.js +75 -0
- package/dist/es/hashes/sha256.js.map +1 -0
- package/dist/es/hashes/sha3.js +122 -0
- package/dist/es/hashes/sha3.js.map +1 -0
- package/dist/es/hashes/sha512.js +118 -0
- package/dist/es/hashes/sha512.js.map +1 -0
- package/dist/es/helpers/pemHelper.js +42 -0
- package/dist/es/helpers/pemHelper.js.map +1 -0
- package/dist/es/index.js +29 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/keys/bip32Path.js +72 -0
- package/dist/es/keys/bip32Path.js.map +1 -0
- package/dist/es/keys/bip39.js +83 -0
- package/dist/es/keys/bip39.js.map +1 -0
- package/dist/es/keys/slip0010.js +83 -0
- package/dist/es/keys/slip0010.js.map +1 -0
- package/dist/es/models/keyType.js +17 -0
- package/dist/es/models/keyType.js.map +1 -0
- package/dist/es/otp/hotp.js +27 -0
- package/dist/es/otp/hotp.js.map +1 -0
- package/dist/es/otp/totp.js +75 -0
- package/dist/es/otp/totp.js.map +1 -0
- package/dist/es/passwords/passwordGenerator.js +28 -0
- package/dist/es/passwords/passwordGenerator.js.map +1 -0
- package/dist/es/passwords/passwordValidator.js +81 -0
- package/dist/es/passwords/passwordValidator.js.map +1 -0
- package/dist/types/address/bech32.d.ts +4 -0
- package/dist/types/address/bip44.d.ts +6 -2
- package/dist/types/ciphers/chaCha20Poly1305.d.ts +4 -0
- package/dist/types/curves/ed25519.d.ts +4 -0
- package/dist/types/curves/secp256k1.d.ts +4 -0
- package/dist/types/curves/x25519.d.ts +4 -0
- package/dist/types/curves/zip215.d.ts +4 -0
- package/dist/types/hashes/blake2b.d.ts +4 -0
- package/dist/types/hashes/blake3.d.ts +4 -0
- package/dist/types/hashes/hmacSha1.d.ts +4 -0
- package/dist/types/hashes/hmacSha256.d.ts +4 -0
- package/dist/types/hashes/hmacSha512.d.ts +4 -0
- package/dist/types/hashes/pbkdf2.d.ts +4 -0
- package/dist/types/hashes/sha1.d.ts +4 -0
- package/dist/types/hashes/sha256.d.ts +4 -0
- package/dist/types/hashes/sha3.d.ts +4 -0
- package/dist/types/hashes/sha512.d.ts +4 -0
- package/dist/types/helpers/pemHelper.d.ts +4 -0
- package/dist/types/index.d.ts +26 -26
- package/dist/types/keys/bip39.d.ts +4 -0
- package/dist/types/keys/slip0010.d.ts +6 -2
- package/dist/types/otp/hotp.d.ts +4 -0
- package/docs/changelog.md +286 -0
- package/docs/reference/classes/Bech32.md +8 -0
- package/docs/reference/classes/Bip39.md +8 -0
- package/docs/reference/classes/Bip44.md +8 -0
- package/docs/reference/classes/Blake2b.md +8 -0
- package/docs/reference/classes/Blake3.md +8 -0
- package/docs/reference/classes/ChaCha20Poly1305.md +8 -0
- package/docs/reference/classes/Ed25519.md +8 -0
- package/docs/reference/classes/HmacSha1.md +8 -0
- package/docs/reference/classes/HmacSha256.md +8 -0
- package/docs/reference/classes/HmacSha512.md +8 -0
- package/docs/reference/classes/Hotp.md +8 -0
- package/docs/reference/classes/Pbkdf2.md +8 -0
- package/docs/reference/classes/PemHelper.md +8 -0
- package/docs/reference/classes/Secp256k1.md +8 -0
- package/docs/reference/classes/Sha1.md +8 -0
- package/docs/reference/classes/Sha256.md +8 -0
- package/docs/reference/classes/Sha3.md +8 -0
- package/docs/reference/classes/Sha512.md +8 -0
- package/docs/reference/classes/Slip0010.md +8 -0
- package/docs/reference/classes/Totp.md +2 -2
- package/docs/reference/classes/X25519.md +8 -0
- package/docs/reference/classes/Zip215.md +8 -0
- package/locales/en.json +9 -28
- package/package.json +29 -16
- package/dist/cjs/index.cjs +0 -1876
- package/dist/esm/index.mjs +0 -1829
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { blake2b } from "@noble/hashes/blake2.js";
|
|
4
|
+
import { Guards } from "@twin.org/core";
|
|
5
|
+
/**
|
|
6
|
+
* Class to help with Blake2B Signature scheme.
|
|
7
|
+
*/
|
|
8
|
+
export class Blake2b {
|
|
9
|
+
/**
|
|
10
|
+
* Blake2b 160.
|
|
11
|
+
*/
|
|
12
|
+
static SIZE_160 = 20;
|
|
13
|
+
/**
|
|
14
|
+
* Blake2b 256.
|
|
15
|
+
*/
|
|
16
|
+
static SIZE_256 = 32;
|
|
17
|
+
/**
|
|
18
|
+
* Blake2b 512.
|
|
19
|
+
*/
|
|
20
|
+
static SIZE_512 = 64;
|
|
21
|
+
/**
|
|
22
|
+
* Runtime name for the class.
|
|
23
|
+
*/
|
|
24
|
+
static CLASS_NAME = "Blake2b";
|
|
25
|
+
/**
|
|
26
|
+
* The instance of the hash.
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
_instance;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new instance of Blake2b.
|
|
33
|
+
* @param outputLength The output length.
|
|
34
|
+
* @param key Optional key for the hash.
|
|
35
|
+
*/
|
|
36
|
+
constructor(outputLength, key) {
|
|
37
|
+
this._instance = blake2b.create({
|
|
38
|
+
dkLen: outputLength,
|
|
39
|
+
key
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Perform Sum 160 on the block.
|
|
44
|
+
* @param block The block to operate on.
|
|
45
|
+
* @param key Optional key for the hash.
|
|
46
|
+
* @returns The sum 160 of the block.
|
|
47
|
+
*/
|
|
48
|
+
static sum160(block, key) {
|
|
49
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
50
|
+
return new Blake2b(Blake2b.SIZE_160, key).update(block).digest();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Perform Sum 256 on the block.
|
|
54
|
+
* @param block The block to operate on.
|
|
55
|
+
* @param key Optional key for the hash.
|
|
56
|
+
* @returns The sum 256 of the block.
|
|
57
|
+
*/
|
|
58
|
+
static sum256(block, key) {
|
|
59
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
60
|
+
return new Blake2b(Blake2b.SIZE_256, key).update(block).digest();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Perform Sum 512 on the block.
|
|
64
|
+
* @param block The block to operate on.
|
|
65
|
+
* @param key Optional key for the hash.
|
|
66
|
+
* @returns The sum 512 of the block.
|
|
67
|
+
*/
|
|
68
|
+
static sum512(block, key) {
|
|
69
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
70
|
+
return new Blake2b(Blake2b.SIZE_512, key).update(block).digest();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Update the hash with the block.
|
|
74
|
+
* @param block The block to update the hash with.
|
|
75
|
+
* @returns The instance for chaining.
|
|
76
|
+
*/
|
|
77
|
+
update(block) {
|
|
78
|
+
Guards.uint8Array(Blake2b.CLASS_NAME, "block", block);
|
|
79
|
+
this._instance.update(block);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get the digest for the hash.
|
|
84
|
+
* @returns The instance for chaining.
|
|
85
|
+
*/
|
|
86
|
+
digest() {
|
|
87
|
+
return this._instance.digest();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=blake2b.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blake2b.js","sourceRoot":"","sources":["../../../src/hashes/blake2b.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,OAAO;IACnB;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,EAAE,CAAC;IAEpC;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,EAAE,CAAC;IAEpC;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,EAAE,CAAC;IAEpC;;OAEG;IACI,MAAM,CAAU,UAAU,aAA6B;IAE9D;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;;;OAIG;IACH,YAAY,YAAoB,EAAE,GAAgB;QACjD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;YAC/B,KAAK,EAAE,YAAY;YACnB,GAAG;SACH,CAAC,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB,EAAE,GAAgB;QACvD,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC5D,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IAClE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB,EAAE,GAAgB;QACvD,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC5D,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IAClE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB,EAAE,GAAgB;QACvD,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC5D,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IAClE,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC5D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { blake2b } from \"@noble/hashes/blake2.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Class to help with Blake2B Signature scheme.\n */\nexport class Blake2b {\n\t/**\n\t * Blake2b 160.\n\t */\n\tpublic static SIZE_160: number = 20;\n\n\t/**\n\t * Blake2b 256.\n\t */\n\tpublic static SIZE_256: number = 32;\n\n\t/**\n\t * Blake2b 512.\n\t */\n\tpublic static SIZE_512: number = 64;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Blake2b>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of Blake2b.\n\t * @param outputLength The output length.\n\t * @param key Optional key for the hash.\n\t */\n\tconstructor(outputLength: number, key?: Uint8Array) {\n\t\tthis._instance = blake2b.create({\n\t\t\tdkLen: outputLength,\n\t\t\tkey\n\t\t});\n\t}\n\n\t/**\n\t * Perform Sum 160 on the block.\n\t * @param block The block to operate on.\n\t * @param key Optional key for the hash.\n\t * @returns The sum 160 of the block.\n\t */\n\tpublic static sum160(block: Uint8Array, key?: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(Blake2b.CLASS_NAME, nameof(block), block);\n\t\treturn new Blake2b(Blake2b.SIZE_160, key).update(block).digest();\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param block The block to operate on.\n\t * @param key Optional key for the hash.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(block: Uint8Array, key?: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(Blake2b.CLASS_NAME, nameof(block), block);\n\t\treturn new Blake2b(Blake2b.SIZE_256, key).update(block).digest();\n\t}\n\n\t/**\n\t * Perform Sum 512 on the block.\n\t * @param block The block to operate on.\n\t * @param key Optional key for the hash.\n\t * @returns The sum 512 of the block.\n\t */\n\tpublic static sum512(block: Uint8Array, key?: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(Blake2b.CLASS_NAME, nameof(block), block);\n\t\treturn new Blake2b(Blake2b.SIZE_512, key).update(block).digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): Blake2b {\n\t\tGuards.uint8Array(Blake2b.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { blake3 } from "@noble/hashes/blake3.js";
|
|
4
|
+
import { Guards } from "@twin.org/core";
|
|
5
|
+
/**
|
|
6
|
+
* Class to help with Blake3 Signature scheme.
|
|
7
|
+
*/
|
|
8
|
+
export class Blake3 {
|
|
9
|
+
/**
|
|
10
|
+
* Blake3 256.
|
|
11
|
+
*/
|
|
12
|
+
static SIZE_256 = 32;
|
|
13
|
+
/**
|
|
14
|
+
* Blake3 512.
|
|
15
|
+
*/
|
|
16
|
+
static SIZE_512 = 64;
|
|
17
|
+
/**
|
|
18
|
+
* Runtime name for the class.
|
|
19
|
+
*/
|
|
20
|
+
static CLASS_NAME = "Blake3";
|
|
21
|
+
/**
|
|
22
|
+
* The instance of the hash.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
_instance;
|
|
27
|
+
/**
|
|
28
|
+
* Create a new instance of Blake3.
|
|
29
|
+
* @param outputLength The output length.
|
|
30
|
+
* @param key Optional key for the hash.
|
|
31
|
+
*/
|
|
32
|
+
constructor(outputLength, key) {
|
|
33
|
+
this._instance = blake3.create({
|
|
34
|
+
dkLen: outputLength,
|
|
35
|
+
key
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Perform Sum 256 on the block.
|
|
40
|
+
* @param block The block to operate on.
|
|
41
|
+
* @param key Optional key for the hash.
|
|
42
|
+
* @returns The sum 256 of the block.
|
|
43
|
+
*/
|
|
44
|
+
static sum256(block, key) {
|
|
45
|
+
Guards.uint8Array(Blake3.CLASS_NAME, "block", block);
|
|
46
|
+
return new Blake3(Blake3.SIZE_256, key).update(block).digest();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Perform Sum 512 on the block.
|
|
50
|
+
* @param block The block to operate on.
|
|
51
|
+
* @param key Optional key for the hash.
|
|
52
|
+
* @returns The sum 512 of the block.
|
|
53
|
+
*/
|
|
54
|
+
static sum512(block, key) {
|
|
55
|
+
Guards.uint8Array(Blake3.CLASS_NAME, "block", block);
|
|
56
|
+
return new Blake3(Blake3.SIZE_512, key).update(block).digest();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Update the hash with the block.
|
|
60
|
+
* @param block The block to update the hash with.
|
|
61
|
+
* @returns The instance for chaining.
|
|
62
|
+
*/
|
|
63
|
+
update(block) {
|
|
64
|
+
Guards.uint8Array(Blake3.CLASS_NAME, "block", block);
|
|
65
|
+
this._instance.update(block);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the digest for the hash.
|
|
70
|
+
* @returns The instance for chaining.
|
|
71
|
+
*/
|
|
72
|
+
digest() {
|
|
73
|
+
return this._instance.digest();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=blake3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blake3.js","sourceRoot":"","sources":["../../../src/hashes/blake3.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,EAAE,CAAC;IAEpC;;OAEG;IACI,MAAM,CAAC,QAAQ,GAAW,EAAE,CAAC;IAEpC;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;;;OAIG;IACH,YAAY,YAAoB,EAAE,GAAgB;QACjD,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC;YAC9B,KAAK,EAAE,YAAY;YACnB,GAAG;SACH,CAAC,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB,EAAE,GAAgB;QACvD,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC3D,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IAChE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,KAAiB,EAAE,GAAgB;QACvD,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC3D,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IAChE,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC3D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { blake3 } from \"@noble/hashes/blake3.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Class to help with Blake3 Signature scheme.\n */\nexport class Blake3 {\n\t/**\n\t * Blake3 256.\n\t */\n\tpublic static SIZE_256: number = 32;\n\n\t/**\n\t * Blake3 512.\n\t */\n\tpublic static SIZE_512: number = 64;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Blake3>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of Blake3.\n\t * @param outputLength The output length.\n\t * @param key Optional key for the hash.\n\t */\n\tconstructor(outputLength: number, key?: Uint8Array) {\n\t\tthis._instance = blake3.create({\n\t\t\tdkLen: outputLength,\n\t\t\tkey\n\t\t});\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param block The block to operate on.\n\t * @param key Optional key for the hash.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(block: Uint8Array, key?: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(Blake3.CLASS_NAME, nameof(block), block);\n\t\treturn new Blake3(Blake3.SIZE_256, key).update(block).digest();\n\t}\n\n\t/**\n\t * Perform Sum 512 on the block.\n\t * @param block The block to operate on.\n\t * @param key Optional key for the hash.\n\t * @returns The sum 512 of the block.\n\t */\n\tpublic static sum512(block: Uint8Array, key?: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(Blake3.CLASS_NAME, nameof(block), block);\n\t\treturn new Blake3(Blake3.SIZE_512, key).update(block).digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): Blake3 {\n\t\tGuards.uint8Array(Blake3.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
4
|
+
import { sha1 } from "@noble/hashes/legacy.js";
|
|
5
|
+
import { Guards } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Class to help with HmacSha1 scheme.
|
|
8
|
+
*/
|
|
9
|
+
export class HmacSha1 {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static CLASS_NAME = "HmacSha1";
|
|
14
|
+
/**
|
|
15
|
+
* The instance of the hash.
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
_instance;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new instance of HmacSha1.
|
|
22
|
+
* @param key The key for the hmac.
|
|
23
|
+
*/
|
|
24
|
+
constructor(key) {
|
|
25
|
+
this._instance = hmac.create(sha1, key);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Perform Sum on the block.
|
|
29
|
+
* @param key The key for the hmac.
|
|
30
|
+
* @param block The block to operate on.
|
|
31
|
+
* @returns The sum of the block.
|
|
32
|
+
*/
|
|
33
|
+
static sum(key, block) {
|
|
34
|
+
Guards.uint8Array(HmacSha1.CLASS_NAME, "key", key);
|
|
35
|
+
Guards.uint8Array(HmacSha1.CLASS_NAME, "block", block);
|
|
36
|
+
return new HmacSha1(key).update(block).digest();
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Update the hash with the block.
|
|
40
|
+
* @param block The block to update the hash with.
|
|
41
|
+
* @returns The instance for chaining.
|
|
42
|
+
*/
|
|
43
|
+
update(block) {
|
|
44
|
+
Guards.uint8Array(HmacSha1.CLASS_NAME, "block", block);
|
|
45
|
+
this._instance.update(block);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the digest for the hash.
|
|
50
|
+
* @returns The instance for chaining.
|
|
51
|
+
*/
|
|
52
|
+
digest() {
|
|
53
|
+
return this._instance.digest();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=hmacSha1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmacSha1.js","sourceRoot":"","sources":["../../../src/hashes/hmacSha1.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,QAAQ;IACpB;;OAEG;IACI,MAAM,CAAU,UAAU,cAA8B;IAE/D;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;;OAGG;IACH,YAAY,GAAe;QAC1B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACzC,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,GAAG,CAAC,GAAe,EAAE,KAAiB;QACnD,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QACzD,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC7D,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IACjD,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC7D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha1 } from \"@noble/hashes/legacy.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Class to help with HmacSha1 scheme.\n */\nexport class HmacSha1 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<HmacSha1>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of HmacSha1.\n\t * @param key The key for the hmac.\n\t */\n\tconstructor(key: Uint8Array) {\n\t\tthis._instance = hmac.create(sha1, key);\n\t}\n\n\t/**\n\t * Perform Sum on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum of the block.\n\t */\n\tpublic static sum(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha1.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha1.CLASS_NAME, nameof(block), block);\n\t\treturn new HmacSha1(key).update(block).digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): HmacSha1 {\n\t\tGuards.uint8Array(HmacSha1.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
4
|
+
import { sha224, sha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { GeneralError, Guards } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Class to help with HmacSha256 scheme.
|
|
8
|
+
*/
|
|
9
|
+
export class HmacSha256 {
|
|
10
|
+
/**
|
|
11
|
+
* Sha256 256.
|
|
12
|
+
*/
|
|
13
|
+
static SIZE_256 = 256;
|
|
14
|
+
/**
|
|
15
|
+
* Sha256 224.
|
|
16
|
+
*/
|
|
17
|
+
static SIZE_224 = 224;
|
|
18
|
+
/**
|
|
19
|
+
* Runtime name for the class.
|
|
20
|
+
*/
|
|
21
|
+
static CLASS_NAME = "HmacSha256";
|
|
22
|
+
/**
|
|
23
|
+
* The instance of the hash.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
+
_instance;
|
|
28
|
+
/**
|
|
29
|
+
* Create a new instance of HmacSha256.
|
|
30
|
+
* @param key The key for the hmac.
|
|
31
|
+
* @param bits The number of bits.
|
|
32
|
+
*/
|
|
33
|
+
constructor(key, bits = HmacSha256.SIZE_256) {
|
|
34
|
+
if (bits !== HmacSha256.SIZE_224 && bits !== HmacSha256.SIZE_256) {
|
|
35
|
+
throw new GeneralError(HmacSha256.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
36
|
+
}
|
|
37
|
+
this._instance = hmac.create(bits === HmacSha256.SIZE_256 ? sha256 : sha224, key);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Perform Sum 224 on the block.
|
|
41
|
+
* @param key The key for the hmac.
|
|
42
|
+
* @param block The block to operate on.
|
|
43
|
+
* @returns The sum 224 of the block.
|
|
44
|
+
*/
|
|
45
|
+
static sum224(key, block) {
|
|
46
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "key", key);
|
|
47
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "block", block);
|
|
48
|
+
const instance = new HmacSha256(key, HmacSha256.SIZE_224);
|
|
49
|
+
instance.update(block);
|
|
50
|
+
return instance.digest();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Perform Sum 256 on the block.
|
|
54
|
+
* @param key The key for the hmac.
|
|
55
|
+
* @param block The block to operate on.
|
|
56
|
+
* @returns The sum 256 of the block.
|
|
57
|
+
*/
|
|
58
|
+
static sum256(key, block) {
|
|
59
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "key", key);
|
|
60
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "block", block);
|
|
61
|
+
const instance = new HmacSha256(key, HmacSha256.SIZE_256);
|
|
62
|
+
instance.update(block);
|
|
63
|
+
return instance.digest();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Update the hash with the block.
|
|
67
|
+
* @param block The block to update the hash with.
|
|
68
|
+
* @returns The instance for chaining.
|
|
69
|
+
*/
|
|
70
|
+
update(block) {
|
|
71
|
+
Guards.uint8Array(HmacSha256.CLASS_NAME, "block", block);
|
|
72
|
+
this._instance.update(block);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get the digest for the hash.
|
|
77
|
+
* @returns The instance for chaining.
|
|
78
|
+
*/
|
|
79
|
+
digest() {
|
|
80
|
+
return this._instance.digest();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=hmacSha256.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmacSha256.js","sourceRoot":"","sources":["../../../src/hashes/hmacSha256.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGtD;;GAEG;AACH,MAAM,OAAO,UAAU;IACtB;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,QAAQ,GAAW,GAAG,CAAC;IAE9C;;OAEG;IACI,MAAM,CAAU,UAAU,gBAAgC;IAEjE;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;;;OAIG;IACH,YAAY,GAAe,EAAE,OAAe,UAAU,CAAC,QAAQ;QAC9D,IAAI,IAAI,KAAK,UAAU,CAAC,QAAQ,IAAI,IAAI,KAAK,UAAU,CAAC,QAAQ,EAAE,CAAC;YAClE,MAAM,IAAI,YAAY,CAAC,UAAU,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7E,CAAC;QAED,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACnF,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,GAAe,EAAE,KAAiB;QACtD,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QAC3D,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC/D,MAAM,QAAQ,GAAG,IAAI,UAAU,CAAC,GAAG,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;QAC1D,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvB,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,GAAe,EAAE,KAAiB;QACtD,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QAC3D,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC/D,MAAM,QAAQ,GAAG,IAAI,UAAU,CAAC,GAAG,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;QAC1D,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvB,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QAC/D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha224, sha256 } from \"@noble/hashes/sha2.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { GeneralError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Class to help with HmacSha256 scheme.\n */\nexport class HmacSha256 {\n\t/**\n\t * Sha256 256.\n\t */\n\tpublic static readonly SIZE_256: number = 256;\n\n\t/**\n\t * Sha256 224.\n\t */\n\tpublic static readonly SIZE_224: number = 224;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<HmacSha256>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of HmacSha256.\n\t * @param key The key for the hmac.\n\t * @param bits The number of bits.\n\t */\n\tconstructor(key: Uint8Array, bits: number = HmacSha256.SIZE_256) {\n\t\tif (bits !== HmacSha256.SIZE_224 && bits !== HmacSha256.SIZE_256) {\n\t\t\tthrow new GeneralError(HmacSha256.CLASS_NAME, \"bitSize\", { bitSize: bits });\n\t\t}\n\n\t\tthis._instance = hmac.create(bits === HmacSha256.SIZE_256 ? sha256 : sha224, key);\n\t}\n\n\t/**\n\t * Perform Sum 224 on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum 224 of the block.\n\t */\n\tpublic static sum224(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha256.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha256.CLASS_NAME, nameof(block), block);\n\t\tconst instance = new HmacSha256(key, HmacSha256.SIZE_224);\n\t\tinstance.update(block);\n\t\treturn instance.digest();\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha256.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha256.CLASS_NAME, nameof(block), block);\n\t\tconst instance = new HmacSha256(key, HmacSha256.SIZE_256);\n\t\tinstance.update(block);\n\t\treturn instance.digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): HmacSha256 {\n\t\tGuards.uint8Array(HmacSha256.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
/* eslint-disable camelcase */
|
|
4
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
5
|
+
import { sha384, sha512, sha512_224, sha512_256 } from "@noble/hashes/sha2.js";
|
|
6
|
+
import { GeneralError, Guards } from "@twin.org/core";
|
|
7
|
+
/**
|
|
8
|
+
* Class to help with HmacSha512 scheme.
|
|
9
|
+
*/
|
|
10
|
+
export class HmacSha512 {
|
|
11
|
+
/**
|
|
12
|
+
* Sha512 224.
|
|
13
|
+
*/
|
|
14
|
+
static SIZE_224 = 224;
|
|
15
|
+
/**
|
|
16
|
+
* Sha512 256.
|
|
17
|
+
*/
|
|
18
|
+
static SIZE_256 = 256;
|
|
19
|
+
/**
|
|
20
|
+
* Sha512 384.
|
|
21
|
+
*/
|
|
22
|
+
static SIZE_384 = 384;
|
|
23
|
+
/**
|
|
24
|
+
* Sha512 512.
|
|
25
|
+
*/
|
|
26
|
+
static SIZE_512 = 512;
|
|
27
|
+
/**
|
|
28
|
+
* Runtime name for the class.
|
|
29
|
+
*/
|
|
30
|
+
static CLASS_NAME = "HmacSha512";
|
|
31
|
+
/**
|
|
32
|
+
* The instance of the hash.
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
_instance;
|
|
37
|
+
/**
|
|
38
|
+
* Create a new instance of HmacSha512.
|
|
39
|
+
* @param key The key for the hmac.
|
|
40
|
+
* @param bits The number of bits.
|
|
41
|
+
*/
|
|
42
|
+
constructor(key, bits = HmacSha512.SIZE_512) {
|
|
43
|
+
if (bits !== HmacSha512.SIZE_224 &&
|
|
44
|
+
bits !== HmacSha512.SIZE_256 &&
|
|
45
|
+
bits !== HmacSha512.SIZE_384 &&
|
|
46
|
+
bits !== HmacSha512.SIZE_512) {
|
|
47
|
+
throw new GeneralError(HmacSha512.CLASS_NAME, "bitSize", { bitSize: bits });
|
|
48
|
+
}
|
|
49
|
+
if (bits === HmacSha512.SIZE_224) {
|
|
50
|
+
this._instance = hmac.create(sha512_224, key);
|
|
51
|
+
}
|
|
52
|
+
else if (bits === HmacSha512.SIZE_256) {
|
|
53
|
+
this._instance = hmac.create(sha512_256, key);
|
|
54
|
+
}
|
|
55
|
+
else if (bits === HmacSha512.SIZE_384) {
|
|
56
|
+
this._instance = hmac.create(sha384, key);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
this._instance = hmac.create(sha512, key);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Perform Sum 512 on the block.
|
|
64
|
+
* @param key The key for the hmac.
|
|
65
|
+
* @param block The block to operate on.
|
|
66
|
+
* @returns The sum 512 of the block.
|
|
67
|
+
*/
|
|
68
|
+
static sum512(key, block) {
|
|
69
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
70
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
71
|
+
const instance = new HmacSha512(key, HmacSha512.SIZE_512);
|
|
72
|
+
instance.update(block);
|
|
73
|
+
return instance.digest();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Perform Sum 384 on the block.
|
|
77
|
+
* @param key The key for the hmac.
|
|
78
|
+
* @param block The block to operate on.
|
|
79
|
+
* @returns The sum 384 of the block.
|
|
80
|
+
*/
|
|
81
|
+
static sum384(key, block) {
|
|
82
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
83
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
84
|
+
const instance = new HmacSha512(key, HmacSha512.SIZE_384);
|
|
85
|
+
instance.update(block);
|
|
86
|
+
return instance.digest();
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Perform Sum 256 on the block.
|
|
90
|
+
* @param key The key for the hmac.
|
|
91
|
+
* @param block The block to operate on.
|
|
92
|
+
* @returns The sum 256 of the block.
|
|
93
|
+
*/
|
|
94
|
+
static sum256(key, block) {
|
|
95
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
96
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
97
|
+
const instance = new HmacSha512(key, HmacSha512.SIZE_256);
|
|
98
|
+
instance.update(block);
|
|
99
|
+
return instance.digest();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Perform Sum 224 on the block.
|
|
103
|
+
* @param key The key for the hmac.
|
|
104
|
+
* @param block The block to operate on.
|
|
105
|
+
* @returns The sum 224 of the block.
|
|
106
|
+
*/
|
|
107
|
+
static sum224(key, block) {
|
|
108
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "key", key);
|
|
109
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
110
|
+
const instance = new HmacSha512(key, HmacSha512.SIZE_224);
|
|
111
|
+
instance.update(block);
|
|
112
|
+
return instance.digest();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Update the hash with the block.
|
|
116
|
+
* @param block The block to update the hash with.
|
|
117
|
+
* @returns The instance for chaining.
|
|
118
|
+
*/
|
|
119
|
+
update(block) {
|
|
120
|
+
Guards.uint8Array(HmacSha512.CLASS_NAME, "block", block);
|
|
121
|
+
this._instance.update(block);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Get the digest for the hash.
|
|
126
|
+
* @returns The instance for chaining.
|
|
127
|
+
*/
|
|
128
|
+
digest() {
|
|
129
|
+
return this._instance.digest();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=hmacSha512.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmacSha512.js","sourceRoot":"","sources":["../../../src/hashes/hmacSha512.ts"],"names":[],"mappings":"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","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable camelcase */\n\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha384, sha512, sha512_224, sha512_256 } from \"@noble/hashes/sha2.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { GeneralError, Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Class to help with HmacSha512 scheme.\n */\nexport class HmacSha512 {\n\t/**\n\t * Sha512 224.\n\t */\n\tpublic static SIZE_224: number = 224;\n\n\t/**\n\t * Sha512 256.\n\t */\n\tpublic static SIZE_256: number = 256;\n\n\t/**\n\t * Sha512 384.\n\t */\n\tpublic static SIZE_384: number = 384;\n\n\t/**\n\t * Sha512 512.\n\t */\n\tpublic static SIZE_512: number = 512;\n\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<HmacSha512>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of HmacSha512.\n\t * @param key The key for the hmac.\n\t * @param bits The number of bits.\n\t */\n\tconstructor(key: Uint8Array, bits: number = HmacSha512.SIZE_512) {\n\t\tif (\n\t\t\tbits !== HmacSha512.SIZE_224 &&\n\t\t\tbits !== HmacSha512.SIZE_256 &&\n\t\t\tbits !== HmacSha512.SIZE_384 &&\n\t\t\tbits !== HmacSha512.SIZE_512\n\t\t) {\n\t\t\tthrow new GeneralError(HmacSha512.CLASS_NAME, \"bitSize\", { bitSize: bits });\n\t\t}\n\n\t\tif (bits === HmacSha512.SIZE_224) {\n\t\t\tthis._instance = hmac.create(sha512_224, key);\n\t\t} else if (bits === HmacSha512.SIZE_256) {\n\t\t\tthis._instance = hmac.create(sha512_256, key);\n\t\t} else if (bits === HmacSha512.SIZE_384) {\n\t\t\tthis._instance = hmac.create(sha384, key);\n\t\t} else {\n\t\t\tthis._instance = hmac.create(sha512, key);\n\t\t}\n\t}\n\n\t/**\n\t * Perform Sum 512 on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum 512 of the block.\n\t */\n\tpublic static sum512(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(block), block);\n\t\tconst instance = new HmacSha512(key, HmacSha512.SIZE_512);\n\t\tinstance.update(block);\n\t\treturn instance.digest();\n\t}\n\n\t/**\n\t * Perform Sum 384 on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum 384 of the block.\n\t */\n\tpublic static sum384(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(block), block);\n\t\tconst instance = new HmacSha512(key, HmacSha512.SIZE_384);\n\t\tinstance.update(block);\n\t\treturn instance.digest();\n\t}\n\n\t/**\n\t * Perform Sum 256 on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum 256 of the block.\n\t */\n\tpublic static sum256(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(block), block);\n\t\tconst instance = new HmacSha512(key, HmacSha512.SIZE_256);\n\t\tinstance.update(block);\n\t\treturn instance.digest();\n\t}\n\n\t/**\n\t * Perform Sum 224 on the block.\n\t * @param key The key for the hmac.\n\t * @param block The block to operate on.\n\t * @returns The sum 224 of the block.\n\t */\n\tpublic static sum224(key: Uint8Array, block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(key), key);\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(block), block);\n\t\tconst instance = new HmacSha512(key, HmacSha512.SIZE_224);\n\t\tinstance.update(block);\n\t\treturn instance.digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): HmacSha512 {\n\t\tGuards.uint8Array(HmacSha512.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { pbkdf2 } from "@noble/hashes/pbkdf2.js";
|
|
4
|
+
import { sha256, sha512 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { Guards } from "@twin.org/core";
|
|
6
|
+
/**
|
|
7
|
+
* Implementation of the password based key derivation function 2.
|
|
8
|
+
*/
|
|
9
|
+
export class Pbkdf2 {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static CLASS_NAME = "Pbkdf2";
|
|
14
|
+
/**
|
|
15
|
+
* Derive a key from the parameters using Sha256.
|
|
16
|
+
* @param password The password to derive the key from.
|
|
17
|
+
* @param salt The salt for the derivation.
|
|
18
|
+
* @param iterations Number of iterations to perform.
|
|
19
|
+
* @param keyLength The length of the key to derive.
|
|
20
|
+
* @returns The derived key.
|
|
21
|
+
*/
|
|
22
|
+
static sha256(password, salt, iterations, keyLength) {
|
|
23
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "password", password);
|
|
24
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "salt", salt);
|
|
25
|
+
Guards.number(Pbkdf2.CLASS_NAME, "iterations", iterations);
|
|
26
|
+
Guards.number(Pbkdf2.CLASS_NAME, "keyLength", keyLength);
|
|
27
|
+
return pbkdf2(sha256, password, salt, { c: iterations, dkLen: keyLength });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Derive a key from the parameters using Sha512.
|
|
31
|
+
* @param password The password to derive the key from.
|
|
32
|
+
* @param salt The salt for the derivation.
|
|
33
|
+
* @param iterations Number of iterations to perform.
|
|
34
|
+
* @param keyLength The length of the key to derive.
|
|
35
|
+
* @returns The derived key.
|
|
36
|
+
*/
|
|
37
|
+
static sha512(password, salt, iterations, keyLength) {
|
|
38
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "password", password);
|
|
39
|
+
Guards.uint8Array(Pbkdf2.CLASS_NAME, "salt", salt);
|
|
40
|
+
Guards.number(Pbkdf2.CLASS_NAME, "iterations", iterations);
|
|
41
|
+
Guards.number(Pbkdf2.CLASS_NAME, "keyLength", keyLength);
|
|
42
|
+
return pbkdf2(sha512, password, salt, { c: iterations, dkLen: keyLength });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=pbkdf2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pbkdf2.js","sourceRoot":"","sources":["../../../src/hashes/pbkdf2.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;OAEG;IACI,MAAM,CAAU,UAAU,YAA4B;IAE7D;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,QAAoB,EACpB,IAAgB,EAChB,UAAkB,EAClB,SAAiB;QAEjB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QACjE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,QAAoB,EACpB,IAAgB,EAChB,UAAkB,EAClB,SAAiB;QAEjB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,cAAoB,QAAQ,CAAC,CAAC;QACjE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,UAAgB,IAAI,CAAC,CAAC;QACzD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,eAAqB,SAAS,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { pbkdf2 } from \"@noble/hashes/pbkdf2.js\";\nimport { sha256, sha512 } from \"@noble/hashes/sha2.js\";\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Implementation of the password based key derivation function 2.\n */\nexport class Pbkdf2 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Pbkdf2>();\n\n\t/**\n\t * Derive a key from the parameters using Sha256.\n\t * @param password The password to derive the key from.\n\t * @param salt The salt for the derivation.\n\t * @param iterations Number of iterations to perform.\n\t * @param keyLength The length of the key to derive.\n\t * @returns The derived key.\n\t */\n\tpublic static sha256(\n\t\tpassword: Uint8Array,\n\t\tsalt: Uint8Array,\n\t\titerations: number,\n\t\tkeyLength: number\n\t): Uint8Array {\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(password), password);\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(salt), salt);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(iterations), iterations);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(keyLength), keyLength);\n\t\treturn pbkdf2(sha256, password, salt, { c: iterations, dkLen: keyLength });\n\t}\n\n\t/**\n\t * Derive a key from the parameters using Sha512.\n\t * @param password The password to derive the key from.\n\t * @param salt The salt for the derivation.\n\t * @param iterations Number of iterations to perform.\n\t * @param keyLength The length of the key to derive.\n\t * @returns The derived key.\n\t */\n\tpublic static sha512(\n\t\tpassword: Uint8Array,\n\t\tsalt: Uint8Array,\n\t\titerations: number,\n\t\tkeyLength: number\n\t): Uint8Array {\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(password), password);\n\t\tGuards.uint8Array(Pbkdf2.CLASS_NAME, nameof(salt), salt);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(iterations), iterations);\n\t\tGuards.number(Pbkdf2.CLASS_NAME, nameof(keyLength), keyLength);\n\t\treturn pbkdf2(sha512, password, salt, { c: iterations, dkLen: keyLength });\n\t}\n}\n"]}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { sha1 } from "@noble/hashes/legacy.js";
|
|
4
|
+
import { Guards } from "@twin.org/core";
|
|
5
|
+
/**
|
|
6
|
+
* Perform a SHA-1 hash on the block.
|
|
7
|
+
*/
|
|
8
|
+
export class Sha1 {
|
|
9
|
+
/**
|
|
10
|
+
* Runtime name for the class.
|
|
11
|
+
*/
|
|
12
|
+
static CLASS_NAME = "Sha1";
|
|
13
|
+
/**
|
|
14
|
+
* The instance of the hash.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
_instance;
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of Sha1.
|
|
21
|
+
*/
|
|
22
|
+
constructor() {
|
|
23
|
+
this._instance = sha1.create();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Perform Sum on the block.
|
|
27
|
+
* @param block The block to operate on.
|
|
28
|
+
* @returns The sum of the block.
|
|
29
|
+
*/
|
|
30
|
+
static sum(block) {
|
|
31
|
+
Guards.uint8Array(Sha1.CLASS_NAME, "block", block);
|
|
32
|
+
return new Sha1().update(block).digest();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Update the hash with the block.
|
|
36
|
+
* @param block The block to update the hash with.
|
|
37
|
+
* @returns The instance for chaining.
|
|
38
|
+
*/
|
|
39
|
+
update(block) {
|
|
40
|
+
Guards.uint8Array(Sha1.CLASS_NAME, "block", block);
|
|
41
|
+
this._instance.update(block);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the digest for the hash.
|
|
46
|
+
* @returns The instance for chaining.
|
|
47
|
+
*/
|
|
48
|
+
digest() {
|
|
49
|
+
return this._instance.digest();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=sha1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sha1.js","sourceRoot":"","sources":["../../../src/hashes/sha1.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAGxC;;GAEG;AACH,MAAM,OAAO,IAAI;IAChB;;OAEG;IACI,MAAM,CAAU,UAAU,UAA0B;IAE3D;;;OAGG;IACH,8DAA8D;IAC7C,SAAS,CAAY;IAEtC;;OAEG;IACH;QACC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,GAAG,CAAC,KAAiB;QAClC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QACzD,OAAO,IAAI,IAAI,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;IAC1C,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAiB;QAC9B,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,WAAiB,KAAK,CAAC,CAAC;QACzD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC7B,OAAO,IAAI,CAAC;IACb,CAAC;IAED;;;OAGG;IACI,MAAM;QACZ,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { sha1 } from \"@noble/hashes/legacy.js\";\nimport type { Hash } from \"@noble/hashes/utils.js\";\nimport { Guards } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Perform a SHA-1 hash on the block.\n */\nexport class Sha1 {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<Sha1>();\n\n\t/**\n\t * The instance of the hash.\n\t * @internal\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tprivate readonly _instance: Hash<any>;\n\n\t/**\n\t * Create a new instance of Sha1.\n\t */\n\tconstructor() {\n\t\tthis._instance = sha1.create();\n\t}\n\n\t/**\n\t * Perform Sum on the block.\n\t * @param block The block to operate on.\n\t * @returns The sum of the block.\n\t */\n\tpublic static sum(block: Uint8Array): Uint8Array {\n\t\tGuards.uint8Array(Sha1.CLASS_NAME, nameof(block), block);\n\t\treturn new Sha1().update(block).digest();\n\t}\n\n\t/**\n\t * Update the hash with the block.\n\t * @param block The block to update the hash with.\n\t * @returns The instance for chaining.\n\t */\n\tpublic update(block: Uint8Array): Sha1 {\n\t\tGuards.uint8Array(Sha1.CLASS_NAME, nameof(block), block);\n\t\tthis._instance.update(block);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get the digest for the hash.\n\t * @returns The instance for chaining.\n\t */\n\tpublic digest(): Uint8Array {\n\t\treturn this._instance.digest();\n\t}\n}\n"]}
|