@decryption/hashes 0.2.0

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.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/_blake.d.ts +20 -0
  4. package/_blake.js +56 -0
  5. package/_md.d.ts +98 -0
  6. package/_md.js +207 -0
  7. package/_u64.d.ts +62 -0
  8. package/_u64.js +106 -0
  9. package/argon2.d.ts +111 -0
  10. package/argon2.js +525 -0
  11. package/blake1.d.ts +155 -0
  12. package/blake1.js +599 -0
  13. package/blake2.d.ts +183 -0
  14. package/blake2.js +540 -0
  15. package/blake3.d.ts +67 -0
  16. package/blake3.js +287 -0
  17. package/eskdf.d.ts +91 -0
  18. package/eskdf.js +223 -0
  19. package/esm/_blake.js +51 -0
  20. package/esm/_md.js +201 -0
  21. package/esm/_u64.js +83 -0
  22. package/esm/argon2.js +516 -0
  23. package/esm/blake1.js +559 -0
  24. package/esm/blake2.js +500 -0
  25. package/esm/blake3.js +283 -0
  26. package/esm/eskdf.js +217 -0
  27. package/esm/hkdf.js +119 -0
  28. package/esm/hmac.js +92 -0
  29. package/esm/index.js +34 -0
  30. package/esm/legacy.js +326 -0
  31. package/esm/pbkdf2.js +134 -0
  32. package/esm/scrypt.js +256 -0
  33. package/esm/sha2.js +457 -0
  34. package/esm/sha3-addons.js +782 -0
  35. package/esm/sha3.js +430 -0
  36. package/esm/utils.js +577 -0
  37. package/esm/webcrypto.js +163 -0
  38. package/hkdf.d.ts +63 -0
  39. package/hkdf.js +125 -0
  40. package/hmac.d.ts +46 -0
  41. package/hmac.js +96 -0
  42. package/index.d.ts +0 -0
  43. package/index.js +34 -0
  44. package/legacy.d.ts +101 -0
  45. package/legacy.js +332 -0
  46. package/package.json +47 -0
  47. package/pbkdf2.d.ts +55 -0
  48. package/pbkdf2.js +138 -0
  49. package/scrypt.d.ts +64 -0
  50. package/scrypt.js +260 -0
  51. package/sha2.d.ts +248 -0
  52. package/sha2.js +499 -0
  53. package/sha3-addons.d.ts +460 -0
  54. package/sha3-addons.js +791 -0
  55. package/sha3.d.ts +198 -0
  56. package/sha3.js +435 -0
  57. package/utils.d.ts +518 -0
  58. package/utils.js +606 -0
  59. package/webcrypto.d.ts +119 -0
  60. package/webcrypto.js +168 -0
