@metamask-previews/cryptography 0.0.0-preview-ed70ef231 → 0.0.0-preview-ff2ab0583
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/CHANGELOG.md +3 -1
- package/dist/hmac.d.ts +35 -0
- package/dist/hmac.d.ts.map +1 -0
- package/dist/hmac.js +60 -0
- package/dist/hmac.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
-
- Initial release ([#10282](https://github.com/MetaMask/core/pull/10282))
|
|
12
|
+
- Initial release ([#10282](https://github.com/MetaMask/core/pull/10282), [#10431](https://github.com/MetaMask/core/pull/10431))
|
|
13
|
+
- Add `sha256`, `sha384`, and `sha512` functions for computing SHA digests
|
|
14
|
+
- Add `hmacSha256`, `hmacSha384`, and `hmacSha512` functions for computing HMAC digests
|
|
13
15
|
|
|
14
16
|
[Unreleased]: https://github.com/MetaMask/core/
|
package/dist/hmac.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type HmacOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* Skip the minimum key length check. Using a key shorter than the hash
|
|
4
|
+
* output length reduces the effective security strength of HMAC (RFC 2104).
|
|
5
|
+
*/
|
|
6
|
+
unsafeKeyLength?: boolean;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Compute the HMAC-SHA-256 of the given data using the given key.
|
|
10
|
+
*
|
|
11
|
+
* @param key - The key to use.
|
|
12
|
+
* @param data - The data to hash.
|
|
13
|
+
* @param options - Additional configuration options.
|
|
14
|
+
* @returns The HMAC-SHA-256 of the data.
|
|
15
|
+
*/
|
|
16
|
+
export declare function hmacSha256(key: BufferSource, data: BufferSource, options?: HmacOptions): Promise<Uint8Array>;
|
|
17
|
+
/**
|
|
18
|
+
* Compute the HMAC-SHA-384 of the given data using the given key.
|
|
19
|
+
*
|
|
20
|
+
* @param key - The key to use.
|
|
21
|
+
* @param data - The data to hash.
|
|
22
|
+
* @param options - Additional configuration options.
|
|
23
|
+
* @returns The HMAC-SHA-384 of the data.
|
|
24
|
+
*/
|
|
25
|
+
export declare function hmacSha384(key: BufferSource, data: BufferSource, options?: HmacOptions): Promise<Uint8Array>;
|
|
26
|
+
/**
|
|
27
|
+
* Compute the HMAC-SHA-512 of the given data using the given key.
|
|
28
|
+
*
|
|
29
|
+
* @param key - The key to use.
|
|
30
|
+
* @param data - The data to hash.
|
|
31
|
+
* @param options - Additional configuration options.
|
|
32
|
+
* @returns The HMAC-SHA-512 of the data.
|
|
33
|
+
*/
|
|
34
|
+
export declare function hmacSha512(key: BufferSource, data: BufferSource, options?: HmacOptions): Promise<Uint8Array>;
|
|
35
|
+
//# sourceMappingURL=hmac.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmac.d.ts","sourceRoot":"","sources":["../src/hmac.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,WAAW,GAAG;IACxB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,UAAU,CAAC,CAErB;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,UAAU,CAAC,CAErB;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,UAAU,CAAC,CAErB"}
|
package/dist/hmac.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// https://datatracker.ietf.org/doc/html/rfc2104#section-3
|
|
2
|
+
const MIN_KEY_LENGTH = {
|
|
3
|
+
'SHA-256': 32,
|
|
4
|
+
'SHA-384': 48,
|
|
5
|
+
'SHA-512': 64,
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Compute the HMAC-SHA-256 of the given data using the given key.
|
|
9
|
+
*
|
|
10
|
+
* @param key - The key to use.
|
|
11
|
+
* @param data - The data to hash.
|
|
12
|
+
* @param options - Additional configuration options.
|
|
13
|
+
* @returns The HMAC-SHA-256 of the data.
|
|
14
|
+
*/
|
|
15
|
+
export async function hmacSha256(key, data, options) {
|
|
16
|
+
return hmac(key, 'SHA-256', data, options);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Compute the HMAC-SHA-384 of the given data using the given key.
|
|
20
|
+
*
|
|
21
|
+
* @param key - The key to use.
|
|
22
|
+
* @param data - The data to hash.
|
|
23
|
+
* @param options - Additional configuration options.
|
|
24
|
+
* @returns The HMAC-SHA-384 of the data.
|
|
25
|
+
*/
|
|
26
|
+
export async function hmacSha384(key, data, options) {
|
|
27
|
+
return hmac(key, 'SHA-384', data, options);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Compute the HMAC-SHA-512 of the given data using the given key.
|
|
31
|
+
*
|
|
32
|
+
* @param key - The key to use.
|
|
33
|
+
* @param data - The data to hash.
|
|
34
|
+
* @param options - Additional configuration options.
|
|
35
|
+
* @returns The HMAC-SHA-512 of the data.
|
|
36
|
+
*/
|
|
37
|
+
export async function hmacSha512(key, data, options) {
|
|
38
|
+
return hmac(key, 'SHA-512', data, options);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Compute the HMAC-SHA of the given data using the given key.
|
|
42
|
+
*
|
|
43
|
+
* @param key - The key to use.
|
|
44
|
+
* @param hash - The hash to use.
|
|
45
|
+
* @param data - The data to hash.
|
|
46
|
+
* @param options - Additional configuration options..
|
|
47
|
+
* @returns The HMAC-SHA of the data.
|
|
48
|
+
*/
|
|
49
|
+
async function hmac(key, hash, data, options = {}) {
|
|
50
|
+
if (key.byteLength === 0) {
|
|
51
|
+
throw new Error(`Unsafe key length: Key must not be zero bytes for HMAC-${hash}.`);
|
|
52
|
+
}
|
|
53
|
+
if (!options.unsafeKeyLength && key.byteLength < MIN_KEY_LENGTH[hash]) {
|
|
54
|
+
throw new Error(`Unsafe key length: Key must be at least ${MIN_KEY_LENGTH[hash]} bytes for HMAC-${hash}. To bypass this check, set the \`unsafeKeyLength\` option to \`true\`.`);
|
|
55
|
+
}
|
|
56
|
+
const subtleKey = await globalThis.crypto.subtle.importKey('raw', key, { name: 'HMAC', hash }, false, ['sign']);
|
|
57
|
+
const result = await globalThis.crypto.subtle.sign('HMAC', subtleKey, data);
|
|
58
|
+
return new Uint8Array(result);
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=hmac.js.map
|
package/dist/hmac.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../src/hmac.ts"],"names":[],"mappings":"AAEA,0DAA0D;AAC1D,MAAM,cAAc,GAAiC;IACnD,SAAS,EAAE,EAAE;IACb,SAAS,EAAE,EAAE;IACb,SAAS,EAAE,EAAE;CACd,CAAC;AAUF;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAiB,EACjB,IAAkB,EAClB,OAAqB;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAiB,EACjB,IAAkB,EAClB,OAAqB;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAiB,EACjB,IAAkB,EAClB,OAAqB;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;;GAQG;AACH,KAAK,UAAU,IAAI,CACjB,GAAiB,EACjB,IAAkB,EAClB,IAAkB,EAClB,OAAO,GAAgB,EAAE;IAEzB,IAAI,GAAG,CAAC,UAAU,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CACb,0DAA0D,IAAI,GAAG,CAClE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,OAAO,CAAC,eAAe,IAAI,GAAG,CAAC,UAAU,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;QACtE,MAAM,IAAI,KAAK,CACb,2CAA2C,cAAc,CAAC,IAAI,CAAC,mBAAmB,IAAI,yEAAyE,CAChK,CAAC;IACJ,CAAC;IAED,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CACxD,KAAK,EACL,GAAG,EACH,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EACtB,KAAK,EACL,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IAC5E,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import type { HashFunction } from './types.js';\n\n// https://datatracker.ietf.org/doc/html/rfc2104#section-3\nconst MIN_KEY_LENGTH: Record<HashFunction, number> = {\n 'SHA-256': 32,\n 'SHA-384': 48,\n 'SHA-512': 64,\n};\n\nexport type HmacOptions = {\n /**\n * Skip the minimum key length check. Using a key shorter than the hash\n * output length reduces the effective security strength of HMAC (RFC 2104).\n */\n unsafeKeyLength?: boolean;\n};\n\n/**\n * Compute the HMAC-SHA-256 of the given data using the given key.\n *\n * @param key - The key to use.\n * @param data - The data to hash.\n * @param options - Additional configuration options.\n * @returns The HMAC-SHA-256 of the data.\n */\nexport async function hmacSha256(\n key: BufferSource,\n data: BufferSource,\n options?: HmacOptions,\n): Promise<Uint8Array> {\n return hmac(key, 'SHA-256', data, options);\n}\n\n/**\n * Compute the HMAC-SHA-384 of the given data using the given key.\n *\n * @param key - The key to use.\n * @param data - The data to hash.\n * @param options - Additional configuration options.\n * @returns The HMAC-SHA-384 of the data.\n */\nexport async function hmacSha384(\n key: BufferSource,\n data: BufferSource,\n options?: HmacOptions,\n): Promise<Uint8Array> {\n return hmac(key, 'SHA-384', data, options);\n}\n\n/**\n * Compute the HMAC-SHA-512 of the given data using the given key.\n *\n * @param key - The key to use.\n * @param data - The data to hash.\n * @param options - Additional configuration options.\n * @returns The HMAC-SHA-512 of the data.\n */\nexport async function hmacSha512(\n key: BufferSource,\n data: BufferSource,\n options?: HmacOptions,\n): Promise<Uint8Array> {\n return hmac(key, 'SHA-512', data, options);\n}\n\n/**\n * Compute the HMAC-SHA of the given data using the given key.\n *\n * @param key - The key to use.\n * @param hash - The hash to use.\n * @param data - The data to hash.\n * @param options - Additional configuration options..\n * @returns The HMAC-SHA of the data.\n */\nasync function hmac(\n key: BufferSource,\n hash: HashFunction,\n data: BufferSource,\n options: HmacOptions = {},\n): Promise<Uint8Array> {\n if (key.byteLength === 0) {\n throw new Error(\n `Unsafe key length: Key must not be zero bytes for HMAC-${hash}.`,\n );\n }\n\n if (!options.unsafeKeyLength && key.byteLength < MIN_KEY_LENGTH[hash]) {\n throw new Error(\n `Unsafe key length: Key must be at least ${MIN_KEY_LENGTH[hash]} bytes for HMAC-${hash}. To bypass this check, set the \\`unsafeKeyLength\\` option to \\`true\\`.`,\n );\n }\n\n const subtleKey = await globalThis.crypto.subtle.importKey(\n 'raw',\n key,\n { name: 'HMAC', hash },\n false,\n ['sign'],\n );\n\n const result = await globalThis.crypto.subtle.sign('HMAC', subtleKey, data);\n return new Uint8Array(result);\n}\n"]}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,mBAAmB,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC","sourcesContent":["export * from './sha.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC","sourcesContent":["export * from './hmac.js';\nexport * from './sha.js';\nexport type * from './types.js';\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type HashFunction = 'SHA-256' | 'SHA-384' | 'SHA-512';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/cryptography",
|
|
3
|
-
"version": "0.0.0-preview-
|
|
3
|
+
"version": "0.0.0-preview-ff2ab0583",
|
|
4
4
|
"description": "Provides optimized versions of commonly used cryptography functions for MetaMask clients",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@metamask/utils": "^12.0.0",
|
|
55
55
|
"@types/jest": "^30.0.0",
|
|
56
56
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
57
|
-
"deepmerge": "^4.
|
|
57
|
+
"deepmerge": "^4.3.1",
|
|
58
58
|
"jest": "^30.4.2",
|
|
59
59
|
"rimraf": "^6.1.3",
|
|
60
60
|
"ts-jest": "^29.4.12",
|