@metamask-previews/cryptography 0.0.0-preview-2e601768f → 0.0.0-preview-91712a148
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 +0 -11
- package/dist/index.d.ts +7 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/package.json +9 -18
- package/dist/ed25519.d.ts +0 -25
- package/dist/ed25519.d.ts.map +0 -1
- package/dist/ed25519.js +0 -60
- package/dist/ed25519.js.map +0 -1
- package/dist/hkdf.d.ts +0 -41
- package/dist/hkdf.d.ts.map +0 -1
- package/dist/hkdf.js +0 -68
- package/dist/hkdf.js.map +0 -1
- package/dist/hmac.d.ts +0 -35
- package/dist/hmac.d.ts.map +0 -1
- package/dist/hmac.js +0 -60
- package/dist/hmac.js.map +0 -1
- package/dist/pbkdf2.d.ts +0 -48
- package/dist/pbkdf2.d.ts.map +0 -1
- package/dist/pbkdf2.js +0 -78
- package/dist/pbkdf2.js.map +0 -1
- package/dist/random.d.ts +0 -9
- package/dist/random.d.ts.map +0 -1
- package/dist/random.js +0 -14
- package/dist/random.js.map +0 -1
- package/dist/sha.d.ts +0 -22
- package/dist/sha.d.ts.map +0 -1
- package/dist/sha.js +0 -28
- package/dist/sha.js.map +0 -1
- package/dist/types.d.ts +0 -2
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts +0 -24
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -51
- package/dist/utils.js.map +0 -1
- package/dist/x25519.d.ts +0 -16
- package/dist/x25519.d.ts.map +0 -1
- package/dist/x25519.js +0 -51
- package/dist/x25519.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,15 +7,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
### Added
|
|
11
|
-
|
|
12
|
-
- Initial release ([#10282](https://github.com/MetaMask/core/pull/10282), [#10431](https://github.com/MetaMask/core/pull/10431), [#10403](https://github.com/MetaMask/core/pull/10403), [#10468](https://github.com/MetaMask/core/pull/10468), [#10503](https://github.com/MetaMask/core/pull/10503), [#10563](https://github.com/MetaMask/core/pull/10563), [#10506](https://github.com/MetaMask/core/pull/10506))
|
|
13
|
-
- Add `sha256`, `sha384`, and `sha512` functions for computing SHA digests
|
|
14
|
-
- Add `hmacSha256`, `hmacSha384`, and `hmacSha512` functions for computing HMAC digests
|
|
15
|
-
- Add `pbkdf2Sha256`, `pbkdf2Sha384`, and `pbkdf2Sha512` functions for key derivation
|
|
16
|
-
- Add `hkdfSha256`, `hkdfSha384`, and `hkdfSha512` functions for key derivation
|
|
17
|
-
- Add `getPublicKey` and `getSharedSecret` functions for X25519 key derivation exported via `@metamask/cryptography/x25519`
|
|
18
|
-
- Add `getRandomBytes` function for generating cryptographically secure random bytes
|
|
19
|
-
- Add `getPublicKey`, `sign`, and `verify` functions for Ed25519 exported via `@metamask/cryptography/ed25519`
|
|
20
|
-
|
|
21
10
|
[Unreleased]: https://github.com/MetaMask/core/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Example function that returns a greeting for the given name.
|
|
3
|
+
*
|
|
4
|
+
* @param name - The name to greet.
|
|
5
|
+
* @returns The greeting.
|
|
6
|
+
*/
|
|
7
|
+
export default function greeter(name: string): string;
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Example function that returns a greeting for the given name.
|
|
3
|
+
*
|
|
4
|
+
* @param name - The name to greet.
|
|
5
|
+
* @returns The greeting.
|
|
6
|
+
*/
|
|
7
|
+
export default function greeter(name) {
|
|
8
|
+
return `Hello, ${name}!`;
|
|
9
|
+
}
|
|
6
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAY;IAC1C,OAAO,UAAU,IAAI,GAAG,CAAC;AAC3B,CAAC","sourcesContent":["/**\n * Example function that returns a greeting for the given name.\n *\n * @param name - The name to greet.\n * @returns The greeting.\n */\nexport default function greeter(name: string): string {\n return `Hello, ${name}!`;\n}\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-91712a148",
|
|
4
4
|
"description": "Provides optimized versions of commonly used cryptography functions for MetaMask clients",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -25,14 +25,6 @@
|
|
|
25
25
|
"types": "./dist/index.d.ts",
|
|
26
26
|
"default": "./dist/index.js"
|
|
27
27
|
},
|
|
28
|
-
"./ed25519": {
|
|
29
|
-
"types": "./dist/ed25519.d.ts",
|
|
30
|
-
"default": "./dist/ed25519.js"
|
|
31
|
-
},
|
|
32
|
-
"./x25519": {
|
|
33
|
-
"types": "./dist/x25519.d.ts",
|
|
34
|
-
"default": "./dist/x25519.js"
|
|
35
|
-
},
|
|
36
28
|
"./package.json": "./package.json"
|
|
37
29
|
},
|
|
38
30
|
"publishConfig": {
|
|
@@ -47,8 +39,8 @@
|
|
|
47
39
|
"build:only-clean": "rimraf './dist' './tsconfig.build.tsbuildinfo'",
|
|
48
40
|
"changelog:update": "../../scripts/update-changelog.sh @metamask/cryptography",
|
|
49
41
|
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/cryptography",
|
|
50
|
-
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.
|
|
51
|
-
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.
|
|
42
|
+
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
|
|
43
|
+
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
|
|
52
44
|
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
|
|
53
45
|
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
|
|
54
46
|
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
@@ -58,15 +50,14 @@
|
|
|
58
50
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
59
51
|
},
|
|
60
52
|
"devDependencies": {
|
|
61
|
-
"@metamask/auto-changelog": "^6.
|
|
62
|
-
"@metamask/utils": "^12.0.0",
|
|
53
|
+
"@metamask/auto-changelog": "^6.1.0",
|
|
63
54
|
"@types/jest": "^30.0.0",
|
|
64
55
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
65
|
-
"deepmerge": "^4.
|
|
66
|
-
"jest": "^30.
|
|
67
|
-
"rimraf": "^
|
|
68
|
-
"ts-jest": "^29.4.
|
|
69
|
-
"tsx": "^4.
|
|
56
|
+
"deepmerge": "^4.2.2",
|
|
57
|
+
"jest": "^30.4.2",
|
|
58
|
+
"rimraf": "^5.0.5",
|
|
59
|
+
"ts-jest": "^29.4.11",
|
|
60
|
+
"tsx": "^4.20.5",
|
|
70
61
|
"typedoc": "^0.25.13",
|
|
71
62
|
"typedoc-plugin-missing-exports": "^2.0.0",
|
|
72
63
|
"typescript": "npm:@typescript/typescript6@^6.0.2"
|
package/dist/ed25519.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Derive the Ed25519 public key corresponding to the given private key.
|
|
3
|
-
*
|
|
4
|
-
* @param privateKey - The 32-byte Ed25519 private key.
|
|
5
|
-
* @returns The 32-byte Ed25519 public key.
|
|
6
|
-
*/
|
|
7
|
-
export declare function getPublicKey(privateKey: BufferSource): Promise<Uint8Array>;
|
|
8
|
-
/**
|
|
9
|
-
* Sign the given data using the given Ed25519 private key.
|
|
10
|
-
*
|
|
11
|
-
* @param privateKey - The 32-byte Ed25519 private key seed.
|
|
12
|
-
* @param data - The data to sign.
|
|
13
|
-
* @returns The 64-byte Ed25519 signature.
|
|
14
|
-
*/
|
|
15
|
-
export declare function sign(privateKey: BufferSource, data: BufferSource): Promise<Uint8Array>;
|
|
16
|
-
/**
|
|
17
|
-
* Verify an Ed25519 signature.
|
|
18
|
-
*
|
|
19
|
-
* @param publicKey - The 32-byte Ed25519 public key.
|
|
20
|
-
* @param signature - The 64-byte signature to verify.
|
|
21
|
-
* @param data - The signed data.
|
|
22
|
-
* @returns `true` if the signature is valid, `false` otherwise.
|
|
23
|
-
*/
|
|
24
|
-
export declare function verify(publicKey: BufferSource, signature: BufferSource, data: BufferSource): Promise<boolean>;
|
|
25
|
-
//# sourceMappingURL=ed25519.d.ts.map
|
package/dist/ed25519.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ed25519.d.ts","sourceRoot":"","sources":["../src/ed25519.ts"],"names":[],"mappings":"AAUA;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,YAAY,GACvB,OAAO,CAAC,UAAU,CAAC,CAiCrB;AAED;;;;;;GAMG;AACH,wBAAsB,IAAI,CACxB,UAAU,EAAE,YAAY,EACxB,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,UAAU,CAAC,CAuBrB;AAED;;;;;;;GAOG;AACH,wBAAsB,MAAM,CAC1B,SAAS,EAAE,YAAY,EACvB,SAAS,EAAE,YAAY,EACvB,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,OAAO,CAAC,CA2BlB"}
|
package/dist/ed25519.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { buildPKCS8Header, toPKCS8 } from './utils.js';
|
|
2
|
-
// https://www.rfc-editor.org/rfc/rfc8032
|
|
3
|
-
const ED25519_KEY_LENGTH = 32;
|
|
4
|
-
const ED25519_SIGNATURE_LENGTH = 64;
|
|
5
|
-
// https://www.rfc-editor.org/rfc/rfc8410#section-7
|
|
6
|
-
// https://github.com/nodejs/node/blob/main/test/parallel/test-webcrypto-export-import-cfrg.js
|
|
7
|
-
const ED25519_PKCS8_HEADER = buildPKCS8Header([0x2b, 0x65, 0x70]);
|
|
8
|
-
/**
|
|
9
|
-
* Derive the Ed25519 public key corresponding to the given private key.
|
|
10
|
-
*
|
|
11
|
-
* @param privateKey - The 32-byte Ed25519 private key.
|
|
12
|
-
* @returns The 32-byte Ed25519 public key.
|
|
13
|
-
*/
|
|
14
|
-
export async function getPublicKey(privateKey) {
|
|
15
|
-
if (privateKey.byteLength !== ED25519_KEY_LENGTH) {
|
|
16
|
-
throw new Error(`Invalid private key length: Private key must be exactly ${ED25519_KEY_LENGTH} bytes for Ed25519.`);
|
|
17
|
-
}
|
|
18
|
-
// The WebCrypto API expects private keys to be in PKCS8 format.
|
|
19
|
-
const subtlePrivateKey = await globalThis.crypto.subtle.importKey('pkcs8', toPKCS8(ED25519_PKCS8_HEADER, privateKey), { name: 'Ed25519' }, true, ['sign']);
|
|
20
|
-
const jwk = await globalThis.crypto.subtle.exportKey('jwk', subtlePrivateKey);
|
|
21
|
-
// Intentionally discarding private key from JWK (`d`).
|
|
22
|
-
const subtlePublicKey = await globalThis.crypto.subtle.importKey('jwk', { kty: jwk.kty, crv: jwk.crv, x: jwk.x }, { name: 'Ed25519' }, true, ['verify']);
|
|
23
|
-
const publicKey = await globalThis.crypto.subtle.exportKey('raw', subtlePublicKey);
|
|
24
|
-
return new Uint8Array(publicKey);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Sign the given data using the given Ed25519 private key.
|
|
28
|
-
*
|
|
29
|
-
* @param privateKey - The 32-byte Ed25519 private key seed.
|
|
30
|
-
* @param data - The data to sign.
|
|
31
|
-
* @returns The 64-byte Ed25519 signature.
|
|
32
|
-
*/
|
|
33
|
-
export async function sign(privateKey, data) {
|
|
34
|
-
if (privateKey.byteLength !== ED25519_KEY_LENGTH) {
|
|
35
|
-
throw new Error(`Invalid private key length: Private key must be exactly ${ED25519_KEY_LENGTH} bytes for Ed25519.`);
|
|
36
|
-
}
|
|
37
|
-
// The WebCrypto API expects private keys to be in PKCS8 format.
|
|
38
|
-
const subtleKey = await globalThis.crypto.subtle.importKey('pkcs8', toPKCS8(ED25519_PKCS8_HEADER, privateKey), { name: 'Ed25519' }, false, ['sign']);
|
|
39
|
-
const signature = await globalThis.crypto.subtle.sign({ name: 'Ed25519' }, subtleKey, data);
|
|
40
|
-
return new Uint8Array(signature);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Verify an Ed25519 signature.
|
|
44
|
-
*
|
|
45
|
-
* @param publicKey - The 32-byte Ed25519 public key.
|
|
46
|
-
* @param signature - The 64-byte signature to verify.
|
|
47
|
-
* @param data - The signed data.
|
|
48
|
-
* @returns `true` if the signature is valid, `false` otherwise.
|
|
49
|
-
*/
|
|
50
|
-
export async function verify(publicKey, signature, data) {
|
|
51
|
-
if (publicKey.byteLength !== ED25519_KEY_LENGTH) {
|
|
52
|
-
throw new Error(`Invalid public key length: Public key must be exactly ${ED25519_KEY_LENGTH} bytes for Ed25519.`);
|
|
53
|
-
}
|
|
54
|
-
if (signature.byteLength !== ED25519_SIGNATURE_LENGTH) {
|
|
55
|
-
throw new Error(`Invalid signature length: Signature must be exactly ${ED25519_SIGNATURE_LENGTH} bytes for Ed25519.`);
|
|
56
|
-
}
|
|
57
|
-
const subtleKey = await globalThis.crypto.subtle.importKey('raw', publicKey, { name: 'Ed25519' }, false, ['verify']);
|
|
58
|
-
return globalThis.crypto.subtle.verify({ name: 'Ed25519' }, subtleKey, signature, data);
|
|
59
|
-
}
|
|
60
|
-
//# sourceMappingURL=ed25519.js.map
|
package/dist/ed25519.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../src/ed25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEvD,yCAAyC;AACzC,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAC9B,MAAM,wBAAwB,GAAG,EAAE,CAAC;AAEpC,mDAAmD;AACnD,8FAA8F;AAC9F,MAAM,oBAAoB,GAAG,gBAAgB,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAElE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,UAAwB;IAExB,IAAI,UAAU,CAAC,UAAU,KAAK,kBAAkB,EAAE,CAAC;QACjD,MAAM,IAAI,KAAK,CACb,2DAA2D,kBAAkB,qBAAqB,CACnG,CAAC;IACJ,CAAC;IAED,gEAAgE;IAChE,MAAM,gBAAgB,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAC/D,OAAO,EACP,OAAO,CAAC,oBAAoB,EAAE,UAAU,CAAC,EACzC,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,IAAI,EACJ,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,MAAM,GAAG,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC;IAE9E,uDAAuD;IACvD,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAC9D,KAAK,EACL,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,EACxC,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,IAAI,EACJ,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CACxD,KAAK,EACL,eAAe,CAChB,CAAC;IAEF,OAAO,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,UAAwB,EACxB,IAAkB;IAElB,IAAI,UAAU,CAAC,UAAU,KAAK,kBAAkB,EAAE,CAAC;QACjD,MAAM,IAAI,KAAK,CACb,2DAA2D,kBAAkB,qBAAqB,CACnG,CAAC;IACJ,CAAC;IAED,gEAAgE;IAChE,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CACxD,OAAO,EACP,OAAO,CAAC,oBAAoB,EAAE,UAAU,CAAC,EACzC,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,KAAK,EACL,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CACnD,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,SAAS,EACT,IAAI,CACL,CAAC;IAEF,OAAO,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAC1B,SAAuB,EACvB,SAAuB,EACvB,IAAkB;IAElB,IAAI,SAAS,CAAC,UAAU,KAAK,kBAAkB,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CACb,yDAAyD,kBAAkB,qBAAqB,CACjG,CAAC;IACJ,CAAC;IAED,IAAI,SAAS,CAAC,UAAU,KAAK,wBAAwB,EAAE,CAAC;QACtD,MAAM,IAAI,KAAK,CACb,uDAAuD,wBAAwB,qBAAqB,CACrG,CAAC;IACJ,CAAC;IAED,MAAM,SAAS,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CACxD,KAAK,EACL,SAAS,EACT,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,KAAK,EACL,CAAC,QAAQ,CAAC,CACX,CAAC;IAEF,OAAO,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CACpC,EAAE,IAAI,EAAE,SAAS,EAAE,EACnB,SAAS,EACT,SAAS,EACT,IAAI,CACL,CAAC;AACJ,CAAC","sourcesContent":["import { buildPKCS8Header, toPKCS8 } from './utils.js';\n\n// https://www.rfc-editor.org/rfc/rfc8032\nconst ED25519_KEY_LENGTH = 32;\nconst ED25519_SIGNATURE_LENGTH = 64;\n\n// https://www.rfc-editor.org/rfc/rfc8410#section-7\n// https://github.com/nodejs/node/blob/main/test/parallel/test-webcrypto-export-import-cfrg.js\nconst ED25519_PKCS8_HEADER = buildPKCS8Header([0x2b, 0x65, 0x70]);\n\n/**\n * Derive the Ed25519 public key corresponding to the given private key.\n *\n * @param privateKey - The 32-byte Ed25519 private key.\n * @returns The 32-byte Ed25519 public key.\n */\nexport async function getPublicKey(\n privateKey: BufferSource,\n): Promise<Uint8Array> {\n if (privateKey.byteLength !== ED25519_KEY_LENGTH) {\n throw new Error(\n `Invalid private key length: Private key must be exactly ${ED25519_KEY_LENGTH} bytes for Ed25519.`,\n );\n }\n\n // The WebCrypto API expects private keys to be in PKCS8 format.\n const subtlePrivateKey = await globalThis.crypto.subtle.importKey(\n 'pkcs8',\n toPKCS8(ED25519_PKCS8_HEADER, privateKey),\n { name: 'Ed25519' },\n true,\n ['sign'],\n );\n\n const jwk = await globalThis.crypto.subtle.exportKey('jwk', subtlePrivateKey);\n\n // Intentionally discarding private key from JWK (`d`).\n const subtlePublicKey = await globalThis.crypto.subtle.importKey(\n 'jwk',\n { kty: jwk.kty, crv: jwk.crv, x: jwk.x },\n { name: 'Ed25519' },\n true,\n ['verify'],\n );\n\n const publicKey = await globalThis.crypto.subtle.exportKey(\n 'raw',\n subtlePublicKey,\n );\n\n return new Uint8Array(publicKey);\n}\n\n/**\n * Sign the given data using the given Ed25519 private key.\n *\n * @param privateKey - The 32-byte Ed25519 private key seed.\n * @param data - The data to sign.\n * @returns The 64-byte Ed25519 signature.\n */\nexport async function sign(\n privateKey: BufferSource,\n data: BufferSource,\n): Promise<Uint8Array> {\n if (privateKey.byteLength !== ED25519_KEY_LENGTH) {\n throw new Error(\n `Invalid private key length: Private key must be exactly ${ED25519_KEY_LENGTH} bytes for Ed25519.`,\n );\n }\n\n // The WebCrypto API expects private keys to be in PKCS8 format.\n const subtleKey = await globalThis.crypto.subtle.importKey(\n 'pkcs8',\n toPKCS8(ED25519_PKCS8_HEADER, privateKey),\n { name: 'Ed25519' },\n false,\n ['sign'],\n );\n\n const signature = await globalThis.crypto.subtle.sign(\n { name: 'Ed25519' },\n subtleKey,\n data,\n );\n\n return new Uint8Array(signature);\n}\n\n/**\n * Verify an Ed25519 signature.\n *\n * @param publicKey - The 32-byte Ed25519 public key.\n * @param signature - The 64-byte signature to verify.\n * @param data - The signed data.\n * @returns `true` if the signature is valid, `false` otherwise.\n */\nexport async function verify(\n publicKey: BufferSource,\n signature: BufferSource,\n data: BufferSource,\n): Promise<boolean> {\n if (publicKey.byteLength !== ED25519_KEY_LENGTH) {\n throw new Error(\n `Invalid public key length: Public key must be exactly ${ED25519_KEY_LENGTH} bytes for Ed25519.`,\n );\n }\n\n if (signature.byteLength !== ED25519_SIGNATURE_LENGTH) {\n throw new Error(\n `Invalid signature length: Signature must be exactly ${ED25519_SIGNATURE_LENGTH} bytes for Ed25519.`,\n );\n }\n\n const subtleKey = await globalThis.crypto.subtle.importKey(\n 'raw',\n publicKey,\n { name: 'Ed25519' },\n false,\n ['verify'],\n );\n\n return globalThis.crypto.subtle.verify(\n { name: 'Ed25519' },\n subtleKey,\n signature,\n data,\n );\n}\n"]}
|
package/dist/hkdf.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export type HkdfOptions = {
|
|
2
|
-
/**
|
|
3
|
-
* Skip the minimum IKM length check. Using IKM shorter than 32 bytes
|
|
4
|
-
* reduces the entropy of the derived key material.
|
|
5
|
-
*/
|
|
6
|
-
unsafeInputKeyingMaterial?: boolean;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Derive key material using HKDF-SHA-256.
|
|
10
|
-
*
|
|
11
|
-
* @param ikm - The input keying material.
|
|
12
|
-
* @param salt - The salt to use.
|
|
13
|
-
* @param info - Context and application-specific information.
|
|
14
|
-
* @param keyLength - The desired key length in bytes.
|
|
15
|
-
* @param options - Additional configuration options.
|
|
16
|
-
* @returns The derived key material.
|
|
17
|
-
*/
|
|
18
|
-
export declare function hkdfSha256(ikm: BufferSource, salt: BufferSource, info: BufferSource, keyLength: number, options?: HkdfOptions): Promise<Uint8Array>;
|
|
19
|
-
/**
|
|
20
|
-
* Derive key material using HKDF-SHA-384.
|
|
21
|
-
*
|
|
22
|
-
* @param ikm - The input keying material.
|
|
23
|
-
* @param salt - The salt to use.
|
|
24
|
-
* @param info - Context and application-specific information.
|
|
25
|
-
* @param keyLength - The desired key length in bytes.
|
|
26
|
-
* @param options - Additional configuration options.
|
|
27
|
-
* @returns The derived key material.
|
|
28
|
-
*/
|
|
29
|
-
export declare function hkdfSha384(ikm: BufferSource, salt: BufferSource, info: BufferSource, keyLength: number, options?: HkdfOptions): Promise<Uint8Array>;
|
|
30
|
-
/**
|
|
31
|
-
* Derive key material using HKDF-SHA-512.
|
|
32
|
-
*
|
|
33
|
-
* @param ikm - The input keying material.
|
|
34
|
-
* @param salt - The salt to use.
|
|
35
|
-
* @param info - Context and application-specific information.
|
|
36
|
-
* @param keyLength - The desired key length in bytes.
|
|
37
|
-
* @param options - Additional configuration options.
|
|
38
|
-
* @returns The derived key material.
|
|
39
|
-
*/
|
|
40
|
-
export declare function hkdfSha512(ikm: BufferSource, salt: BufferSource, info: BufferSource, keyLength: number, options?: HkdfOptions): Promise<Uint8Array>;
|
|
41
|
-
//# sourceMappingURL=hkdf.d.ts.map
|
package/dist/hkdf.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hkdf.d.ts","sourceRoot":"","sources":["../src/hkdf.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,WAAW,GAAG;IACxB;;;OAGG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;CACrC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,UAAU,CAAC,CAErB;AAED;;;;;;;;;GASG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,UAAU,CAAC,CAErB;AAED;;;;;;;;;GASG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,YAAY,EAClB,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,UAAU,CAAC,CAErB"}
|
package/dist/hkdf.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
const MIN_IKM_LENGTH = 32;
|
|
2
|
-
/**
|
|
3
|
-
* Derive key material using HKDF-SHA-256.
|
|
4
|
-
*
|
|
5
|
-
* @param ikm - The input keying material.
|
|
6
|
-
* @param salt - The salt to use.
|
|
7
|
-
* @param info - Context and application-specific information.
|
|
8
|
-
* @param keyLength - The desired key length in bytes.
|
|
9
|
-
* @param options - Additional configuration options.
|
|
10
|
-
* @returns The derived key material.
|
|
11
|
-
*/
|
|
12
|
-
export async function hkdfSha256(ikm, salt, info, keyLength, options) {
|
|
13
|
-
return hkdf(ikm, 'SHA-256', salt, info, keyLength, options);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Derive key material using HKDF-SHA-384.
|
|
17
|
-
*
|
|
18
|
-
* @param ikm - The input keying material.
|
|
19
|
-
* @param salt - The salt to use.
|
|
20
|
-
* @param info - Context and application-specific information.
|
|
21
|
-
* @param keyLength - The desired key length in bytes.
|
|
22
|
-
* @param options - Additional configuration options.
|
|
23
|
-
* @returns The derived key material.
|
|
24
|
-
*/
|
|
25
|
-
export async function hkdfSha384(ikm, salt, info, keyLength, options) {
|
|
26
|
-
return hkdf(ikm, 'SHA-384', salt, info, keyLength, options);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Derive key material using HKDF-SHA-512.
|
|
30
|
-
*
|
|
31
|
-
* @param ikm - The input keying material.
|
|
32
|
-
* @param salt - The salt to use.
|
|
33
|
-
* @param info - Context and application-specific information.
|
|
34
|
-
* @param keyLength - The desired key length in bytes.
|
|
35
|
-
* @param options - Additional configuration options.
|
|
36
|
-
* @returns The derived key material.
|
|
37
|
-
*/
|
|
38
|
-
export async function hkdfSha512(ikm, salt, info, keyLength, options) {
|
|
39
|
-
return hkdf(ikm, 'SHA-512', salt, info, keyLength, options);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Derive key material using HKDF.
|
|
43
|
-
*
|
|
44
|
-
* @param ikm - The input keying material.
|
|
45
|
-
* @param hash - The hash function to use.
|
|
46
|
-
* @param salt - The salt to use.
|
|
47
|
-
* @param info - Context and application-specific information.
|
|
48
|
-
* @param keyLength - The desired key length in bytes.
|
|
49
|
-
* @param options - Additional configuration options.
|
|
50
|
-
* @returns The derived key material.
|
|
51
|
-
*/
|
|
52
|
-
async function hkdf(ikm, hash, salt, info, keyLength, options = {}) {
|
|
53
|
-
if (ikm.byteLength === 0) {
|
|
54
|
-
throw new Error(`Unsafe input keying material length: IKM must not be zero bytes for HKDF-${hash}.`);
|
|
55
|
-
}
|
|
56
|
-
if (!options.unsafeInputKeyingMaterial && ikm.byteLength < MIN_IKM_LENGTH) {
|
|
57
|
-
throw new Error(`Unsafe input keying material length: IKM must be at least ${MIN_IKM_LENGTH} bytes for HKDF-${hash}. To bypass this check, set the \`unsafeInputKeyingMaterial\` option to \`true\`.`);
|
|
58
|
-
}
|
|
59
|
-
const subtleKey = await globalThis.crypto.subtle.importKey('raw', ikm, { name: 'HKDF' }, false, ['deriveBits']);
|
|
60
|
-
const derivedBits = await globalThis.crypto.subtle.deriveBits({
|
|
61
|
-
name: 'HKDF',
|
|
62
|
-
hash,
|
|
63
|
-
salt,
|
|
64
|
-
info,
|
|
65
|
-
}, subtleKey, keyLength * 8);
|
|
66
|
-
return new Uint8Array(derivedBits);
|
|
67
|
-
}
|
|
68
|
-
//# sourceMappingURL=hkdf.js.map
|
package/dist/hkdf.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hkdf.js","sourceRoot":"","sources":["../src/hkdf.ts"],"names":[],"mappings":"AAEA,MAAM,cAAc,GAAG,EAAE,CAAC;AAU1B;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAiB,EACjB,IAAkB,EAClB,IAAkB,EAClB,SAAiB,EACjB,OAAqB;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAiB,EACjB,IAAkB,EAClB,IAAkB,EAClB,SAAiB,EACjB,OAAqB;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAiB,EACjB,IAAkB,EAClB,IAAkB,EAClB,SAAiB,EACjB,OAAqB;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;;;;;GAUG;AACH,KAAK,UAAU,IAAI,CACjB,GAAiB,EACjB,IAAkB,EAClB,IAAkB,EAClB,IAAkB,EAClB,SAAiB,EACjB,OAAO,GAAgB,EAAE;IAEzB,IAAI,GAAG,CAAC,UAAU,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,CACb,4EAA4E,IAAI,GAAG,CACpF,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,OAAO,CAAC,yBAAyB,IAAI,GAAG,CAAC,UAAU,GAAG,cAAc,EAAE,CAAC;QAC1E,MAAM,IAAI,KAAK,CACb,6DAA6D,cAAc,mBAAmB,IAAI,mFAAmF,CACtL,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,EAChB,KAAK,EACL,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAC3D;QACE,IAAI,EAAE,MAAM;QACZ,IAAI;QACJ,IAAI;QACJ,IAAI;KACL,EACD,SAAS,EACT,SAAS,GAAG,CAAC,CACd,CAAC;IAEF,OAAO,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import type { HashFunction } from './types.js';\n\nconst MIN_IKM_LENGTH = 32;\n\nexport type HkdfOptions = {\n /**\n * Skip the minimum IKM length check. Using IKM shorter than 32 bytes\n * reduces the entropy of the derived key material.\n */\n unsafeInputKeyingMaterial?: boolean;\n};\n\n/**\n * Derive key material using HKDF-SHA-256.\n *\n * @param ikm - The input keying material.\n * @param salt - The salt to use.\n * @param info - Context and application-specific information.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The derived key material.\n */\nexport async function hkdfSha256(\n ikm: BufferSource,\n salt: BufferSource,\n info: BufferSource,\n keyLength: number,\n options?: HkdfOptions,\n): Promise<Uint8Array> {\n return hkdf(ikm, 'SHA-256', salt, info, keyLength, options);\n}\n\n/**\n * Derive key material using HKDF-SHA-384.\n *\n * @param ikm - The input keying material.\n * @param salt - The salt to use.\n * @param info - Context and application-specific information.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The derived key material.\n */\nexport async function hkdfSha384(\n ikm: BufferSource,\n salt: BufferSource,\n info: BufferSource,\n keyLength: number,\n options?: HkdfOptions,\n): Promise<Uint8Array> {\n return hkdf(ikm, 'SHA-384', salt, info, keyLength, options);\n}\n\n/**\n * Derive key material using HKDF-SHA-512.\n *\n * @param ikm - The input keying material.\n * @param salt - The salt to use.\n * @param info - Context and application-specific information.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The derived key material.\n */\nexport async function hkdfSha512(\n ikm: BufferSource,\n salt: BufferSource,\n info: BufferSource,\n keyLength: number,\n options?: HkdfOptions,\n): Promise<Uint8Array> {\n return hkdf(ikm, 'SHA-512', salt, info, keyLength, options);\n}\n\n/**\n * Derive key material using HKDF.\n *\n * @param ikm - The input keying material.\n * @param hash - The hash function to use.\n * @param salt - The salt to use.\n * @param info - Context and application-specific information.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The derived key material.\n */\nasync function hkdf(\n ikm: BufferSource,\n hash: HashFunction,\n salt: BufferSource,\n info: BufferSource,\n keyLength: number,\n options: HkdfOptions = {},\n): Promise<Uint8Array> {\n if (ikm.byteLength === 0) {\n throw new Error(\n `Unsafe input keying material length: IKM must not be zero bytes for HKDF-${hash}.`,\n );\n }\n\n if (!options.unsafeInputKeyingMaterial && ikm.byteLength < MIN_IKM_LENGTH) {\n throw new Error(\n `Unsafe input keying material length: IKM must be at least ${MIN_IKM_LENGTH} bytes for HKDF-${hash}. To bypass this check, set the \\`unsafeInputKeyingMaterial\\` option to \\`true\\`.`,\n );\n }\n\n const subtleKey = await globalThis.crypto.subtle.importKey(\n 'raw',\n ikm,\n { name: 'HKDF' },\n false,\n ['deriveBits'],\n );\n\n const derivedBits = await globalThis.crypto.subtle.deriveBits(\n {\n name: 'HKDF',\n hash,\n salt,\n info,\n },\n subtleKey,\n keyLength * 8,\n );\n\n return new Uint8Array(derivedBits);\n}\n"]}
|
package/dist/hmac.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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
|
package/dist/hmac.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
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
DELETED
|
@@ -1 +0,0 @@
|
|
|
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/pbkdf2.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options for the PBKDF2 functions.
|
|
3
|
-
*/
|
|
4
|
-
export type Pbkdf2Options = {
|
|
5
|
-
/**
|
|
6
|
-
* If `true`, the iteration count check is skipped. Disabling this check
|
|
7
|
-
* reduces the computational cost of brute-force attacks against the derived
|
|
8
|
-
* key.
|
|
9
|
-
*/
|
|
10
|
-
unsafeIterations?: boolean;
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* Compute the PBKDF2 of the given password, salt, iterations, and key length.
|
|
14
|
-
* The hash function used is SHA-256.
|
|
15
|
-
*
|
|
16
|
-
* @param password - The password to hash.
|
|
17
|
-
* @param salt - The salt to use.
|
|
18
|
-
* @param iterations - The number of iterations.
|
|
19
|
-
* @param keyLength - The desired key length in bytes.
|
|
20
|
-
* @param options - Additional configuration options.
|
|
21
|
-
* @returns The PBKDF2 of the password.
|
|
22
|
-
*/
|
|
23
|
-
export declare function pbkdf2Sha256(password: BufferSource, salt: BufferSource, iterations: number, keyLength: number, options?: Pbkdf2Options): Promise<Uint8Array>;
|
|
24
|
-
/**
|
|
25
|
-
* Compute the PBKDF2 of the given password, salt, iterations, and key length.
|
|
26
|
-
* The hash function used is SHA-384.
|
|
27
|
-
*
|
|
28
|
-
* @param password - The password to hash.
|
|
29
|
-
* @param salt - The salt to use.
|
|
30
|
-
* @param iterations - The number of iterations.
|
|
31
|
-
* @param keyLength - The desired key length in bytes.
|
|
32
|
-
* @param options - Additional configuration options.
|
|
33
|
-
* @returns The PBKDF2 of the password.
|
|
34
|
-
*/
|
|
35
|
-
export declare function pbkdf2Sha384(password: BufferSource, salt: BufferSource, iterations: number, keyLength: number, options?: Pbkdf2Options): Promise<Uint8Array>;
|
|
36
|
-
/**
|
|
37
|
-
* Compute the PBKDF2 of the given password, salt, iterations, and key length.
|
|
38
|
-
* The hash function used is SHA-512.
|
|
39
|
-
*
|
|
40
|
-
* @param password - The password to hash.
|
|
41
|
-
* @param salt - The salt to use.
|
|
42
|
-
* @param iterations - The number of iterations.
|
|
43
|
-
* @param keyLength - The desired key length in bytes.
|
|
44
|
-
* @param options - Additional configuration options.
|
|
45
|
-
* @returns The PBKDF2 of the password.
|
|
46
|
-
*/
|
|
47
|
-
export declare function pbkdf2Sha512(password: BufferSource, salt: BufferSource, iterations: number, keyLength: number, options?: Pbkdf2Options): Promise<Uint8Array>;
|
|
48
|
-
//# sourceMappingURL=pbkdf2.d.ts.map
|
package/dist/pbkdf2.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pbkdf2.d.ts","sourceRoot":"","sources":["../src/pbkdf2.ts"],"names":[],"mappings":"AAWA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,YAAY,EACtB,IAAI,EAAE,YAAY,EAClB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,UAAU,CAAC,CAErB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,YAAY,EACtB,IAAI,EAAE,YAAY,EAClB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,UAAU,CAAC,CAErB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAChC,QAAQ,EAAE,YAAY,EACtB,IAAI,EAAE,YAAY,EAClB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,UAAU,CAAC,CAErB"}
|
package/dist/pbkdf2.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
|
|
2
|
-
const MIN_ITERATIONS = {
|
|
3
|
-
'SHA-256': 600_000,
|
|
4
|
-
// SHA-384 is not explicitly listed by OWASP; using the SHA-512 value since
|
|
5
|
-
// both variants use 64-bit words internally.
|
|
6
|
-
'SHA-384': 220_000,
|
|
7
|
-
'SHA-512': 220_000,
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Compute the PBKDF2 of the given password, salt, iterations, and key length.
|
|
11
|
-
* The hash function used is SHA-256.
|
|
12
|
-
*
|
|
13
|
-
* @param password - The password to hash.
|
|
14
|
-
* @param salt - The salt to use.
|
|
15
|
-
* @param iterations - The number of iterations.
|
|
16
|
-
* @param keyLength - The desired key length in bytes.
|
|
17
|
-
* @param options - Additional configuration options.
|
|
18
|
-
* @returns The PBKDF2 of the password.
|
|
19
|
-
*/
|
|
20
|
-
export async function pbkdf2Sha256(password, salt, iterations, keyLength, options) {
|
|
21
|
-
return pbkdf2(password, salt, 'SHA-256', iterations, keyLength, options);
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Compute the PBKDF2 of the given password, salt, iterations, and key length.
|
|
25
|
-
* The hash function used is SHA-384.
|
|
26
|
-
*
|
|
27
|
-
* @param password - The password to hash.
|
|
28
|
-
* @param salt - The salt to use.
|
|
29
|
-
* @param iterations - The number of iterations.
|
|
30
|
-
* @param keyLength - The desired key length in bytes.
|
|
31
|
-
* @param options - Additional configuration options.
|
|
32
|
-
* @returns The PBKDF2 of the password.
|
|
33
|
-
*/
|
|
34
|
-
export async function pbkdf2Sha384(password, salt, iterations, keyLength, options) {
|
|
35
|
-
return pbkdf2(password, salt, 'SHA-384', iterations, keyLength, options);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Compute the PBKDF2 of the given password, salt, iterations, and key length.
|
|
39
|
-
* The hash function used is SHA-512.
|
|
40
|
-
*
|
|
41
|
-
* @param password - The password to hash.
|
|
42
|
-
* @param salt - The salt to use.
|
|
43
|
-
* @param iterations - The number of iterations.
|
|
44
|
-
* @param keyLength - The desired key length in bytes.
|
|
45
|
-
* @param options - Additional configuration options.
|
|
46
|
-
* @returns The PBKDF2 of the password.
|
|
47
|
-
*/
|
|
48
|
-
export async function pbkdf2Sha512(password, salt, iterations, keyLength, options) {
|
|
49
|
-
return pbkdf2(password, salt, 'SHA-512', iterations, keyLength, options);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Compute the PBKDF2 of the given password, salt, hash, iterations, and key length.
|
|
53
|
-
*
|
|
54
|
-
* @param password - The password to hash.
|
|
55
|
-
* @param salt - The salt to use.
|
|
56
|
-
* @param hash - The hashing function to use.
|
|
57
|
-
* @param iterations - The number of iterations.
|
|
58
|
-
* @param keyLength - The desired key length in bytes.
|
|
59
|
-
* @param options - Additional options.
|
|
60
|
-
* @returns The PBKDF2 of the password.
|
|
61
|
-
*/
|
|
62
|
-
async function pbkdf2(password, salt, hash, iterations, keyLength, options) {
|
|
63
|
-
if (!options?.unsafeIterations && iterations < MIN_ITERATIONS[hash]) {
|
|
64
|
-
throw new Error(`Unsafe number of iterations: Iterations must be at least ${MIN_ITERATIONS[hash]} for PBKDF2-${hash}. To bypass this check, set the \`unsafeIterations\` option to \`true\`.`);
|
|
65
|
-
}
|
|
66
|
-
const key = await globalThis.crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveBits']);
|
|
67
|
-
const derivedBits = await globalThis.crypto.subtle.deriveBits({
|
|
68
|
-
name: 'PBKDF2',
|
|
69
|
-
salt,
|
|
70
|
-
iterations,
|
|
71
|
-
hash,
|
|
72
|
-
}, key,
|
|
73
|
-
// `keyLength` is the number of bytes, but `deriveBits` expects the
|
|
74
|
-
// number of bits, so we multiply by 8.
|
|
75
|
-
keyLength * 8);
|
|
76
|
-
return new Uint8Array(derivedBits);
|
|
77
|
-
}
|
|
78
|
-
//# sourceMappingURL=pbkdf2.js.map
|
package/dist/pbkdf2.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pbkdf2.js","sourceRoot":"","sources":["../src/pbkdf2.ts"],"names":[],"mappings":"AAEA,mFAAmF;AACnF,MAAM,cAAc,GAAiC;IACnD,SAAS,EAAE,OAAO;IAClB,2EAA2E;IAC3E,6CAA6C;IAC7C,SAAS,EAAE,OAAO;IAClB,SAAS,EAAE,OAAO;CACnB,CAAC;AAcF;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,QAAsB,EACtB,IAAkB,EAClB,UAAkB,EAClB,SAAiB,EACjB,OAAuB;IAEvB,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC3E,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,QAAsB,EACtB,IAAkB,EAClB,UAAkB,EAClB,SAAiB,EACjB,OAAuB;IAEvB,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC3E,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,QAAsB,EACtB,IAAkB,EAClB,UAAkB,EAClB,SAAiB,EACjB,OAAuB;IAEvB,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;AAC3E,CAAC;AAED;;;;;;;;;;GAUG;AACH,KAAK,UAAU,MAAM,CACnB,QAAsB,EACtB,IAAkB,EAClB,IAAkB,EAClB,UAAkB,EAClB,SAAiB,EACjB,OAAuB;IAEvB,IAAI,CAAC,OAAO,EAAE,gBAAgB,IAAI,UAAU,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;QACpE,MAAM,IAAI,KAAK,CACb,4DAA4D,cAAc,CAAC,IAAI,CAAC,eAAe,IAAI,0EAA0E,CAC9K,CAAC;IACJ,CAAC;IAED,MAAM,GAAG,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAClD,KAAK,EACL,QAAQ,EACR,EAAE,IAAI,EAAE,QAAQ,EAAE,EAClB,KAAK,EACL,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,MAAM,WAAW,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAC3D;QACE,IAAI,EAAE,QAAQ;QACd,IAAI;QACJ,UAAU;QACV,IAAI;KACL,EACD,GAAG;IACH,mEAAmE;IACnE,uCAAuC;IACvC,SAAS,GAAG,CAAC,CACd,CAAC;IAEF,OAAO,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import type { HashFunction } from './types.js';\n\n// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html\nconst MIN_ITERATIONS: Record<HashFunction, number> = {\n 'SHA-256': 600_000,\n // SHA-384 is not explicitly listed by OWASP; using the SHA-512 value since\n // both variants use 64-bit words internally.\n 'SHA-384': 220_000,\n 'SHA-512': 220_000,\n};\n\n/**\n * Options for the PBKDF2 functions.\n */\nexport type Pbkdf2Options = {\n /**\n * If `true`, the iteration count check is skipped. Disabling this check\n * reduces the computational cost of brute-force attacks against the derived\n * key.\n */\n unsafeIterations?: boolean;\n};\n\n/**\n * Compute the PBKDF2 of the given password, salt, iterations, and key length.\n * The hash function used is SHA-256.\n *\n * @param password - The password to hash.\n * @param salt - The salt to use.\n * @param iterations - The number of iterations.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The PBKDF2 of the password.\n */\nexport async function pbkdf2Sha256(\n password: BufferSource,\n salt: BufferSource,\n iterations: number,\n keyLength: number,\n options?: Pbkdf2Options,\n): Promise<Uint8Array> {\n return pbkdf2(password, salt, 'SHA-256', iterations, keyLength, options);\n}\n\n/**\n * Compute the PBKDF2 of the given password, salt, iterations, and key length.\n * The hash function used is SHA-384.\n *\n * @param password - The password to hash.\n * @param salt - The salt to use.\n * @param iterations - The number of iterations.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The PBKDF2 of the password.\n */\nexport async function pbkdf2Sha384(\n password: BufferSource,\n salt: BufferSource,\n iterations: number,\n keyLength: number,\n options?: Pbkdf2Options,\n): Promise<Uint8Array> {\n return pbkdf2(password, salt, 'SHA-384', iterations, keyLength, options);\n}\n\n/**\n * Compute the PBKDF2 of the given password, salt, iterations, and key length.\n * The hash function used is SHA-512.\n *\n * @param password - The password to hash.\n * @param salt - The salt to use.\n * @param iterations - The number of iterations.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional configuration options.\n * @returns The PBKDF2 of the password.\n */\nexport async function pbkdf2Sha512(\n password: BufferSource,\n salt: BufferSource,\n iterations: number,\n keyLength: number,\n options?: Pbkdf2Options,\n): Promise<Uint8Array> {\n return pbkdf2(password, salt, 'SHA-512', iterations, keyLength, options);\n}\n\n/**\n * Compute the PBKDF2 of the given password, salt, hash, iterations, and key length.\n *\n * @param password - The password to hash.\n * @param salt - The salt to use.\n * @param hash - The hashing function to use.\n * @param iterations - The number of iterations.\n * @param keyLength - The desired key length in bytes.\n * @param options - Additional options.\n * @returns The PBKDF2 of the password.\n */\nasync function pbkdf2(\n password: BufferSource,\n salt: BufferSource,\n hash: HashFunction,\n iterations: number,\n keyLength: number,\n options?: Pbkdf2Options,\n): Promise<Uint8Array> {\n if (!options?.unsafeIterations && iterations < MIN_ITERATIONS[hash]) {\n throw new Error(\n `Unsafe number of iterations: Iterations must be at least ${MIN_ITERATIONS[hash]} for PBKDF2-${hash}. To bypass this check, set the \\`unsafeIterations\\` option to \\`true\\`.`,\n );\n }\n\n const key = await globalThis.crypto.subtle.importKey(\n 'raw',\n password,\n { name: 'PBKDF2' },\n false,\n ['deriveBits'],\n );\n\n const derivedBits = await globalThis.crypto.subtle.deriveBits(\n {\n name: 'PBKDF2',\n salt,\n iterations,\n hash,\n },\n key,\n // `keyLength` is the number of bytes, but `deriveBits` expects the\n // number of bits, so we multiply by 8.\n keyLength * 8,\n );\n\n return new Uint8Array(derivedBits);\n}\n"]}
|
package/dist/random.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate random bytes using the platform's cryptographically secure pseudo-random number generator (CSPRNG).
|
|
3
|
-
*
|
|
4
|
-
* @param length - The number of random bytes to generate.
|
|
5
|
-
* @returns An `Uint8Array` of the provided length with random bytes.
|
|
6
|
-
* @throws If `length` is not greater than 0.
|
|
7
|
-
*/
|
|
8
|
-
export declare function getRandomBytes(length: number): Uint8Array;
|
|
9
|
-
//# sourceMappingURL=random.d.ts.map
|
package/dist/random.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAKzD"}
|
package/dist/random.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate random bytes using the platform's cryptographically secure pseudo-random number generator (CSPRNG).
|
|
3
|
-
*
|
|
4
|
-
* @param length - The number of random bytes to generate.
|
|
5
|
-
* @returns An `Uint8Array` of the provided length with random bytes.
|
|
6
|
-
* @throws If `length` is not greater than 0.
|
|
7
|
-
*/
|
|
8
|
-
export function getRandomBytes(length) {
|
|
9
|
-
if (length <= 0) {
|
|
10
|
-
throw new Error('Invalid length: Length must be greater than 0.');
|
|
11
|
-
}
|
|
12
|
-
return globalThis.crypto.getRandomValues(new Uint8Array(length));
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=random.js.map
|
package/dist/random.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IACD,OAAO,UAAU,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;AACnE,CAAC","sourcesContent":["/**\n * Generate random bytes using the platform's cryptographically secure pseudo-random number generator (CSPRNG).\n *\n * @param length - The number of random bytes to generate.\n * @returns An `Uint8Array` of the provided length with random bytes.\n * @throws If `length` is not greater than 0.\n */\nexport function getRandomBytes(length: number): Uint8Array {\n if (length <= 0) {\n throw new Error('Invalid length: Length must be greater than 0.');\n }\n return globalThis.crypto.getRandomValues(new Uint8Array(length));\n}\n"]}
|
package/dist/sha.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Compute a SHA-256 digest for a given byte array.
|
|
3
|
-
*
|
|
4
|
-
* @param bytes - A byte array.
|
|
5
|
-
* @returns The SHA-256 hash as a byte array.
|
|
6
|
-
*/
|
|
7
|
-
export declare function sha256(bytes: BufferSource): Promise<Uint8Array>;
|
|
8
|
-
/**
|
|
9
|
-
* Compute a SHA-384 digest for a given byte array.
|
|
10
|
-
*
|
|
11
|
-
* @param bytes - A byte array.
|
|
12
|
-
* @returns The SHA-384 hash as a byte array.
|
|
13
|
-
*/
|
|
14
|
-
export declare function sha384(bytes: BufferSource): Promise<Uint8Array>;
|
|
15
|
-
/**
|
|
16
|
-
* Compute a SHA-512 digest for a given byte array.
|
|
17
|
-
*
|
|
18
|
-
* @param bytes - A byte array.
|
|
19
|
-
* @returns The SHA-512 hash as a byte array.
|
|
20
|
-
*/
|
|
21
|
-
export declare function sha512(bytes: BufferSource): Promise<Uint8Array>;
|
|
22
|
-
//# sourceMappingURL=sha.d.ts.map
|
package/dist/sha.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sha.d.ts","sourceRoot":"","sources":["../src/sha.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAIrE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAIrE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,CAIrE"}
|
package/dist/sha.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Compute a SHA-256 digest for a given byte array.
|
|
3
|
-
*
|
|
4
|
-
* @param bytes - A byte array.
|
|
5
|
-
* @returns The SHA-256 hash as a byte array.
|
|
6
|
-
*/
|
|
7
|
-
export async function sha256(bytes) {
|
|
8
|
-
return new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', bytes));
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Compute a SHA-384 digest for a given byte array.
|
|
12
|
-
*
|
|
13
|
-
* @param bytes - A byte array.
|
|
14
|
-
* @returns The SHA-384 hash as a byte array.
|
|
15
|
-
*/
|
|
16
|
-
export async function sha384(bytes) {
|
|
17
|
-
return new Uint8Array(await globalThis.crypto.subtle.digest('SHA-384', bytes));
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Compute a SHA-512 digest for a given byte array.
|
|
21
|
-
*
|
|
22
|
-
* @param bytes - A byte array.
|
|
23
|
-
* @returns The SHA-512 hash as a byte array.
|
|
24
|
-
*/
|
|
25
|
-
export async function sha512(bytes) {
|
|
26
|
-
return new Uint8Array(await globalThis.crypto.subtle.digest('SHA-512', bytes));
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=sha.js.map
|
package/dist/sha.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sha.js","sourceRoot":"","sources":["../src/sha.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,KAAmB;IAC9C,OAAO,IAAI,UAAU,CACnB,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CACxD,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,KAAmB;IAC9C,OAAO,IAAI,UAAU,CACnB,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CACxD,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,KAAmB;IAC9C,OAAO,IAAI,UAAU,CACnB,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,CAAC,CACxD,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Compute a SHA-256 digest for a given byte array.\n *\n * @param bytes - A byte array.\n * @returns The SHA-256 hash as a byte array.\n */\nexport async function sha256(bytes: BufferSource): Promise<Uint8Array> {\n return new Uint8Array(\n await globalThis.crypto.subtle.digest('SHA-256', bytes),\n );\n}\n\n/**\n * Compute a SHA-384 digest for a given byte array.\n *\n * @param bytes - A byte array.\n * @returns The SHA-384 hash as a byte array.\n */\nexport async function sha384(bytes: BufferSource): Promise<Uint8Array> {\n return new Uint8Array(\n await globalThis.crypto.subtle.digest('SHA-384', bytes),\n );\n}\n\n/**\n * Compute a SHA-512 digest for a given byte array.\n *\n * @param bytes - A byte array.\n * @returns The SHA-512 hash as a byte array.\n */\nexport async function sha512(bytes: BufferSource): Promise<Uint8Array> {\n return new Uint8Array(\n await globalThis.crypto.subtle.digest('SHA-512', bytes),\n );\n}\n"]}
|
package/dist/types.d.ts
DELETED
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
package/dist/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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/dist/utils.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Convert a `BufferSource` to a `Uint8Array` view over the same bytes.
|
|
3
|
-
*
|
|
4
|
-
* @param source - The `ArrayBuffer`, typed array, or `DataView` to convert.
|
|
5
|
-
* @returns A `Uint8Array` sharing memory with the source.
|
|
6
|
-
*/
|
|
7
|
-
export declare function toUint8Array(source: BufferSource): Uint8Array<ArrayBuffer>;
|
|
8
|
-
/**
|
|
9
|
-
* Build the 16-byte PKCS8 header for a private key.
|
|
10
|
-
* https://www.rfc-editor.org/rfc/rfc8410#section-7
|
|
11
|
-
*
|
|
12
|
-
* @param oid - The 3-byte curve OID.
|
|
13
|
-
* @returns The PKCS8 header.
|
|
14
|
-
*/
|
|
15
|
-
export declare function buildPKCS8Header(oid: [number, number, number]): Uint8Array<ArrayBuffer>;
|
|
16
|
-
/**
|
|
17
|
-
* Wrap a raw private key in a PKCS8 envelope.
|
|
18
|
-
*
|
|
19
|
-
* @param header - The algorithm-specific PKCS8 header.
|
|
20
|
-
* @param key - The raw key bytes to wrap.
|
|
21
|
-
* @returns The complete PKCS8 envelope.
|
|
22
|
-
*/
|
|
23
|
-
export declare function toPKCS8(header: Uint8Array, key: BufferSource): Uint8Array<ArrayBuffer>;
|
|
24
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,YAAY,GAAG,UAAU,CAAC,WAAW,CAAC,CAK1E;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,GAC5B,UAAU,CAAC,WAAW,CAAC,CAiBzB;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CACrB,MAAM,EAAE,UAAU,EAClB,GAAG,EAAE,YAAY,GAChB,UAAU,CAAC,WAAW,CAAC,CAKzB"}
|
package/dist/utils.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Convert a `BufferSource` to a `Uint8Array` view over the same bytes.
|
|
3
|
-
*
|
|
4
|
-
* @param source - The `ArrayBuffer`, typed array, or `DataView` to convert.
|
|
5
|
-
* @returns A `Uint8Array` sharing memory with the source.
|
|
6
|
-
*/
|
|
7
|
-
export function toUint8Array(source) {
|
|
8
|
-
if (source instanceof ArrayBuffer) {
|
|
9
|
-
return new Uint8Array(source);
|
|
10
|
-
}
|
|
11
|
-
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Build the 16-byte PKCS8 header for a private key.
|
|
15
|
-
* https://www.rfc-editor.org/rfc/rfc8410#section-7
|
|
16
|
-
*
|
|
17
|
-
* @param oid - The 3-byte curve OID.
|
|
18
|
-
* @returns The PKCS8 header.
|
|
19
|
-
*/
|
|
20
|
-
export function buildPKCS8Header(oid) {
|
|
21
|
-
return new Uint8Array([
|
|
22
|
-
0x30,
|
|
23
|
-
0x2e,
|
|
24
|
-
0x02,
|
|
25
|
-
0x01,
|
|
26
|
-
0x00,
|
|
27
|
-
0x30,
|
|
28
|
-
0x05,
|
|
29
|
-
0x06,
|
|
30
|
-
0x03,
|
|
31
|
-
...oid,
|
|
32
|
-
0x04,
|
|
33
|
-
0x22,
|
|
34
|
-
0x04,
|
|
35
|
-
0x20,
|
|
36
|
-
]);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Wrap a raw private key in a PKCS8 envelope.
|
|
40
|
-
*
|
|
41
|
-
* @param header - The algorithm-specific PKCS8 header.
|
|
42
|
-
* @param key - The raw key bytes to wrap.
|
|
43
|
-
* @returns The complete PKCS8 envelope.
|
|
44
|
-
*/
|
|
45
|
-
export function toPKCS8(header, key) {
|
|
46
|
-
const pkcs8 = new Uint8Array(header.length + key.byteLength);
|
|
47
|
-
pkcs8.set(header);
|
|
48
|
-
pkcs8.set(toUint8Array(key), header.length);
|
|
49
|
-
return pkcs8;
|
|
50
|
-
}
|
|
51
|
-
//# sourceMappingURL=utils.js.map
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,MAAoB;IAC/C,IAAI,MAAM,YAAY,WAAW,EAAE,CAAC;QAClC,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;AAC7E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAC9B,GAA6B;IAE7B,OAAO,IAAI,UAAU,CAAC;QACpB,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,GAAG,GAAG;QACN,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,IAAI;KACL,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CACrB,MAAkB,EAClB,GAAiB;IAEjB,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC;IAC7D,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAClB,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/**\n * Convert a `BufferSource` to a `Uint8Array` view over the same bytes.\n *\n * @param source - The `ArrayBuffer`, typed array, or `DataView` to convert.\n * @returns A `Uint8Array` sharing memory with the source.\n */\nexport function toUint8Array(source: BufferSource): Uint8Array<ArrayBuffer> {\n if (source instanceof ArrayBuffer) {\n return new Uint8Array(source);\n }\n return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);\n}\n\n/**\n * Build the 16-byte PKCS8 header for a private key.\n * https://www.rfc-editor.org/rfc/rfc8410#section-7\n *\n * @param oid - The 3-byte curve OID.\n * @returns The PKCS8 header.\n */\nexport function buildPKCS8Header(\n oid: [number, number, number],\n): Uint8Array<ArrayBuffer> {\n return new Uint8Array([\n 0x30,\n 0x2e,\n 0x02,\n 0x01,\n 0x00,\n 0x30,\n 0x05,\n 0x06,\n 0x03,\n ...oid,\n 0x04,\n 0x22,\n 0x04,\n 0x20,\n ]);\n}\n\n/**\n * Wrap a raw private key in a PKCS8 envelope.\n *\n * @param header - The algorithm-specific PKCS8 header.\n * @param key - The raw key bytes to wrap.\n * @returns The complete PKCS8 envelope.\n */\nexport function toPKCS8(\n header: Uint8Array,\n key: BufferSource,\n): Uint8Array<ArrayBuffer> {\n const pkcs8 = new Uint8Array(header.length + key.byteLength);\n pkcs8.set(header);\n pkcs8.set(toUint8Array(key), header.length);\n return pkcs8;\n}\n"]}
|
package/dist/x25519.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Derive the X25519 public key corresponding to a private key.
|
|
3
|
-
*
|
|
4
|
-
* @param privateKey - The 32-byte X25519 private key.
|
|
5
|
-
* @returns The 32-byte X25519 public key.
|
|
6
|
-
*/
|
|
7
|
-
export declare function getPublicKey(privateKey: BufferSource): Promise<Uint8Array>;
|
|
8
|
-
/**
|
|
9
|
-
* Compute the X25519 shared secret given a private key and a peer's public key.
|
|
10
|
-
*
|
|
11
|
-
* @param privateKey - The 32-byte X25519 private key.
|
|
12
|
-
* @param publicKey - The 32-byte X25519 public key of the peer.
|
|
13
|
-
* @returns The 32-byte shared secret.
|
|
14
|
-
*/
|
|
15
|
-
export declare function getSharedSecret(privateKey: BufferSource, publicKey: BufferSource): Promise<Uint8Array>;
|
|
16
|
-
//# sourceMappingURL=x25519.d.ts.map
|
package/dist/x25519.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"x25519.d.ts","sourceRoot":"","sources":["../src/x25519.ts"],"names":[],"mappings":"AA8DA;;;;;GAKG;AACH,wBAAsB,YAAY,CAChC,UAAU,EAAE,YAAY,GACvB,OAAO,CAAC,UAAU,CAAC,CAGrB;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CACnC,UAAU,EAAE,YAAY,EACxB,SAAS,EAAE,YAAY,GACtB,OAAO,CAAC,UAAU,CAAC,CAGrB"}
|
package/dist/x25519.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { buildPKCS8Header, toPKCS8 } from './utils.js';
|
|
2
|
-
const X25519_KEY_LENGTH = 32;
|
|
3
|
-
// https://www.rfc-editor.org/rfc/rfc7748#section-4.1
|
|
4
|
-
const X25519_BASE_POINT = new Uint8Array(32);
|
|
5
|
-
X25519_BASE_POINT[0] = 9;
|
|
6
|
-
// https://www.rfc-editor.org/rfc/rfc8410#section-7
|
|
7
|
-
// https://github.com/nodejs/node/blob/main/test/parallel/test-webcrypto-export-import-cfrg.js
|
|
8
|
-
const X25519_PKCS8_HEADER = buildPKCS8Header([0x2b, 0x65, 0x6e]);
|
|
9
|
-
/**
|
|
10
|
-
* Perform scalar multiplication of a point by a private key,
|
|
11
|
-
* specified as the X25519 function in RFC 7748, Section 5.
|
|
12
|
-
*
|
|
13
|
-
* @param privateKey - The 32-byte X25519 private key (scalar).
|
|
14
|
-
* @param publicKey - The 32-byte X25519 public key (u-coordinate).
|
|
15
|
-
* @returns The 32-byte result of the scalar multiplication.
|
|
16
|
-
*/
|
|
17
|
-
async function scalarMultiply(privateKey, publicKey) {
|
|
18
|
-
if (privateKey.byteLength !== X25519_KEY_LENGTH) {
|
|
19
|
-
throw new Error(`Invalid private key length: Private key must be exactly ${X25519_KEY_LENGTH} bytes for X25519.`);
|
|
20
|
-
}
|
|
21
|
-
if (publicKey.byteLength !== X25519_KEY_LENGTH) {
|
|
22
|
-
throw new Error(`Invalid public key length: Public key must be exactly ${X25519_KEY_LENGTH} bytes for X25519.`);
|
|
23
|
-
}
|
|
24
|
-
// The WebCrypto API expects private keys to be in PKCS8 format.
|
|
25
|
-
const subtlePrivateKey = await globalThis.crypto.subtle.importKey('pkcs8', toPKCS8(X25519_PKCS8_HEADER, privateKey), { name: 'X25519' }, false, ['deriveBits']);
|
|
26
|
-
const subtlePublicKey = await globalThis.crypto.subtle.importKey('raw', publicKey, { name: 'X25519' }, false, []);
|
|
27
|
-
const result = await globalThis.crypto.subtle.deriveBits({ name: 'X25519', public: subtlePublicKey }, subtlePrivateKey, X25519_KEY_LENGTH * 8);
|
|
28
|
-
return new Uint8Array(result);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Derive the X25519 public key corresponding to a private key.
|
|
32
|
-
*
|
|
33
|
-
* @param privateKey - The 32-byte X25519 private key.
|
|
34
|
-
* @returns The 32-byte X25519 public key.
|
|
35
|
-
*/
|
|
36
|
-
export async function getPublicKey(privateKey) {
|
|
37
|
-
// X25519 public keys are derived as X25519(k, 9)
|
|
38
|
-
return scalarMultiply(privateKey, X25519_BASE_POINT);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Compute the X25519 shared secret given a private key and a peer's public key.
|
|
42
|
-
*
|
|
43
|
-
* @param privateKey - The 32-byte X25519 private key.
|
|
44
|
-
* @param publicKey - The 32-byte X25519 public key of the peer.
|
|
45
|
-
* @returns The 32-byte shared secret.
|
|
46
|
-
*/
|
|
47
|
-
export async function getSharedSecret(privateKey, publicKey) {
|
|
48
|
-
// X25519 shared secrets are derived as X25519(a, K_b)
|
|
49
|
-
return scalarMultiply(privateKey, publicKey);
|
|
50
|
-
}
|
|
51
|
-
//# sourceMappingURL=x25519.js.map
|
package/dist/x25519.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"x25519.js","sourceRoot":"","sources":["../src/x25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEvD,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,qDAAqD;AACrD,MAAM,iBAAiB,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;AAC7C,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEzB,mDAAmD;AACnD,8FAA8F;AAC9F,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAEjE;;;;;;;GAOG;AACH,KAAK,UAAU,cAAc,CAC3B,UAAwB,EACxB,SAAuB;IAEvB,IAAI,UAAU,CAAC,UAAU,KAAK,iBAAiB,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CACb,2DAA2D,iBAAiB,oBAAoB,CACjG,CAAC;IACJ,CAAC;IAED,IAAI,SAAS,CAAC,UAAU,KAAK,iBAAiB,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CACb,yDAAyD,iBAAiB,oBAAoB,CAC/F,CAAC;IACJ,CAAC;IAED,gEAAgE;IAChE,MAAM,gBAAgB,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAC/D,OAAO,EACP,OAAO,CAAC,mBAAmB,EAAE,UAAU,CAAC,EACxC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAClB,KAAK,EACL,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAC9D,KAAK,EACL,SAAS,EACT,EAAE,IAAI,EAAE,QAAQ,EAAE,EAClB,KAAK,EACL,EAAE,CACH,CAAC;IAEF,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CACtD,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,EAC3C,gBAAgB,EAChB,iBAAiB,GAAG,CAAC,CACtB,CAAC;IAEF,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,UAAwB;IAExB,iDAAiD;IACjD,OAAO,cAAc,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,UAAwB,EACxB,SAAuB;IAEvB,sDAAsD;IACtD,OAAO,cAAc,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;AAC/C,CAAC","sourcesContent":["import { buildPKCS8Header, toPKCS8 } from './utils.js';\n\nconst X25519_KEY_LENGTH = 32;\n\n// https://www.rfc-editor.org/rfc/rfc7748#section-4.1\nconst X25519_BASE_POINT = new Uint8Array(32);\nX25519_BASE_POINT[0] = 9;\n\n// https://www.rfc-editor.org/rfc/rfc8410#section-7\n// https://github.com/nodejs/node/blob/main/test/parallel/test-webcrypto-export-import-cfrg.js\nconst X25519_PKCS8_HEADER = buildPKCS8Header([0x2b, 0x65, 0x6e]);\n\n/**\n * Perform scalar multiplication of a point by a private key,\n * specified as the X25519 function in RFC 7748, Section 5.\n *\n * @param privateKey - The 32-byte X25519 private key (scalar).\n * @param publicKey - The 32-byte X25519 public key (u-coordinate).\n * @returns The 32-byte result of the scalar multiplication.\n */\nasync function scalarMultiply(\n privateKey: BufferSource,\n publicKey: BufferSource,\n): Promise<Uint8Array> {\n if (privateKey.byteLength !== X25519_KEY_LENGTH) {\n throw new Error(\n `Invalid private key length: Private key must be exactly ${X25519_KEY_LENGTH} bytes for X25519.`,\n );\n }\n\n if (publicKey.byteLength !== X25519_KEY_LENGTH) {\n throw new Error(\n `Invalid public key length: Public key must be exactly ${X25519_KEY_LENGTH} bytes for X25519.`,\n );\n }\n\n // The WebCrypto API expects private keys to be in PKCS8 format.\n const subtlePrivateKey = await globalThis.crypto.subtle.importKey(\n 'pkcs8',\n toPKCS8(X25519_PKCS8_HEADER, privateKey),\n { name: 'X25519' },\n false,\n ['deriveBits'],\n );\n\n const subtlePublicKey = await globalThis.crypto.subtle.importKey(\n 'raw',\n publicKey,\n { name: 'X25519' },\n false,\n [],\n );\n\n const result = await globalThis.crypto.subtle.deriveBits(\n { name: 'X25519', public: subtlePublicKey },\n subtlePrivateKey,\n X25519_KEY_LENGTH * 8,\n );\n\n return new Uint8Array(result);\n}\n\n/**\n * Derive the X25519 public key corresponding to a private key.\n *\n * @param privateKey - The 32-byte X25519 private key.\n * @returns The 32-byte X25519 public key.\n */\nexport async function getPublicKey(\n privateKey: BufferSource,\n): Promise<Uint8Array> {\n // X25519 public keys are derived as X25519(k, 9)\n return scalarMultiply(privateKey, X25519_BASE_POINT);\n}\n\n/**\n * Compute the X25519 shared secret given a private key and a peer's public key.\n *\n * @param privateKey - The 32-byte X25519 private key.\n * @param publicKey - The 32-byte X25519 public key of the peer.\n * @returns The 32-byte shared secret.\n */\nexport async function getSharedSecret(\n privateKey: BufferSource,\n publicKey: BufferSource,\n): Promise<Uint8Array> {\n // X25519 shared secrets are derived as X25519(a, K_b)\n return scalarMultiply(privateKey, publicKey);\n}\n"]}
|