package/webcrypto.d.ts ADDED
@@ -0,0 +1,119 @@
1
+ import { type Pbkdf2Opt } from './pbkdf2';
2
+ import { type KDFInput, type TArg, type TRet } from './utils';
3
+ /** Callable WebCrypto hash function descriptor. */
4
+ export type WebHash = {
5
+ /**
6
+ * Hashes one message with the selected WebCrypto digest.
7
+ * @param msg - message bytes to hash
8
+ * @returns Promise resolving to digest bytes.
9
+ */
10
+ (msg: TArg<Uint8Array>): Promise<TRet<Uint8Array>>;
11
+ /** WebCrypto algorithm name passed to `crypto.subtle`. */
12
+ webCryptoName: string;
13
+ /** Digest size in bytes. */
14
+ outputLen: number;
15
+ /** Input block size in bytes. */
16
+ blockLen: number;
17
+ };
18
+ /** WebCrypto SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
19
+ /**
20
+ * WebCrypto SHA2-256 hash function from RFC 6234.
21
+ * @param msg - message bytes to hash
22
+ * @returns Promise resolving to digest bytes.
23
+ * @example
24
+ * Hash a message with WebCrypto SHA2-256.
25
+ * ```ts
26
+ * await sha256(new Uint8Array([97, 98, 99]));
27
+ * ```
28
+ */
29
+ export declare const sha256: TRet<WebHash>;
30
+ /**
31
+ * WebCrypto SHA2-384 hash function from RFC 6234.
32
+ * @param msg - message bytes to hash
33
+ * @returns Promise resolving to digest bytes.
34
+ * @example
35
+ * Hash a message with WebCrypto SHA2-384.
36
+ * ```ts
37
+ * await sha384(new Uint8Array([97, 98, 99]));
38
+ * ```
39
+ */
40
+ export declare const sha384: TRet<WebHash>;
41
+ /**
42
+ * WebCrypto SHA2-512 hash function from RFC 6234.
43
+ * @param msg - message bytes to hash
44
+ * @returns Promise resolving to digest bytes.
45
+ * @example
46
+ * Hash a message with WebCrypto SHA2-512.
47
+ * ```ts
48
+ * await sha512(new Uint8Array([97, 98, 99]));
49
+ * ```
50
+ */
51
+ export declare const sha512: TRet<WebHash>;
52
+ /**
53
+ * WebCrypto HMAC: RFC2104 message authentication code.
54
+ * @param hash - function that would be used e.g. sha256. Webcrypto version.
55
+ * @param key - authentication key bytes
56
+ * @param message - message bytes to authenticate
57
+ * @returns Promise resolving to authentication tag bytes.
58
+ * `.create()` exists only to mirror the synchronous API surface
59
+ * and always throws `not implemented`.
60
+ * @example
61
+ * Compute an RFC 2104 HMAC with WebCrypto.
62
+ * ```ts
63
+ * import { hmac, sha256 } from '@decryption/hashes/webcrypto.js';
64
+ * await hmac(sha256, new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]));
65
+ * ```
66
+ */
67
+ type WebHmacFn = {
68
+ (hash: TArg<WebHash>, key: TArg<Uint8Array>, message: TArg<Uint8Array>): Promise<TRet<Uint8Array>>;
69
+ create(hash: TArg<WebHash>, key: TArg<Uint8Array>): any;
70
+ };
71
+ export declare const hmac: TRet<WebHmacFn>;
72
+ /**
73
+ * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
74
+ * Combines hkdf_extract + hkdf_expand in one step
75
+ * @param hash - hash function that would be used (e.g. sha256). Webcrypto version.
76
+ * @param ikm - input keying material, the initial key
77
+ * @param salt - optional salt value (a non-secret random value)
78
+ * @param info - optional context and application specific information bytes
79
+ * @param length - length of output keying material in bytes.
80
+ * RFC 5869 §2.3 allows `0..255*HashLen`, so `0` requests an empty OKM.
81
+ * @returns Promise resolving to derived key bytes.
82
+ * The RFC `L <= 255 * HashLen` bound is currently enforced only by backend
83
+ * `deriveBits()` rejection, not by an explicit library-side guard.
84
+ * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
85
+ * @example
86
+ * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
87
+ * ```ts
88
+ * import { hkdf, sha256 } from '@decryption/hashes/webcrypto.js';
89
+ * import { randomBytes, utf8ToBytes } from '@decryption/hashes/utils.js';
90
+ * const inputKey = randomBytes(32);
91
+ * const salt = randomBytes(32);
92
+ * const info = utf8ToBytes('application-key');
93
+ * const okm = await hkdf(sha256, inputKey, salt, info, 32);
94
+ * ```
95
+ */
96
+ export declare function hkdf(hash: TArg<WebHash>, ikm: TArg<Uint8Array>, salt: TArg<Uint8Array | undefined>, info: TArg<Uint8Array | undefined>, length: number): Promise<TRet<Uint8Array>>;
97
+ /**
98
+ * WebCrypto PBKDF2-HMAC: RFC 8018 key derivation function.
99
+ * @param hash - hash function that would be used e.g. sha256. Webcrypto version.
100
+ * @param password - password from which a derived key is generated; string
101
+ * inputs are normalized through `kdfInputToBytes()`, i.e. UTF-8
102
+ * @param salt - cryptographic salt; string inputs are normalized through
103
+ * `kdfInputToBytes()`, i.e. UTF-8
104
+ * @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
105
+ * must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
106
+ * @returns Promise resolving to derived key bytes.
107
+ * Positive-iteration enforcement is currently delegated to backend
108
+ * `deriveBits()` rejection (for example `c = 0`), not a dedicated
109
+ * library-side guard.
110
+ * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
111
+ * @example
112
+ * WebCrypto PBKDF2-HMAC: RFC 2898 key derivation function.
113
+ * ```ts
114
+ * import { pbkdf2, sha256 } from '@decryption/hashes/webcrypto.js';
115
+ * const key = await pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
116
+ * ```
117
+ */
118
+ export declare function pbkdf2(hash: TArg<WebHash>, password: TArg<KDFInput>, salt: TArg<KDFInput>, opts: Pbkdf2Opt): Promise<TRet<Uint8Array>>;
119
+ export {};
package/webcrypto.js ADDED
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hmac = exports.sha512 = exports.sha384 = exports.sha256 = void 0;
4
+ exports.hkdf = hkdf;
5
+ exports.pbkdf2 = pbkdf2;
6
+ const utils_1 = require("./utils");
7
+ function _subtle() {
8
+ const cr = typeof globalThis === 'object' ? globalThis.crypto : null;
9
+ const sb = cr?.subtle;
10
+ if (typeof sb === 'object' && sb != null)
11
+ return sb;
12
+ throw new Error('crypto.subtle must be defined');
13
+ }
14
+ function createWebHash(name, blockLen, outputLen) {
15
+ const hashC = async (msg) => {
16
+ (0, utils_1.abytes)(msg);
17
+ const crypto = _subtle();
18
+ return new Uint8Array(await crypto.digest(name, msg));
19
+ };
20
+ hashC.webCryptoName = name; // make sure it won't interfere with function name
21
+ hashC.outputLen = outputLen;
22
+ hashC.blockLen = blockLen;
23
+ hashC.create = () => {
24
+ // Present only so this async wrapper satisfies the shared
25
+ // hash-wrapper shape checked by `ahashWeb()`.
26
+ throw new Error('not implemented');
27
+ };
28
+ // Later WebCrypto HMAC/HKDF/PBKDF2 calls read descriptor metadata directly, so freezing prevents
29
+ // callers from retargeting a `sha256` wrapper into a different backend digest by mutation.
30
+ return Object.freeze(hashC);
31
+ }
32
+ function ahashWeb(hash) {
33
+ (0, utils_1.ahash)(hash);
34
+ if (typeof hash.webCryptoName !== 'string')
35
+ throw new Error('non-web hash');
36
+ }
37
+ /** WebCrypto SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
38
+ // export const sha1: WebHash = createHash('SHA-1', 64, 20);
39
+ /**
40
+ * WebCrypto SHA2-256 hash function from RFC 6234.
41
+ * @param msg - message bytes to hash
42
+ * @returns Promise resolving to digest bytes.
43
+ * @example
44
+ * Hash a message with WebCrypto SHA2-256.
45
+ * ```ts
46
+ * await sha256(new Uint8Array([97, 98, 99]));
47
+ * ```
48
+ */
49
+ exports.sha256 = createWebHash('SHA-256', 64, 32);
50
+ /**
51
+ * WebCrypto SHA2-384 hash function from RFC 6234.
52
+ * @param msg - message bytes to hash
53
+ * @returns Promise resolving to digest bytes.
54
+ * @example
55
+ * Hash a message with WebCrypto SHA2-384.
56
+ * ```ts
57
+ * await sha384(new Uint8Array([97, 98, 99]));
58
+ * ```
59
+ */
60
+ exports.sha384 = createWebHash('SHA-384', 128, 48);
61
+ /**
62
+ * WebCrypto SHA2-512 hash function from RFC 6234.
63
+ * @param msg - message bytes to hash
64
+ * @returns Promise resolving to digest bytes.
65
+ * @example
66
+ * Hash a message with WebCrypto SHA2-512.
67
+ * ```ts
68
+ * await sha512(new Uint8Array([97, 98, 99]));
69
+ * ```
70
+ */
71
+ exports.sha512 = createWebHash('SHA-512', 128, 64);
72
+ exports.hmac = (() => {
73
+ const hmac_ = async (hash, key, message) => {
74
+ const crypto = _subtle();
75
+ (0, utils_1.abytes)(key, undefined, 'key');
76
+ (0, utils_1.abytes)(message, undefined, 'message');
77
+ ahashWeb(hash);
78
+ // WebCrypto keys can't be zeroized
79
+ // prettier-ignore
80
+ const wkey = await crypto.importKey('raw', key, { name: 'HMAC', hash: hash.webCryptoName }, false, ['sign']);
81
+ return new Uint8Array(await crypto.sign('HMAC', wkey, message));
82
+ };
83
+ hmac_.create = (_hash, _key) => {
84
+ throw new Error('not implemented');
85
+ };
86
+ return hmac_;
87
+ })();
88
+ /**
89
+ * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
90
+ * Combines hkdf_extract + hkdf_expand in one step
91
+ * @param hash - hash function that would be used (e.g. sha256). Webcrypto version.
92
+ * @param ikm - input keying material, the initial key
93
+ * @param salt - optional salt value (a non-secret random value)
94
+ * @param info - optional context and application specific information bytes
95
+ * @param length - length of output keying material in bytes.
96
+ * RFC 5869 §2.3 allows `0..255*HashLen`, so `0` requests an empty OKM.
97
+ * @returns Promise resolving to derived key bytes.
98
+ * The RFC `L <= 255 * HashLen` bound is currently enforced only by backend
99
+ * `deriveBits()` rejection, not by an explicit library-side guard.
100
+ * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
101
+ * @example
102
+ * WebCrypto HKDF (RFC 5869): derive keys from an initial input.
103
+ * ```ts
104
+ * import { hkdf, sha256 } from '@decryption/hashes/webcrypto.js';
105
+ * import { randomBytes, utf8ToBytes } from '@decryption/hashes/utils.js';
106
+ * const inputKey = randomBytes(32);
107
+ * const salt = randomBytes(32);
108
+ * const info = utf8ToBytes('application-key');
109
+ * const okm = await hkdf(sha256, inputKey, salt, info, 32);
110
+ * ```
111
+ */
112
+ async function hkdf(hash, ikm, salt, info, length) {
113
+ const crypto = _subtle();
114
+ ahashWeb(hash);
115
+ (0, utils_1.abytes)(ikm, undefined, 'ikm');
116
+ (0, utils_1.anumber)(length, 'length');
117
+ if (salt !== undefined)
118
+ (0, utils_1.abytes)(salt, undefined, 'salt');
119
+ if (info !== undefined)
120
+ (0, utils_1.abytes)(info, undefined, 'info');
121
+ const wkey = await crypto.importKey('raw', ikm, 'HKDF', false, ['deriveBits']);
122
+ const opts = {
123
+ name: 'HKDF',
124
+ hash: hash.webCryptoName,
125
+ salt: salt === undefined ? new Uint8Array(0) : salt,
126
+ info: info === undefined ? new Uint8Array(0) : info,
127
+ };
128
+ return new Uint8Array(await crypto.deriveBits(opts, wkey, 8 * length));
129
+ }
130
+ /**
131
+ * WebCrypto PBKDF2-HMAC: RFC 8018 key derivation function.
132
+ * @param hash - hash function that would be used e.g. sha256. Webcrypto version.
133
+ * @param password - password from which a derived key is generated; string
134
+ * inputs are normalized through `kdfInputToBytes()`, i.e. UTF-8
135
+ * @param salt - cryptographic salt; string inputs are normalized through
136
+ * `kdfInputToBytes()`, i.e. UTF-8
137
+ * @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
138
+ * must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
139
+ * @returns Promise resolving to derived key bytes.
140
+ * Positive-iteration enforcement is currently delegated to backend
141
+ * `deriveBits()` rejection (for example `c = 0`), not a dedicated
142
+ * library-side guard.
143
+ * @throws If the current runtime does not provide `crypto.subtle`. {@link Error}
144
+ * @example
145
+ * WebCrypto PBKDF2-HMAC: RFC 2898 key derivation function.
146
+ * ```ts
147
+ * import { pbkdf2, sha256 } from '@decryption/hashes/webcrypto.js';
148
+ * const key = await pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
149
+ * ```
150
+ */
151
+ async function pbkdf2(hash, password, salt, opts) {
152
+ const crypto = _subtle();
153
+ ahashWeb(hash);
154
+ const _opts = (0, utils_1.checkOpts)({ dkLen: 32 }, opts);
155
+ const { c, dkLen } = _opts;
156
+ (0, utils_1.anumber)(c, 'c');
157
+ (0, utils_1.anumber)(dkLen, 'dkLen');
158
+ // RFC 8018 §5.2 defines dkLen as a positive integer.
159
+ if (dkLen < 1)
160
+ throw new Error('"dkLen" must be >= 1');
161
+ const _password = (0, utils_1.kdfInputToBytes)(password, 'password');
162
+ const _salt = (0, utils_1.kdfInputToBytes)(salt, 'salt');
163
+ const key = await crypto.importKey('raw', _password, 'PBKDF2', false, [
164
+ 'deriveBits',
165
+ ]);
166
+ const deriveOpts = { name: 'PBKDF2', salt: _salt, iterations: c, hash: hash.webCryptoName };
167
+ return new Uint8Array(await crypto.deriveBits(deriveOpts, key, 8 * dkLen));
168
+ }