@enbox/crypto 0.0.1
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/LICENSE +201 -0
- package/README.md +708 -0
- package/dist/browser.js +64 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +64 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/algorithms/aes-ctr.js +188 -0
- package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
- package/dist/cjs/algorithms/aes-gcm.js +196 -0
- package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
- package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
- package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/cjs/algorithms/ecdsa.js +352 -0
- package/dist/cjs/algorithms/ecdsa.js.map +1 -0
- package/dist/cjs/algorithms/eddsa.js +325 -0
- package/dist/cjs/algorithms/eddsa.js.map +1 -0
- package/dist/cjs/algorithms/sha-2.js +119 -0
- package/dist/cjs/algorithms/sha-2.js.map +1 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jose/jwe.js +3 -0
- package/dist/cjs/jose/jwe.js.map +1 -0
- package/dist/cjs/jose/jwk.js +278 -0
- package/dist/cjs/jose/jwk.js.map +1 -0
- package/dist/cjs/jose/jws.js +3 -0
- package/dist/cjs/jose/jws.js.map +1 -0
- package/dist/cjs/jose/jwt.js +3 -0
- package/dist/cjs/jose/jwt.js.map +1 -0
- package/dist/cjs/jose/utils.js +60 -0
- package/dist/cjs/jose/utils.js.map +1 -0
- package/dist/cjs/local-key-manager.js +521 -0
- package/dist/cjs/local-key-manager.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/primitives/aes-ctr.js +398 -0
- package/dist/cjs/primitives/aes-ctr.js.map +1 -0
- package/dist/cjs/primitives/aes-gcm.js +425 -0
- package/dist/cjs/primitives/aes-gcm.js.map +1 -0
- package/dist/cjs/primitives/concat-kdf.js +215 -0
- package/dist/cjs/primitives/concat-kdf.js.map +1 -0
- package/dist/cjs/primitives/ed25519.js +651 -0
- package/dist/cjs/primitives/ed25519.js.map +1 -0
- package/dist/cjs/primitives/pbkdf2.js +120 -0
- package/dist/cjs/primitives/pbkdf2.js.map +1 -0
- package/dist/cjs/primitives/secp256k1.js +958 -0
- package/dist/cjs/primitives/secp256k1.js.map +1 -0
- package/dist/cjs/primitives/secp256r1.js +959 -0
- package/dist/cjs/primitives/secp256r1.js.map +1 -0
- package/dist/cjs/primitives/sha256.js +93 -0
- package/dist/cjs/primitives/sha256.js.map +1 -0
- package/dist/cjs/primitives/x25519.js +498 -0
- package/dist/cjs/primitives/x25519.js.map +1 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/cjs/primitives/xchacha20.js +316 -0
- package/dist/cjs/primitives/xchacha20.js.map +1 -0
- package/dist/cjs/types/cipher.js +3 -0
- package/dist/cjs/types/cipher.js.map +1 -0
- package/dist/cjs/types/crypto-api.js +3 -0
- package/dist/cjs/types/crypto-api.js.map +1 -0
- package/dist/cjs/types/hasher.js +3 -0
- package/dist/cjs/types/hasher.js.map +1 -0
- package/dist/cjs/types/identifier.js +3 -0
- package/dist/cjs/types/identifier.js.map +1 -0
- package/dist/cjs/types/key-compressor.js +3 -0
- package/dist/cjs/types/key-compressor.js.map +1 -0
- package/dist/cjs/types/key-converter.js +3 -0
- package/dist/cjs/types/key-converter.js.map +1 -0
- package/dist/cjs/types/key-deriver.js +3 -0
- package/dist/cjs/types/key-deriver.js.map +1 -0
- package/dist/cjs/types/key-generator.js +3 -0
- package/dist/cjs/types/key-generator.js.map +1 -0
- package/dist/cjs/types/key-io.js +3 -0
- package/dist/cjs/types/key-io.js.map +1 -0
- package/dist/cjs/types/key-wrapper.js +3 -0
- package/dist/cjs/types/key-wrapper.js.map +1 -0
- package/dist/cjs/types/params-direct.js +3 -0
- package/dist/cjs/types/params-direct.js.map +1 -0
- package/dist/cjs/types/params-enclosed.js +3 -0
- package/dist/cjs/types/params-enclosed.js.map +1 -0
- package/dist/cjs/types/params-kms.js +3 -0
- package/dist/cjs/types/params-kms.js.map +1 -0
- package/dist/cjs/types/signer.js +3 -0
- package/dist/cjs/types/signer.js.map +1 -0
- package/dist/cjs/utils.js +173 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/algorithms/aes-ctr.js +124 -0
- package/dist/esm/algorithms/aes-ctr.js.map +1 -0
- package/dist/esm/algorithms/aes-gcm.js +132 -0
- package/dist/esm/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/algorithms/crypto-algorithm.js +6 -0
- package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +237 -0
- package/dist/esm/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/algorithms/eddsa.js +213 -0
- package/dist/esm/algorithms/eddsa.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +57 -0
- package/dist/esm/algorithms/sha-2.js.map +1 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jose/jwe.js +2 -0
- package/dist/esm/jose/jwe.js.map +1 -0
- package/dist/esm/jose/jwk.js +241 -0
- package/dist/esm/jose/jwk.js.map +1 -0
- package/dist/esm/jose/jws.js +2 -0
- package/dist/esm/jose/jws.js.map +1 -0
- package/dist/esm/jose/jwt.js +2 -0
- package/dist/esm/jose/jwt.js.map +1 -0
- package/dist/esm/jose/utils.js +34 -0
- package/dist/esm/jose/utils.js.map +1 -0
- package/dist/esm/local-key-manager.js +417 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/primitives/aes-ctr.js +327 -0
- package/dist/esm/primitives/aes-ctr.js.map +1 -0
- package/dist/esm/primitives/aes-gcm.js +347 -0
- package/dist/esm/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +185 -0
- package/dist/esm/primitives/concat-kdf.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +521 -0
- package/dist/esm/primitives/ed25519.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +78 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/primitives/secp256k1.js +805 -0
- package/dist/esm/primitives/secp256k1.js.map +1 -0
- package/dist/esm/primitives/secp256r1.js +806 -0
- package/dist/esm/primitives/secp256r1.js.map +1 -0
- package/dist/esm/primitives/sha256.js +55 -0
- package/dist/esm/primitives/sha256.js.map +1 -0
- package/dist/esm/primitives/x25519.js +392 -0
- package/dist/esm/primitives/x25519.js.map +1 -0
- package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/esm/primitives/xchacha20.js +246 -0
- package/dist/esm/primitives/xchacha20.js.map +1 -0
- package/dist/esm/types/cipher.js +2 -0
- package/dist/esm/types/cipher.js.map +1 -0
- package/dist/esm/types/crypto-api.js +2 -0
- package/dist/esm/types/crypto-api.js.map +1 -0
- package/dist/esm/types/hasher.js +2 -0
- package/dist/esm/types/hasher.js.map +1 -0
- package/dist/esm/types/identifier.js +2 -0
- package/dist/esm/types/identifier.js.map +1 -0
- package/dist/esm/types/key-compressor.js +2 -0
- package/dist/esm/types/key-compressor.js.map +1 -0
- package/dist/esm/types/key-converter.js +2 -0
- package/dist/esm/types/key-converter.js.map +1 -0
- package/dist/esm/types/key-deriver.js +2 -0
- package/dist/esm/types/key-deriver.js.map +1 -0
- package/dist/esm/types/key-generator.js +2 -0
- package/dist/esm/types/key-generator.js.map +1 -0
- package/dist/esm/types/key-io.js +2 -0
- package/dist/esm/types/key-io.js.map +1 -0
- package/dist/esm/types/key-wrapper.js +2 -0
- package/dist/esm/types/key-wrapper.js.map +1 -0
- package/dist/esm/types/params-direct.js +2 -0
- package/dist/esm/types/params-direct.js.map +1 -0
- package/dist/esm/types/params-enclosed.js +2 -0
- package/dist/esm/types/params-enclosed.js.map +1 -0
- package/dist/esm/types/params-kms.js +2 -0
- package/dist/esm/types/params-kms.js.map +1 -0
- package/dist/esm/types/signer.js +2 -0
- package/dist/esm/types/signer.js.map +1 -0
- package/dist/esm/utils.js +165 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/algorithms/aes-ctr.d.ts +121 -0
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
- package/dist/types/algorithms/aes-gcm.d.ts +152 -0
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +154 -0
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/algorithms/eddsa.d.ts +151 -0
- package/dist/types/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +51 -0
- package/dist/types/algorithms/sha-2.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/jose/jwe.d.ts +135 -0
- package/dist/types/jose/jwe.d.ts.map +1 -0
- package/dist/types/jose/jwk.d.ts +439 -0
- package/dist/types/jose/jwk.d.ts.map +1 -0
- package/dist/types/jose/jws.d.ts +67 -0
- package/dist/types/jose/jws.d.ts.map +1 -0
- package/dist/types/jose/jwt.d.ts +139 -0
- package/dist/types/jose/jwt.d.ts.map +1 -0
- package/dist/types/jose/utils.d.ts +14 -0
- package/dist/types/jose/utils.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +307 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/primitives/aes-ctr.d.ts +219 -0
- package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
- package/dist/types/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +160 -0
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
- package/dist/types/primitives/ed25519.d.ts +359 -0
- package/dist/types/primitives/ed25519.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +94 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/primitives/secp256k1.d.ts +598 -0
- package/dist/types/primitives/secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/secp256r1.d.ts +599 -0
- package/dist/types/primitives/secp256r1.d.ts.map +1 -0
- package/dist/types/primitives/sha256.d.ts +42 -0
- package/dist/types/primitives/sha256.d.ts.map +1 -0
- package/dist/types/primitives/x25519.d.ts +283 -0
- package/dist/types/primitives/x25519.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20.d.ts +186 -0
- package/dist/types/primitives/xchacha20.d.ts.map +1 -0
- package/dist/types/types/cipher.d.ts +49 -0
- package/dist/types/types/cipher.d.ts.map +1 -0
- package/dist/types/types/crypto-api.d.ts +40 -0
- package/dist/types/types/crypto-api.d.ts.map +1 -0
- package/dist/types/types/hasher.d.ts +33 -0
- package/dist/types/types/hasher.d.ts.map +1 -0
- package/dist/types/types/identifier.d.ts +16 -0
- package/dist/types/types/identifier.d.ts.map +1 -0
- package/dist/types/types/key-compressor.d.ts +28 -0
- package/dist/types/types/key-compressor.d.ts.map +1 -0
- package/dist/types/types/key-converter.d.ts +57 -0
- package/dist/types/types/key-converter.d.ts.map +1 -0
- package/dist/types/types/key-deriver.d.ts +39 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -0
- package/dist/types/types/key-generator.d.ts +105 -0
- package/dist/types/types/key-generator.d.ts.map +1 -0
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -0
- package/dist/types/types/key-wrapper.d.ts +38 -0
- package/dist/types/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/types/params-direct.d.ts +90 -0
- package/dist/types/types/params-direct.d.ts.map +1 -0
- package/dist/types/types/params-enclosed.d.ts +47 -0
- package/dist/types/types/params-enclosed.d.ts.map +1 -0
- package/dist/types/types/params-kms.d.ts +131 -0
- package/dist/types/types/params-kms.d.ts.map +1 -0
- package/dist/types/types/signer.d.ts +46 -0
- package/dist/types/types/signer.d.ts.map +1 -0
- package/dist/types/utils.d.ts +112 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +7 -0
- package/package.json +103 -0
- package/src/algorithms/aes-ctr.ts +156 -0
- package/src/algorithms/aes-gcm.ts +187 -0
- package/src/algorithms/crypto-algorithm.ts +4 -0
- package/src/algorithms/ecdsa.ts +269 -0
- package/src/algorithms/eddsa.ts +243 -0
- package/src/algorithms/sha-2.ts +65 -0
- package/src/index.ts +42 -0
- package/src/jose/jwe.ts +196 -0
- package/src/jose/jwk.ts +632 -0
- package/src/jose/jws.ts +95 -0
- package/src/jose/jwt.ts +147 -0
- package/src/jose/utils.ts +34 -0
- package/src/local-key-manager.ts +540 -0
- package/src/primitives/aes-ctr.ts +352 -0
- package/src/primitives/aes-gcm.ts +378 -0
- package/src/primitives/concat-kdf.ts +240 -0
- package/src/primitives/ed25519.ts +548 -0
- package/src/primitives/pbkdf2.ts +122 -0
- package/src/primitives/secp256k1.ts +848 -0
- package/src/primitives/secp256r1.ts +850 -0
- package/src/primitives/sha256.ts +47 -0
- package/src/primitives/x25519.ts +403 -0
- package/src/primitives/xchacha20-poly1305.ts +274 -0
- package/src/primitives/xchacha20.ts +247 -0
- package/src/types/cipher.ts +53 -0
- package/src/types/crypto-api.ts +56 -0
- package/src/types/hasher.ts +32 -0
- package/src/types/identifier.ts +16 -0
- package/src/types/key-compressor.ts +25 -0
- package/src/types/key-converter.ts +53 -0
- package/src/types/key-deriver.ts +43 -0
- package/src/types/key-generator.ts +119 -0
- package/src/types/key-io.ts +42 -0
- package/src/types/key-wrapper.ts +42 -0
- package/src/types/params-direct.ts +106 -0
- package/src/types/params-enclosed.ts +50 -0
- package/src/types/params-kms.ts +156 -0
- package/src/types/signer.ts +50 -0
- package/src/utils.ts +181 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { crypto } from '@noble/hashes/crypto';
|
|
2
|
+
import { randomBytes as nobleRandomBytes } from '@noble/hashes/utils';
|
|
3
|
+
/**
|
|
4
|
+
* A collection of cryptographic utility methods.
|
|
5
|
+
*/
|
|
6
|
+
export class CryptoUtils {
|
|
7
|
+
/**
|
|
8
|
+
* Determines the JOSE algorithm identifier of the digital signature algorithm based on the `alg` or
|
|
9
|
+
* `crv` property of a {@link Jwk | JWK}.
|
|
10
|
+
*
|
|
11
|
+
* If the `alg` property is present, its value takes precedence and is returned. Otherwise, the
|
|
12
|
+
* `crv` property is used to determine the algorithm.
|
|
13
|
+
*
|
|
14
|
+
* @memberof CryptoUtils
|
|
15
|
+
* @see {@link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms | JOSE Algorithms}
|
|
16
|
+
* @see {@link https://datatracker.ietf.org/doc/draft-ietf-jose-fully-specified-algorithms/ | Fully-Specified Algorithms for JOSE and COSE}
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const publicKey: Jwk = {
|
|
21
|
+
* "kty": "OKP",
|
|
22
|
+
* "crv": "Ed25519",
|
|
23
|
+
* "x": "FEJG7OakZi500EydXxuE8uMc8uaAzEJkmQeG8khXANw"
|
|
24
|
+
* }
|
|
25
|
+
* const algorithm = getJoseSignatureAlgorithmFromPublicKey(publicKey);
|
|
26
|
+
* console.log(algorithm); // Output: "EdDSA"
|
|
27
|
+
* ```
|
|
28
|
+
* @param publicKey - A JWK containing the `alg` and/or `crv` properties.
|
|
29
|
+
* @returns The name of the algorithm associated with the key.
|
|
30
|
+
* @throws Error if the algorithm cannot be determined from the provided input.
|
|
31
|
+
*/
|
|
32
|
+
static getJoseSignatureAlgorithmFromPublicKey(publicKey) {
|
|
33
|
+
const curveToJoseAlgorithm = {
|
|
34
|
+
'Ed25519': 'EdDSA',
|
|
35
|
+
'P-256': 'ES256',
|
|
36
|
+
'P-384': 'ES384',
|
|
37
|
+
'P-521': 'ES512',
|
|
38
|
+
'secp256k1': 'ES256K',
|
|
39
|
+
};
|
|
40
|
+
// If the key contains an `alg` property that matches a JOSE registered algorithm identifier,
|
|
41
|
+
// return its value.
|
|
42
|
+
if (publicKey.alg && Object.values(curveToJoseAlgorithm).includes(publicKey.alg)) {
|
|
43
|
+
return publicKey.alg;
|
|
44
|
+
}
|
|
45
|
+
// If the key contains a `crv` property, return the corresponding algorithm.
|
|
46
|
+
if (publicKey.crv && Object.keys(curveToJoseAlgorithm).includes(publicKey.crv)) {
|
|
47
|
+
return curveToJoseAlgorithm[publicKey.crv];
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`Unable to determine algorithm based on provided input: alg=${publicKey.alg}, crv=${publicKey.crv}. ` +
|
|
50
|
+
`Supported 'alg' values: ${Object.values(curveToJoseAlgorithm).join(', ')}. ` +
|
|
51
|
+
`Supported 'crv' values: ${Object.keys(curveToJoseAlgorithm).join(', ')}.`);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Generates secure pseudorandom values of the specified length using
|
|
55
|
+
* `crypto.getRandomValues`, which defers to the operating system.
|
|
56
|
+
*
|
|
57
|
+
* @memberof CryptoUtils
|
|
58
|
+
* @remarks
|
|
59
|
+
* This function is a wrapper around `randomBytes` from the '@noble/hashes'
|
|
60
|
+
* package. It's designed to be cryptographically strong, suitable for
|
|
61
|
+
* generating initialization vectors, nonces, and other random values.
|
|
62
|
+
*
|
|
63
|
+
* @see {@link https://www.npmjs.com/package/@noble/hashes | @noble/hashes on NPM} for more
|
|
64
|
+
* information about the underlying implementation.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const bytes = randomBytes(32); // Generates 32 random bytes
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @param bytesLength - The number of bytes to generate.
|
|
72
|
+
* @returns A Uint8Array containing the generated random bytes.
|
|
73
|
+
*/
|
|
74
|
+
static randomBytes(bytesLength) {
|
|
75
|
+
return nobleRandomBytes(bytesLength);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Generates a UUID (Universally Unique Identifier) using a
|
|
79
|
+
* cryptographically strong random number generator following
|
|
80
|
+
* the version 4 format, as specified in RFC 4122.
|
|
81
|
+
*
|
|
82
|
+
* A version 4 UUID is a randomly generated UUID. The 13th character
|
|
83
|
+
* is set to '4' to denote version 4, and the 17th character is one
|
|
84
|
+
* of '8', '9', 'A', or 'B' to comply with the variant 1 format of
|
|
85
|
+
* UUIDs (the high bits are set to '10').
|
|
86
|
+
*
|
|
87
|
+
* The UUID is a 36 character string, including hyphens, and looks like this:
|
|
88
|
+
* xxxxxxxx-xxxx-4xxx-axxx-xxxxxxxxxxxx
|
|
89
|
+
*
|
|
90
|
+
* Note that while UUIDs are not guaranteed to be unique, they are
|
|
91
|
+
* practically unique" given the large number of possible UUIDs and
|
|
92
|
+
* the randomness of generation.
|
|
93
|
+
* @memberof CryptoUtils
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const uuid = randomUuid();
|
|
97
|
+
* console.log(uuid); // Outputs a version 4 UUID, e.g., '123e4567-e89b-12d3-a456-426655440000'
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @returns A string containing a randomly generated, 36 character long v4 UUID.
|
|
101
|
+
*/
|
|
102
|
+
static randomUuid() {
|
|
103
|
+
const uuid = crypto.randomUUID();
|
|
104
|
+
return uuid;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Generates a secure random PIN (Personal Identification Number) of a
|
|
108
|
+
* specified length.
|
|
109
|
+
*
|
|
110
|
+
* This function ensures that the generated PIN is cryptographically secure and
|
|
111
|
+
* uniformly distributed by using rejection sampling. It repeatedly generates
|
|
112
|
+
* random numbers until it gets one in the desired range [0, max]. This avoids
|
|
113
|
+
* bias introduced by simply taking the modulus or truncating the number.
|
|
114
|
+
*
|
|
115
|
+
* Note: The function can generate PINs of 3 to 10 digits in length.
|
|
116
|
+
* Any request for a PIN outside this range will result in an error.
|
|
117
|
+
*
|
|
118
|
+
* Example usage:
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* const pin = randomPin({ length: 4 });
|
|
122
|
+
* console.log(pin); // Outputs a 4-digit PIN, e.g., "0231"
|
|
123
|
+
* ```
|
|
124
|
+
* @memberof CryptoUtils
|
|
125
|
+
* @param options - The options object containing the desired length of the generated PIN.
|
|
126
|
+
* @param options.length - The desired length of the generated PIN. The value should be
|
|
127
|
+
* an integer between 3 and 8 inclusive.
|
|
128
|
+
*
|
|
129
|
+
* @returns A string representing the generated PIN. The PIN will be zero-padded
|
|
130
|
+
* to match the specified length, if necessary.
|
|
131
|
+
*
|
|
132
|
+
* @throws Will throw an error if the requested PIN length is less than 3 or greater than 8.
|
|
133
|
+
*/
|
|
134
|
+
static randomPin({ length }) {
|
|
135
|
+
if (3 > length || length > 10) {
|
|
136
|
+
throw new Error('randomPin() can securely generate a PIN between 3 to 10 digits.');
|
|
137
|
+
}
|
|
138
|
+
const max = Math.pow(10, length) - 1;
|
|
139
|
+
let pin;
|
|
140
|
+
if (length <= 6) {
|
|
141
|
+
const rejectionRange = Math.pow(10, length);
|
|
142
|
+
do {
|
|
143
|
+
// Adjust the byte generation based on length.
|
|
144
|
+
const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2)); // 2 digits per byte.
|
|
145
|
+
const view = new DataView(randomBuffer.buffer);
|
|
146
|
+
// Convert the buffer to integer and take modulus based on length.
|
|
147
|
+
pin = view.getUint16(0, false) % rejectionRange;
|
|
148
|
+
} while (pin > max);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const rejectionRange = Math.pow(10, 10); // For max 10 digit number.
|
|
152
|
+
do {
|
|
153
|
+
// Generates 4 random bytes.
|
|
154
|
+
const randomBuffer = CryptoUtils.randomBytes(4);
|
|
155
|
+
// Create a DataView to read from the randomBuffer.
|
|
156
|
+
const view = new DataView(randomBuffer.buffer);
|
|
157
|
+
// Transform bytes to number (big endian).
|
|
158
|
+
pin = view.getUint32(0, false) % rejectionRange;
|
|
159
|
+
} while (pin > max); // Reject if the number is outside the desired range.
|
|
160
|
+
}
|
|
161
|
+
// Pad the PIN with leading zeros to the desired length.
|
|
162
|
+
return pin.toString().padStart(length, '0');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
import type { Cipher } from '../types/cipher.js';
|
|
3
|
+
import type { KeyGenerator } from '../types/key-generator.js';
|
|
4
|
+
import type { DecryptParams, EncryptParams, GenerateKeyParams } from '../types/params-direct.js';
|
|
5
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
6
|
+
/**
|
|
7
|
+
* The `AesCtrGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
8
|
+
* passed into the `generateKey()` method when using the AES-CTR algorithm.
|
|
9
|
+
*/
|
|
10
|
+
export interface AesCtrGenerateKeyParams extends GenerateKeyParams {
|
|
11
|
+
/** Specifies the algorithm variant for key generation in AES-CTR mode.
|
|
12
|
+
* The value determines the length of the key to be generated and must be one of the following:
|
|
13
|
+
* - `"A128CTR"`: Generates a 128-bit key.
|
|
14
|
+
* - `"A192CTR"`: Generates a 192-bit key.
|
|
15
|
+
* - `"A256CTR"`: Generates a 256-bit key.
|
|
16
|
+
*/
|
|
17
|
+
algorithm: 'A128CTR' | 'A192CTR' | 'A256CTR';
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The `AesCtrParams` interface defines the algorithm-specific parameters that should be passed
|
|
21
|
+
* into the `encrypt()` and `decrypt()` methods when using the AES-CTR algorithm.
|
|
22
|
+
*/
|
|
23
|
+
export interface AesCtrParams {
|
|
24
|
+
/** The initial value of the counter block. */
|
|
25
|
+
counter: Uint8Array;
|
|
26
|
+
/** The number of bits in the counter block that are used for the actual counter. */
|
|
27
|
+
length: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The `AesCtrAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
31
|
+
* the AES algorithm in Counter (CTR) mode. This class implements both {@link Cipher | `Cipher`} and
|
|
32
|
+
* { @link KeyGenerator | `KeyGenerator`} interfaces, providing key generation, encryption, and
|
|
33
|
+
* decryption features.
|
|
34
|
+
*
|
|
35
|
+
* This class is typically accessed through implementations that extend the
|
|
36
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
37
|
+
*/
|
|
38
|
+
export declare class AesCtrAlgorithm extends CryptoAlgorithm implements Cipher<EncryptParams & AesCtrParams, DecryptParams & AesCtrParams>, KeyGenerator<AesCtrGenerateKeyParams, Jwk> {
|
|
39
|
+
/**
|
|
40
|
+
* Decrypts the provided data using AES-CTR.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* This method performs AES-CTR decryption on the given encrypted data using the specified key.
|
|
44
|
+
* Similar to the encryption process, it requires an initial counter block and the length
|
|
45
|
+
* of the counter block, along with the encrypted data and the decryption key. The method
|
|
46
|
+
* returns the decrypted data as a Uint8Array.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
51
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
52
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block used during encryption
|
|
53
|
+
* const key = { ... }; // A Jwk object representing the same AES key used for encryption
|
|
54
|
+
* const decryptedData = await aesCtr.decrypt({
|
|
55
|
+
* data: encryptedData,
|
|
56
|
+
* counter,
|
|
57
|
+
* key,
|
|
58
|
+
* length: 128 // Length of the counter in bits
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @param params - The parameters for the decryption operation.
|
|
63
|
+
*
|
|
64
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
65
|
+
*/
|
|
66
|
+
decrypt(params: DecryptParams & AesCtrParams): Promise<Uint8Array>;
|
|
67
|
+
/**
|
|
68
|
+
* Encrypts the provided data using AES-CTR.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* This method performs AES-CTR encryption on the given data using the specified key.
|
|
72
|
+
* It requires the initial counter block and the length of the counter block, alongside
|
|
73
|
+
* the data and key. The method is designed to work asynchronously and returns the
|
|
74
|
+
* encrypted data as a Uint8Array.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
79
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
80
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
|
|
81
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
82
|
+
* const encryptedData = await aesCtr.encrypt({
|
|
83
|
+
* data,
|
|
84
|
+
* counter,
|
|
85
|
+
* key,
|
|
86
|
+
* length: 128 // Length of the counter in bits
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @param params - The parameters for the encryption operation.
|
|
91
|
+
*
|
|
92
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
93
|
+
*/
|
|
94
|
+
encrypt(params: EncryptParams & AesCtrParams): Promise<Uint8Array>;
|
|
95
|
+
/**
|
|
96
|
+
* Generates a symmetric key for AES in Counter (CTR) mode in JSON Web Key (JWK) format.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* This method generates a symmetric AES key for use in CTR mode, based on the specified
|
|
100
|
+
* `algorithm` parameter which determines the key length. It uses cryptographically secure random
|
|
101
|
+
* number generation to ensure the uniqueness and security of the key. The key is returned in JWK
|
|
102
|
+
* format.
|
|
103
|
+
*
|
|
104
|
+
* The generated key includes the following components:
|
|
105
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
106
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
107
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
112
|
+
* const privateKey = await aesCtr.generateKey({ algorithm: 'A256CTR' });
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @param params - The parameters for the key generation.
|
|
116
|
+
*
|
|
117
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
118
|
+
*/
|
|
119
|
+
generateKey({ algorithm }: AesCtrGenerateKeyParams): Promise<Jwk>;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=aes-ctr.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-ctr.d.ts","sourceRoot":"","sources":["../../../src/algorithms/aes-ctr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAGjG,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;GAGG;AACH,MAAM,WAAW,uBAAwB,SAAQ,iBAAiB;IAChE;;;;;OAKG;IACH,SAAS,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;CAC9C;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,8CAA8C;IAC9C,OAAO,EAAE,UAAU,CAAC;IAEpB,oFAAoF;IACpF,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;GAQG;AACH,qBAAa,eAAgB,SAAQ,eACnC,YAAW,MAAM,CAAC,aAAa,GAAG,YAAY,EAAE,aAAa,GAAG,YAAY,CAAC,EAClE,YAAY,CAAC,uBAAuB,EAAE,GAAG,CAAC;IAErD;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,OAAO,CAAC,MAAM,EACzB,aAAa,GAAG,YAAY,GAC3B,OAAO,CAAC,UAAU,CAAC;IAMtB;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,OAAO,CAAC,MAAM,EACzB,aAAa,GAAG,YAAY,GAC3B,OAAO,CAAC,UAAU,CAAC;IAMtB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,WAAW,CAAC,EAAE,SAAS,EAAE,EACpC,uBAAuB,GACtB,OAAO,CAAC,GAAG,CAAC;CAYhB"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
import type { Cipher } from '../types/cipher.js';
|
|
3
|
+
import type { KeyGenerator } from '../types/key-generator.js';
|
|
4
|
+
import type { DecryptParams, EncryptParams, GenerateKeyParams } from '../types/params-direct.js';
|
|
5
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
6
|
+
import { AES_GCM_TAG_LENGTHS } from '../primitives/aes-gcm.js';
|
|
7
|
+
/**
|
|
8
|
+
* The `AesGcmGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
9
|
+
* passed into the `generateKey()` method when using the AES-GCM algorithm.
|
|
10
|
+
*/
|
|
11
|
+
export interface AesGcmGenerateKeyParams extends GenerateKeyParams {
|
|
12
|
+
/** Specifies the algorithm variant for key generation in AES-GCM mode.
|
|
13
|
+
* The value determines the length of the key to be generated and must be one of the following:
|
|
14
|
+
* - `"A128GCM"`: Generates a 128-bit key.
|
|
15
|
+
* - `"A192GCM"`: Generates a 192-bit key.
|
|
16
|
+
* - `"A256GCM"`: Generates a 256-bit key.
|
|
17
|
+
*/
|
|
18
|
+
algorithm: 'A128GCM' | 'A192GCM' | 'A256GCM';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The `AesGcmParams` interface defines the algorithm-specific parameters that should be passed
|
|
22
|
+
* into the `encrypt()` and `decrypt()` methods when using the AES-GCM algorithm.
|
|
23
|
+
*/
|
|
24
|
+
export interface AesGcmParams {
|
|
25
|
+
/**
|
|
26
|
+
* The `additionalData` property is used for authentication alongside encrypted data but isn't
|
|
27
|
+
* encrypted itself. It must match in both encryption and decryption; a mismatch will cause
|
|
28
|
+
* decryption to fail. This feature allows for the authentication of data without encrypting it.
|
|
29
|
+
*
|
|
30
|
+
* The `additionalData` property is optional and omitting it does not compromise encryption
|
|
31
|
+
* security.
|
|
32
|
+
*/
|
|
33
|
+
additionalData?: Uint8Array;
|
|
34
|
+
/**
|
|
35
|
+
* The initialization vector (IV) must be unique for every encryption operation carried out with a
|
|
36
|
+
* given key. The IV need not be secret, but it must be unpredictable: that is, the IV must not be
|
|
37
|
+
* reused with the same key. The IV must be 12 bytes (96 bits) in length in accordance with the
|
|
38
|
+
* AES-GCM specification recommendedation to promote interoperability and efficiency.
|
|
39
|
+
*
|
|
40
|
+
* Note: It is OK to transmit the IV in the clear with the encrypted message.
|
|
41
|
+
*/
|
|
42
|
+
iv: Uint8Array;
|
|
43
|
+
/**
|
|
44
|
+
* This property determines the size in bits of the authentication tag generated in the encryption
|
|
45
|
+
* operation and used for authentication in the corresponding decryption. In accordance with the
|
|
46
|
+
* AES-GCM specification, the tag length must be 96, 104, 112, 120 or 128.
|
|
47
|
+
*
|
|
48
|
+
* The `tagLength` property is optional and defaults to 128 bits if omitted.
|
|
49
|
+
*/
|
|
50
|
+
tagLength?: typeof AES_GCM_TAG_LENGTHS[number];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The `AesGcmAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
54
|
+
* the AES algorithm in Galois/Counter Mode (GCM). This class implements both
|
|
55
|
+
* {@link Cipher | `Cipher`} and { @link KeyGenerator | `KeyGenerator`} interfaces, providing
|
|
56
|
+
* key generation, encryption, and decryption features.
|
|
57
|
+
*
|
|
58
|
+
* This class is typically accessed through implementations that extend the
|
|
59
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
60
|
+
*/
|
|
61
|
+
export declare class AesGcmAlgorithm extends CryptoAlgorithm implements Cipher<AesGcmParams, AesGcmParams>, KeyGenerator<AesGcmGenerateKeyParams, Jwk> {
|
|
62
|
+
/**
|
|
63
|
+
* Decrypts the provided data using AES-GCM.
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* This method performs AES-GCM decryption on the given encrypted data using the specified key.
|
|
67
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
68
|
+
* and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
|
|
69
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
70
|
+
* tag used when encrypting the data. If not specified, the default tag length of 128 bits is
|
|
71
|
+
* used.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
76
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
77
|
+
* const iv = new Uint8Array([...]); // Initialization vector used during encryption
|
|
78
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
79
|
+
* const key = { ... }; // A Jwk object representing the AES key
|
|
80
|
+
* const decryptedData = await aesGcm.decrypt({
|
|
81
|
+
* data: encryptedData,
|
|
82
|
+
* iv,
|
|
83
|
+
* additionalData,
|
|
84
|
+
* key,
|
|
85
|
+
* tagLength: 128 // Optional tag length in bits
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @param params - The parameters for the decryption operation.
|
|
90
|
+
*
|
|
91
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
92
|
+
*/
|
|
93
|
+
decrypt(params: DecryptParams & AesGcmParams): Promise<Uint8Array>;
|
|
94
|
+
/**
|
|
95
|
+
* Encrypts the provided data using AES-GCM.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* This method performs AES-GCM encryption on the given data using the specified key.
|
|
99
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
100
|
+
* and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
|
|
101
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
102
|
+
* tag generated in the encryption operation and used for authentication in the corresponding
|
|
103
|
+
* decryption. If not specified, the default tag length of 128 bits is used.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
108
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
109
|
+
* const iv = new Uint8Array([...]); // Initialization vector
|
|
110
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
111
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
112
|
+
* const encryptedData = await aesGcm.encrypt({
|
|
113
|
+
* data,
|
|
114
|
+
* iv,
|
|
115
|
+
* additionalData,
|
|
116
|
+
* key,
|
|
117
|
+
* tagLength: 128 // Optional tag length in bits
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param params - The parameters for the encryption operation.
|
|
122
|
+
*
|
|
123
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
124
|
+
*/
|
|
125
|
+
encrypt(params: EncryptParams & AesGcmParams): Promise<Uint8Array>;
|
|
126
|
+
/**
|
|
127
|
+
* Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* This method generates a symmetric AES key for use in GCM mode, based on the specified
|
|
131
|
+
* `algorithm` parameter which determines the key length. It uses cryptographically secure random
|
|
132
|
+
* number generation to ensure the uniqueness and security of the key. The key is returned in JWK
|
|
133
|
+
* format.
|
|
134
|
+
*
|
|
135
|
+
* The generated key includes the following components:
|
|
136
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
137
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
138
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
143
|
+
* const privateKey = await aesGcm.generateKey({ algorithm: 'A256GCM' });
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @param params - The parameters for the key generation.
|
|
147
|
+
*
|
|
148
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
149
|
+
*/
|
|
150
|
+
generateKey({ algorithm }: AesGcmGenerateKeyParams): Promise<Jwk>;
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=aes-gcm.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm.d.ts","sourceRoot":"","sources":["../../../src/algorithms/aes-gcm.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAEjG,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAU,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEvE;;;GAGG;AACH,MAAM,WAAW,uBAAwB,SAAQ,iBAAiB;IAChE;;;;;OAKG;IACH,SAAS,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;CAC9C;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;;;OAOG;IACH,cAAc,CAAC,EAAE,UAAU,CAAC;IAE5B;;;;;;;OAOG;IACH,EAAE,EAAE,UAAU,CAAC;IAEf;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC;CAChD;AAED;;;;;;;;GAQG;AACH,qBAAa,eAAgB,SAAQ,eACnC,YAAW,MAAM,CAAC,YAAY,EAAE,YAAY,CAAC,EAClC,YAAY,CAAC,uBAAuB,EAAE,GAAG,CAAC;IAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,OAAO,CAAC,MAAM,EACzB,aAAa,GAAG,YAAY,GAC3B,OAAO,CAAC,UAAU,CAAC;IAMtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,OAAO,CAAC,MAAM,EACzB,aAAa,GAAG,YAAY,GAC3B,OAAO,CAAC,UAAU,CAAC;IAMtB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,WAAW,CAAC,EAAE,SAAS,EAAE,EACpC,uBAAuB,GACtB,OAAO,CAAC,GAAG,CAAC;CAYhB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto-algorithm.d.ts","sourceRoot":"","sources":["../../../src/algorithms/crypto-algorithm.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,8BAAsB,eAAe;CAAG"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
import type { Signer } from '../types/signer.js';
|
|
3
|
+
import type { AsymmetricKeyGenerator } from '../types/key-generator.js';
|
|
4
|
+
import type { ComputePublicKeyParams, GenerateKeyParams, GetPublicKeyParams, SignParams, VerifyParams } from '../types/params-direct.js';
|
|
5
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
6
|
+
/**
|
|
7
|
+
* The `EcdsaGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
8
|
+
* passed into the `generateKey()` method when using the ECDSA algorithm.
|
|
9
|
+
*/
|
|
10
|
+
export interface EcdsaGenerateKeyParams extends GenerateKeyParams {
|
|
11
|
+
/**
|
|
12
|
+
* A string defining the type of key to generate. The value must be one of the following:
|
|
13
|
+
* - `"ES256"`: ECDSA using the secp256r1 (P-256) curve and SHA-256.
|
|
14
|
+
* - `"ES256K"`: ECDSA using the secp256k1 curve and SHA-256.
|
|
15
|
+
* - `"secp256k1"`: ECDSA using the secp256k1 curve and SHA-256.
|
|
16
|
+
* - `"secp256r1"`: ECDSA using the secp256r1 (P-256) curve and SHA-256.
|
|
17
|
+
*/
|
|
18
|
+
algorithm: 'ES256' | 'ES256K' | 'secp256k1' | 'secp256r1';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The `EcdsaAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
22
|
+
* the Elliptic Curve Digital Signature Algorithm (ECDSA). This class implements both
|
|
23
|
+
* {@link Signer | `Signer`} and { @link AsymmetricKeyGenerator | `AsymmetricKeyGenerator`}
|
|
24
|
+
* interfaces, providing private key generation, public key derivation, and creation/verification
|
|
25
|
+
* of signatures.
|
|
26
|
+
*
|
|
27
|
+
* This class is typically accessed through implementations that extend the
|
|
28
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
29
|
+
*/
|
|
30
|
+
export declare class EcdsaAlgorithm extends CryptoAlgorithm implements AsymmetricKeyGenerator<EcdsaGenerateKeyParams, Jwk, GetPublicKeyParams>, Signer<SignParams, VerifyParams> {
|
|
31
|
+
/**
|
|
32
|
+
* Derives the public key in JWK format from a given private key.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* This method takes a private key in JWK format and derives its corresponding public key,
|
|
36
|
+
* also in JWK format. The process ensures that the derived public key correctly corresponds to
|
|
37
|
+
* the given private key.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
42
|
+
* const privateKey = { ... }; // A Jwk object representing a private key
|
|
43
|
+
* const publicKey = await ecdsa.computePublicKey({ key: privateKey });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @param params - The parameters for the public key derivation.
|
|
47
|
+
* @param params.key - The private key in JWK format from which to derive the public key.
|
|
48
|
+
*
|
|
49
|
+
* @returns A Promise that resolves to the derived public key in JWK format.
|
|
50
|
+
*/
|
|
51
|
+
computePublicKey({ key }: ComputePublicKeyParams): Promise<Jwk>;
|
|
52
|
+
/**
|
|
53
|
+
* Generates a new private key with the specified algorithm in JSON Web Key (JWK) format.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
58
|
+
* const privateKey = await ecdsa.generateKey({ algorithm: 'ES256K' });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @param params - The parameters for key generation.
|
|
62
|
+
* @param params.algorithm - The algorithm to use for key generation.
|
|
63
|
+
*
|
|
64
|
+
* @returns A Promise that resolves to the generated private key in JWK format.
|
|
65
|
+
*/
|
|
66
|
+
generateKey({ algorithm }: EcdsaGenerateKeyParams): Promise<Jwk>;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves the public key properties from a given private key in JWK format.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* This method extracts the public key portion from an ECDSA private key in JWK format. It does
|
|
72
|
+
* so by removing the private key property 'd' and making a shallow copy, effectively yielding the
|
|
73
|
+
* public key.
|
|
74
|
+
*
|
|
75
|
+
* Note: This method offers a significant performance advantage, being about 200 times faster
|
|
76
|
+
* than `computePublicKey()`. However, it does not mathematically validate the private key, nor
|
|
77
|
+
* does it derive the public key from the private key. It simply extracts existing public key
|
|
78
|
+
* properties from the private key object. This makes it suitable for scenarios where speed is
|
|
79
|
+
* critical and the private key's integrity is already assured.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
84
|
+
* const privateKey = { ... }; // A Jwk object representing a private key
|
|
85
|
+
* const publicKey = await ecdsa.getPublicKey({ key: privateKey });
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param params - The parameters for retrieving the public key properties.
|
|
89
|
+
* @param params.key - The private key in JWK format.
|
|
90
|
+
*
|
|
91
|
+
* @returns A Promise that resolves to the public key in JWK format.
|
|
92
|
+
*/
|
|
93
|
+
getPublicKey({ key }: GetPublicKeyParams): Promise<Jwk>;
|
|
94
|
+
/**
|
|
95
|
+
* Generates an ECDSA signature of given data using a private key.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* This method uses the signature algorithm determined by the given `algorithm` to sign the
|
|
99
|
+
* provided data.
|
|
100
|
+
*
|
|
101
|
+
* The signature can later be verified by parties with access to the corresponding
|
|
102
|
+
* public key, ensuring that the data has not been tampered with and was indeed signed by the
|
|
103
|
+
* holder of the private key.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
108
|
+
* const data = new TextEncoder().encode('Message');
|
|
109
|
+
* const privateKey = { ... }; // A Jwk object representing a private key
|
|
110
|
+
* const signature = await ecdsa.sign({
|
|
111
|
+
* key: privateKey,
|
|
112
|
+
* data
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @param params - The parameters for the signing operation.
|
|
117
|
+
* @param params.key - The private key to use for signing, represented in JWK format.
|
|
118
|
+
* @param params.data - The data to sign.
|
|
119
|
+
*
|
|
120
|
+
* @returns A Promise resolving to the digital signature as a `Uint8Array`.
|
|
121
|
+
*/
|
|
122
|
+
sign({ key, data }: SignParams): Promise<Uint8Array>;
|
|
123
|
+
/**
|
|
124
|
+
* Verifies an ECDSA signature associated with the provided data using the provided key.
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* This method uses the signature algorithm determined by the `crv` property of the provided key
|
|
128
|
+
* to check the validity of a digital signature against the original data. It confirms whether the
|
|
129
|
+
* signature was created by the holder of the corresponding private key and that the data has not
|
|
130
|
+
* been tampered with.
|
|
131
|
+
*s
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
135
|
+
* const publicKey = { ... }; // Public key in JWK format corresponding to the private key that signed the data
|
|
136
|
+
* const signature = new Uint8Array([...]); // Signature to verify
|
|
137
|
+
* const data = new TextEncoder().encode('Message');
|
|
138
|
+
* const isValid = await ecdsa.verify({
|
|
139
|
+
* key: publicKey,
|
|
140
|
+
* signature,
|
|
141
|
+
* data
|
|
142
|
+
* });
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @param params - The parameters for the verification operation.
|
|
146
|
+
* @param params.key - The key to use for verification.
|
|
147
|
+
* @param params.signature - The signature to verify.
|
|
148
|
+
* @param params.data - The data to verify.
|
|
149
|
+
*
|
|
150
|
+
* @returns A Promise resolving to a boolean indicating whether the signature is valid.
|
|
151
|
+
*/
|
|
152
|
+
verify({ key, signature, data }: VerifyParams): Promise<boolean>;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=ecdsa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ecdsa.d.ts","sourceRoot":"","sources":["../../../src/algorithms/ecdsa.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACxE,OAAO,KAAK,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAIzI,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD;;;GAGG;AACH,MAAM,WAAW,sBAAuB,SAAQ,iBAAiB;IAC/D;;;;;;OAMG;IACH,SAAS,EAAE,OAAO,GAAG,QAAQ,GAAG,WAAW,GAAG,WAAW,CAAC;CAC3D;AAED;;;;;;;;;GASG;AACH,qBAAa,cAAe,SAAQ,eAClC,YAAW,sBAAsB,CAAC,sBAAsB,EAAE,GAAG,EAAE,kBAAkB,CAAC,EACvE,MAAM,CAAC,UAAU,EAAE,YAAY,CAAC;IAE3C;;;;;;;;;;;;;;;;;;;OAmBG;IACU,gBAAgB,CAAC,EAAE,GAAG,EAAE,EACnC,sBAAsB,GACrB,OAAO,CAAC,GAAG,CAAC;IAuBf;;;;;;;;;;;;;OAaG;IACU,WAAW,CAAC,EAAE,SAAS,EAAE,EACpC,sBAAsB,GACrB,OAAO,CAAC,GAAG,CAAC;IAmBf;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACU,YAAY,CAAC,EAAE,GAAG,EAAE,EAC/B,kBAAkB,GACjB,OAAO,CAAC,GAAG,CAAC;IAuBf;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACU,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,EAC7B,UAAU,GACT,OAAO,CAAC,UAAU,CAAC;IAmBtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACU,MAAM,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,EAC1C,YAAY,GACX,OAAO,CAAC,OAAO,CAAC;CAkBpB"}
|