@enbox/crypto 0.0.2 → 0.0.4
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/README.md +34 -102
- package/dist/browser.mjs +6 -10
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/algorithms/aes-ctr.js +1 -1
- package/dist/esm/algorithms/aes-gcm.js +35 -2
- package/dist/esm/algorithms/aes-gcm.js.map +1 -1
- package/dist/esm/algorithms/aes-kw.js +154 -0
- package/dist/esm/algorithms/aes-kw.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +119 -6
- package/dist/esm/algorithms/ecdsa.js.map +1 -1
- package/dist/esm/algorithms/eddsa.js +99 -6
- package/dist/esm/algorithms/eddsa.js.map +1 -1
- package/dist/esm/algorithms/hkdf.js +53 -0
- package/dist/esm/algorithms/hkdf.js.map +1 -0
- package/dist/esm/algorithms/pbkdf2.js +55 -0
- package/dist/esm/algorithms/pbkdf2.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +2 -2
- package/dist/esm/algorithms/sha-2.js.map +1 -1
- package/dist/esm/algorithms/x25519.js +125 -0
- package/dist/esm/algorithms/x25519.js.map +1 -0
- package/dist/esm/crypto-error.js +41 -0
- package/dist/esm/crypto-error.js.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jose/jwk.js +52 -26
- package/dist/esm/jose/jwk.js.map +1 -1
- package/dist/esm/local-key-manager.js +9 -3
- package/dist/esm/local-key-manager.js.map +1 -1
- package/dist/esm/primitives/aes-ctr.js.map +1 -1
- package/dist/esm/primitives/aes-gcm.js.map +1 -1
- package/dist/esm/primitives/aes-kw.js +246 -0
- package/dist/esm/primitives/aes-kw.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +1 -1
- package/dist/esm/primitives/concat-kdf.js.map +1 -1
- package/dist/esm/primitives/ecies-secp256k1.js +79 -0
- package/dist/esm/primitives/ecies-secp256k1.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +3 -3
- package/dist/esm/primitives/ed25519.js.map +1 -1
- package/dist/esm/primitives/hkdf.js +79 -0
- package/dist/esm/primitives/hkdf.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +49 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -1
- package/dist/esm/primitives/secp256k1.js +4 -4
- package/dist/esm/primitives/secp256k1.js.map +1 -1
- package/dist/esm/primitives/secp256r1.js +4 -4
- package/dist/esm/primitives/secp256r1.js.map +1 -1
- package/dist/esm/primitives/x25519.js +10 -17
- package/dist/esm/primitives/x25519.js.map +1 -1
- package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
- package/dist/esm/primitives/xchacha20.js +1 -1
- package/dist/esm/primitives/xchacha20.js.map +1 -1
- package/dist/esm/utils.js +30 -0
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts +2 -2
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
- package/dist/types/algorithms/aes-gcm.d.ts +25 -5
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
- package/dist/types/algorithms/aes-kw.d.ts +129 -0
- package/dist/types/algorithms/aes-kw.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +49 -4
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
- package/dist/types/algorithms/eddsa.d.ts +49 -4
- package/dist/types/algorithms/eddsa.d.ts.map +1 -1
- package/dist/types/algorithms/hkdf.d.ts +35 -0
- package/dist/types/algorithms/hkdf.d.ts.map +1 -0
- package/dist/types/algorithms/pbkdf2.d.ts +35 -0
- package/dist/types/algorithms/pbkdf2.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +2 -2
- package/dist/types/algorithms/sha-2.d.ts.map +1 -1
- package/dist/types/algorithms/x25519.d.ts +76 -0
- package/dist/types/algorithms/x25519.d.ts.map +1 -0
- package/dist/types/crypto-error.d.ts +29 -0
- package/dist/types/crypto-error.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/jose/jwk.d.ts.map +1 -1
- package/dist/types/local-key-manager.d.ts +6 -6
- package/dist/types/local-key-manager.d.ts.map +1 -1
- package/dist/types/primitives/aes-kw.d.ts +103 -0
- package/dist/types/primitives/aes-kw.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +1 -1
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
- package/dist/types/primitives/ecies-secp256k1.d.ts +53 -0
- package/dist/types/primitives/ecies-secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/hkdf.d.ts +90 -0
- package/dist/types/primitives/hkdf.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +58 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
- package/dist/types/primitives/x25519.d.ts +9 -16
- package/dist/types/primitives/x25519.d.ts.map +1 -1
- package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
- package/dist/types/types/cipher.d.ts +1 -1
- package/dist/types/types/crypto-api.d.ts +54 -6
- package/dist/types/types/crypto-api.d.ts.map +1 -1
- package/dist/types/types/key-converter.d.ts +37 -15
- package/dist/types/types/key-converter.d.ts.map +1 -1
- package/dist/types/types/key-deriver.d.ts +41 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -1
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -1
- package/dist/types/types/params-direct.d.ts +96 -1
- package/dist/types/types/params-direct.d.ts.map +1 -1
- package/dist/types/types/params-kms.d.ts +55 -0
- package/dist/types/types/params-kms.d.ts.map +1 -1
- package/dist/types/utils.d.ts +19 -0
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +4 -4
- package/package.json +29 -45
- package/src/algorithms/aes-ctr.ts +2 -2
- package/src/algorithms/aes-gcm.ts +41 -4
- package/src/algorithms/aes-kw.ts +182 -0
- package/src/algorithms/ecdsa.ts +145 -8
- package/src/algorithms/eddsa.ts +117 -10
- package/src/algorithms/hkdf.ts +54 -0
- package/src/algorithms/pbkdf2.ts +57 -0
- package/src/algorithms/sha-2.ts +3 -3
- package/src/algorithms/x25519.ts +153 -0
- package/src/crypto-error.ts +45 -0
- package/src/index.ts +8 -0
- package/src/jose/jwk.ts +32 -32
- package/src/local-key-manager.ts +22 -16
- package/src/primitives/aes-ctr.ts +1 -1
- package/src/primitives/aes-gcm.ts +5 -5
- package/src/primitives/aes-kw.ts +269 -0
- package/src/primitives/concat-kdf.ts +4 -2
- package/src/primitives/ecies-secp256k1.ts +113 -0
- package/src/primitives/ed25519.ts +6 -6
- package/src/primitives/hkdf.ts +121 -0
- package/src/primitives/pbkdf2.ts +91 -0
- package/src/primitives/secp256k1.ts +6 -6
- package/src/primitives/secp256r1.ts +6 -6
- package/src/primitives/x25519.ts +12 -19
- package/src/primitives/xchacha20-poly1305.ts +57 -4
- package/src/primitives/xchacha20.ts +1 -1
- package/src/types/cipher.ts +1 -1
- package/src/types/crypto-api.ts +129 -11
- package/src/types/key-converter.ts +33 -7
- package/src/types/key-deriver.ts +49 -0
- package/src/types/key-io.ts +40 -0
- package/src/types/params-direct.ts +118 -1
- package/src/types/params-kms.ts +67 -0
- package/src/utils.ts +55 -2
- package/dist/browser.js +0 -64
- package/dist/browser.js.map +0 -7
- package/dist/cjs/algorithms/aes-ctr.js +0 -188
- package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
- package/dist/cjs/algorithms/aes-gcm.js +0 -196
- package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
- package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
- package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
- package/dist/cjs/algorithms/ecdsa.js +0 -352
- package/dist/cjs/algorithms/ecdsa.js.map +0 -1
- package/dist/cjs/algorithms/eddsa.js +0 -325
- package/dist/cjs/algorithms/eddsa.js.map +0 -1
- package/dist/cjs/algorithms/sha-2.js +0 -119
- package/dist/cjs/algorithms/sha-2.js.map +0 -1
- package/dist/cjs/index.js +0 -41
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/jose/jwe.js +0 -3
- package/dist/cjs/jose/jwe.js.map +0 -1
- package/dist/cjs/jose/jwk.js +0 -278
- package/dist/cjs/jose/jwk.js.map +0 -1
- package/dist/cjs/jose/jws.js +0 -3
- package/dist/cjs/jose/jws.js.map +0 -1
- package/dist/cjs/jose/jwt.js +0 -3
- package/dist/cjs/jose/jwt.js.map +0 -1
- package/dist/cjs/jose/utils.js +0 -60
- package/dist/cjs/jose/utils.js.map +0 -1
- package/dist/cjs/local-key-manager.js +0 -521
- package/dist/cjs/local-key-manager.js.map +0 -1
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/primitives/aes-ctr.js +0 -398
- package/dist/cjs/primitives/aes-ctr.js.map +0 -1
- package/dist/cjs/primitives/aes-gcm.js +0 -425
- package/dist/cjs/primitives/aes-gcm.js.map +0 -1
- package/dist/cjs/primitives/concat-kdf.js +0 -215
- package/dist/cjs/primitives/concat-kdf.js.map +0 -1
- package/dist/cjs/primitives/ed25519.js +0 -651
- package/dist/cjs/primitives/ed25519.js.map +0 -1
- package/dist/cjs/primitives/pbkdf2.js +0 -120
- package/dist/cjs/primitives/pbkdf2.js.map +0 -1
- package/dist/cjs/primitives/secp256k1.js +0 -958
- package/dist/cjs/primitives/secp256k1.js.map +0 -1
- package/dist/cjs/primitives/secp256r1.js +0 -959
- package/dist/cjs/primitives/secp256r1.js.map +0 -1
- package/dist/cjs/primitives/sha256.js +0 -93
- package/dist/cjs/primitives/sha256.js.map +0 -1
- package/dist/cjs/primitives/x25519.js +0 -498
- package/dist/cjs/primitives/x25519.js.map +0 -1
- package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
- package/dist/cjs/primitives/xchacha20.js +0 -316
- package/dist/cjs/primitives/xchacha20.js.map +0 -1
- package/dist/cjs/types/cipher.js +0 -3
- package/dist/cjs/types/cipher.js.map +0 -1
- package/dist/cjs/types/crypto-api.js +0 -3
- package/dist/cjs/types/crypto-api.js.map +0 -1
- package/dist/cjs/types/hasher.js +0 -3
- package/dist/cjs/types/hasher.js.map +0 -1
- package/dist/cjs/types/identifier.js +0 -3
- package/dist/cjs/types/identifier.js.map +0 -1
- package/dist/cjs/types/key-compressor.js +0 -3
- package/dist/cjs/types/key-compressor.js.map +0 -1
- package/dist/cjs/types/key-converter.js +0 -3
- package/dist/cjs/types/key-converter.js.map +0 -1
- package/dist/cjs/types/key-deriver.js +0 -3
- package/dist/cjs/types/key-deriver.js.map +0 -1
- package/dist/cjs/types/key-generator.js +0 -3
- package/dist/cjs/types/key-generator.js.map +0 -1
- package/dist/cjs/types/key-io.js +0 -3
- package/dist/cjs/types/key-io.js.map +0 -1
- package/dist/cjs/types/key-wrapper.js +0 -3
- package/dist/cjs/types/key-wrapper.js.map +0 -1
- package/dist/cjs/types/params-direct.js +0 -3
- package/dist/cjs/types/params-direct.js.map +0 -1
- package/dist/cjs/types/params-enclosed.js +0 -3
- package/dist/cjs/types/params-enclosed.js.map +0 -1
- package/dist/cjs/types/params-kms.js +0 -3
- package/dist/cjs/types/params-kms.js.map +0 -1
- package/dist/cjs/types/signer.js +0 -3
- package/dist/cjs/types/signer.js.map +0 -1
- package/dist/cjs/utils.js +0 -173
- package/dist/cjs/utils.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-io.d.ts","sourceRoot":"","sources":["../../../src/types/key-io.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;;;;GAOG;AACH,MAAM,WAAW,mBAAmB,CAClC,cAAc,EACd,eAAe,EACf,cAAc;IAEd;;;;;;;;;;OAUG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAEhD;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC7D"}
|
|
1
|
+
{"version":3,"file":"key-io.d.ts","sourceRoot":"","sources":["../../../src/types/key-io.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;;;;GAOG;AACH,MAAM,WAAW,mBAAmB,CAClC,cAAc,EACd,eAAe,EACf,cAAc;IAEd;;;;;;;;;;OAUG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAEhD;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC7D;AAED;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,cAAc,EAAE,eAAe,GAAG,GAAG;IAChE;;;;;;OAMG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC7D;AAED;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,cAAc,EAAE,eAAe,GAAG,IAAI;IACjE;;;;;;OAMG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC7D;AAED;;GAEG;AACH,MAAM,WAAW,UAAU,CAAC,cAAc;IACxC;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClD"}
|
|
@@ -1,10 +1,41 @@
|
|
|
1
|
-
import type { Jwk } from '../jose/jwk.js';
|
|
2
1
|
import type { AlgorithmIdentifier } from './identifier.js';
|
|
2
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
3
|
+
/**
|
|
4
|
+
* Parameters for converting raw private key bytes to a JWK.
|
|
5
|
+
*/
|
|
6
|
+
export interface BytesToPrivateKeyParams {
|
|
7
|
+
/** The algorithm identifier. */
|
|
8
|
+
algorithm: AlgorithmIdentifier;
|
|
9
|
+
/** The raw private key bytes. */
|
|
10
|
+
privateKeyBytes: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parameters for converting raw public key bytes to a JWK.
|
|
14
|
+
*/
|
|
15
|
+
export interface BytesToPublicKeyParams {
|
|
16
|
+
/** The algorithm identifier. */
|
|
17
|
+
algorithm: AlgorithmIdentifier;
|
|
18
|
+
/** The raw public key bytes. */
|
|
19
|
+
publicKeyBytes: Uint8Array;
|
|
20
|
+
}
|
|
3
21
|
/**
|
|
4
22
|
* Parameters for computing a public key.
|
|
5
23
|
*/
|
|
6
24
|
export interface ComputePublicKeyParams extends GetPublicKeyParams {
|
|
7
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Parameters for encryption and decryption operations.
|
|
28
|
+
*
|
|
29
|
+
* Intended for use with a Key Management System.
|
|
30
|
+
*/
|
|
31
|
+
export interface CipherParams {
|
|
32
|
+
/** A {@link Jwk} containing the key to be used for encryption or decryption. */
|
|
33
|
+
key: Jwk;
|
|
34
|
+
/** Data to be encrypted or decrypted. */
|
|
35
|
+
data: Uint8Array;
|
|
36
|
+
/** Additional algorithm-specific parameters for encryption or decryption. */
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
8
39
|
/**
|
|
9
40
|
* Parameters for decrypting data.
|
|
10
41
|
*/
|
|
@@ -35,6 +66,32 @@ export interface DeriveKeyParams {
|
|
|
35
66
|
/** An object defining the algorithm-specific parameters for the derived key. */
|
|
36
67
|
derivedKeyParams: unknown;
|
|
37
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Parameters for deriving a key from raw byte-based key material.
|
|
71
|
+
*
|
|
72
|
+
* Unlike {@link DeriveKeyParams} which operates on JWK keys, this interface works with raw
|
|
73
|
+
* byte arrays as the base key input, making it suitable for agent-level key derivation where
|
|
74
|
+
* keys originate from passphrases, seed phrases, or other byte-oriented sources.
|
|
75
|
+
*/
|
|
76
|
+
export interface DeriveKeyFromBytesParams {
|
|
77
|
+
/** The algorithm identifier. */
|
|
78
|
+
algorithm: string;
|
|
79
|
+
/** The base key to be used for derivation as a byte array. */
|
|
80
|
+
baseKeyBytes: Uint8Array;
|
|
81
|
+
/** The algorithm identifier for the derived key. */
|
|
82
|
+
derivedKeyAlgorithm?: string;
|
|
83
|
+
/** Additional algorithm-specific parameters for key derivation. */
|
|
84
|
+
[key: string]: unknown;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Parameters for derivation of cryptographic byte arrays.
|
|
88
|
+
*/
|
|
89
|
+
export interface DeriveKeyBytesParams {
|
|
90
|
+
/** The base key to be used for derivation as a byte array. */
|
|
91
|
+
baseKeyBytes: Uint8Array;
|
|
92
|
+
/** The desired length of the derived key in bits. */
|
|
93
|
+
length: number;
|
|
94
|
+
}
|
|
38
95
|
/**
|
|
39
96
|
* Parameters for computing a hash digest.
|
|
40
97
|
*/
|
|
@@ -76,6 +133,33 @@ export interface SignParams {
|
|
|
76
133
|
/** Data to be signed. */
|
|
77
134
|
data: Uint8Array;
|
|
78
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Parameters for converting a private key JWK to raw bytes.
|
|
138
|
+
*/
|
|
139
|
+
export interface PrivateKeyToBytesParams {
|
|
140
|
+
/** The private key in JWK format. */
|
|
141
|
+
privateKey: Jwk;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Parameters for converting a public key JWK to raw bytes.
|
|
145
|
+
*/
|
|
146
|
+
export interface PublicKeyToBytesParams {
|
|
147
|
+
/** The public key in JWK format. */
|
|
148
|
+
publicKey: Jwk;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Parameters for unwrapping a key.
|
|
152
|
+
*/
|
|
153
|
+
export interface UnwrapKeyParams {
|
|
154
|
+
/** A {@link Jwk} containing the key used to decrypt the unwrapped key. */
|
|
155
|
+
decryptionKey: Jwk;
|
|
156
|
+
/** The wrapped private key as a byte array. */
|
|
157
|
+
wrappedKeyBytes: Uint8Array;
|
|
158
|
+
/** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
|
|
159
|
+
wrappedKeyAlgorithm: string;
|
|
160
|
+
/** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
|
|
161
|
+
decryptParams?: unknown;
|
|
162
|
+
}
|
|
79
163
|
/**
|
|
80
164
|
* Parameters for verifying a signature.
|
|
81
165
|
*/
|
|
@@ -87,4 +171,15 @@ export interface VerifyParams {
|
|
|
87
171
|
/** The data associated with the signature. */
|
|
88
172
|
data: Uint8Array;
|
|
89
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Parameters for wrapping a key.
|
|
176
|
+
*/
|
|
177
|
+
export interface WrapKeyParams {
|
|
178
|
+
/** A {@link Jwk} containing the key used to encrypt the unwrapped key. */
|
|
179
|
+
encryptionKey: Jwk;
|
|
180
|
+
/** A {@link Jwk} containing the private key to be wrapped. */
|
|
181
|
+
unwrappedKey: Jwk;
|
|
182
|
+
/** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
|
|
183
|
+
encryptParams?: unknown;
|
|
184
|
+
}
|
|
90
185
|
//# sourceMappingURL=params-direct.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"params-direct.d.ts","sourceRoot":"","sources":["../../../src/types/params-direct.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"params-direct.d.ts","sourceRoot":"","sources":["../../../src/types/params-direct.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,g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AAW,aAAa;IAC5B,0EAA0E;IAC1E,aAAa,EAAE,GAAG,CAAC;IAEnB,8DAA8D;IAC9D,YAAY,EAAE,GAAG,CAAC;IAElB,8FAA8F;IAC9F,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB"}
|
|
@@ -128,4 +128,59 @@ export interface KmsUnwrapKeyParams {
|
|
|
128
128
|
/** Algorithm to be used for unwrapping. */
|
|
129
129
|
unwrapAlgorithm: AlgorithmIdentifier;
|
|
130
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Parameters for KMS-based encryption and decryption operations.
|
|
133
|
+
*
|
|
134
|
+
* Intended for use with a Key Management System where the key is referenced by URI.
|
|
135
|
+
*/
|
|
136
|
+
export interface KmsCipherParams {
|
|
137
|
+
/** Identifier for the private key in the KMS. */
|
|
138
|
+
keyUri: KeyIdentifier;
|
|
139
|
+
/** Data to be encrypted or decrypted. */
|
|
140
|
+
data: Uint8Array;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Parameters for KMS-based derivation of a byte array from a given base key.
|
|
144
|
+
*
|
|
145
|
+
* Intended for use with a Key Management System.
|
|
146
|
+
*/
|
|
147
|
+
export interface KmsDeriveKeyBytesParams {
|
|
148
|
+
/** Identifier for the base key used in derivation in the KMS. */
|
|
149
|
+
baseKeyUri: KeyIdentifier;
|
|
150
|
+
/** The desired length of the derived key in bits. */
|
|
151
|
+
length: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Parameters for KMS-based key unwrapping. Intended for use with a Key Management System where
|
|
155
|
+
* the decryption key is referenced by URI.
|
|
156
|
+
*/
|
|
157
|
+
export interface KmsUriUnwrapKeyParams {
|
|
158
|
+
/** Identifier for the private key in the KMS used for decrypting the wrapped key. */
|
|
159
|
+
decryptionKeyUri: KeyIdentifier;
|
|
160
|
+
/** The wrapped private key as a byte array. */
|
|
161
|
+
wrappedKeyBytes: Uint8Array;
|
|
162
|
+
/** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
|
|
163
|
+
wrappedKeyAlgorithm: string;
|
|
164
|
+
/** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
|
|
165
|
+
decryptParams?: unknown;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Parameters for KMS-based key wrapping. Intended for use with a Key Management System where
|
|
169
|
+
* the encryption key is referenced by URI.
|
|
170
|
+
*/
|
|
171
|
+
export interface KmsUriWrapKeyParams {
|
|
172
|
+
/** Identifier for the private key in the KMS used for encrypting the unwrapped key. */
|
|
173
|
+
encryptionKeyUri: KeyIdentifier;
|
|
174
|
+
/** A {@link Jwk} containing the private key to be wrapped. */
|
|
175
|
+
unwrappedKey: Jwk;
|
|
176
|
+
/** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
|
|
177
|
+
encryptParams?: unknown;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Parameters for KMS-based key deletion. Intended for use with a Key Management System.
|
|
181
|
+
*/
|
|
182
|
+
export interface KmsDeleteKeyParams {
|
|
183
|
+
/** Identifier for the key to be deleted in the KMS. */
|
|
184
|
+
keyUri: KeyIdentifier;
|
|
185
|
+
}
|
|
131
186
|
//# sourceMappingURL=params-kms.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"params-kms.d.ts","sourceRoot":"","sources":["../../../src/types/params-kms.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE1E;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,iDAAiD;IACjD,MAAM,EAAE,aAAa,CAAC;IAEtB,4BAA4B;IAC5B,IAAI,EAAE,UAAU,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,4DAA4D;IAC5D,MAAM,EAAE,aAAa,CAAC;IAEtB;;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EAAE,UAAU,CAAC;CAClB;AAED;;;;GAIG;AACH,MAAM,WAAW,uBAAuB;IACtC,iEAAiE;IACjE,UAAU,EAAE,aAAa,CAAC;IAE1B,qDAAqD;IACrD,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,qFAAqF;IACrF,gBAAgB,EAAE,aAAa,CAAC;IAEhC,+CAA+C;IAC/C,eAAe,EAAE,UAAU,CAAC;IAE5B,0EAA0E;IAC1E,mBAAmB,EAAE,MAAM,CAAC;IAE5B,iGAAiG;IACjG,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,uFAAuF;IACvF,gBAAgB,EAAE,aAAa,CAAC;IAEhC,8DAA8D;IAC9D,YAAY,EAAE,GAAG,CAAC;IAElB,8FAA8F;IAC9F,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,uDAAuD;IACvD,MAAM,EAAE,aAAa,CAAC;CACvB"}
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import type { Cipher } from './types/cipher.js';
|
|
1
2
|
import type { Jwk } from './jose/jwk.js';
|
|
3
|
+
import type { KeyWrapper } from './types/key-wrapper.js';
|
|
4
|
+
import type { KeyExporter, KeyImporter } from './types/key-io.js';
|
|
2
5
|
/**
|
|
3
6
|
* A collection of cryptographic utility methods.
|
|
4
7
|
*/
|
|
@@ -109,4 +112,20 @@ export declare class CryptoUtils {
|
|
|
109
112
|
length: number;
|
|
110
113
|
}): string;
|
|
111
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Type guard that checks whether the given object implements the {@link Cipher} interface.
|
|
117
|
+
*/
|
|
118
|
+
export declare function isCipher<EncryptInput, DecryptInput>(obj: unknown): obj is Cipher<EncryptInput, DecryptInput>;
|
|
119
|
+
/**
|
|
120
|
+
* Type guard that checks whether the given object implements the {@link KeyExporter} interface.
|
|
121
|
+
*/
|
|
122
|
+
export declare function isKeyExporter<ExportKeyInput, ExportKeyOutput>(obj: unknown): obj is KeyExporter<ExportKeyInput, ExportKeyOutput>;
|
|
123
|
+
/**
|
|
124
|
+
* Type guard that checks whether the given object implements the {@link KeyImporter} interface.
|
|
125
|
+
*/
|
|
126
|
+
export declare function isKeyImporter<ImportKeyInput, ImportKeyOutput>(obj: unknown): obj is KeyImporter<ImportKeyInput, ImportKeyOutput>;
|
|
127
|
+
/**
|
|
128
|
+
* Type guard that checks whether the given object implements the {@link KeyWrapper} interface.
|
|
129
|
+
*/
|
|
130
|
+
export declare function isKeyWrapper<WrapKeyInput, UnwrapKeyInput>(obj: unknown): obj is KeyWrapper<WrapKeyInput, UnwrapKeyInput>;
|
|
112
131
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAKlE;;GAEG;AACH,qBAAa,WAAW;IAEtB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,MAAM,CAAC,sCAAsC,CAAC,SAAS,EAAE,GAAG,GAAG,MAAM;IA2BrE;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,MAAM,CAAC,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,UAAU;IAInD;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,MAAM,CAAC,UAAU,IAAI,MAAM;IAO3B;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM;CAiCzD;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,YAAY,EAAE,YAAY,EACjD,GAAG,EAAE,OAAO,GACX,GAAG,IAAI,MAAM,CAAC,YAAY,EAAE,YAAY,CAAC,CAM3C;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,cAAc,EAAE,eAAe,EAC3D,GAAG,EAAE,OAAO,GACX,GAAG,IAAI,WAAW,CAAC,cAAc,EAAE,eAAe,CAAC,CAKrD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,cAAc,EAAE,eAAe,EAC3D,GAAG,EAAE,OAAO,GACX,GAAG,IAAI,WAAW,CAAC,cAAc,EAAE,eAAe,CAAC,CAKrD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,YAAY,EAAE,cAAc,EACvD,GAAG,EAAE,OAAO,GACX,GAAG,IAAI,UAAU,CAAC,YAAY,EAAE,cAAc,CAAC,CAMjD"}
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0;var a=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;var l={}.toString;function c(t=32){if(r&&typeof r.getRandomValues=="function")return r.getRandomValues(new Uint8Array(t));throw new Error("crypto.getRandomValues must be defined")}var u=class t{static getJoseSignatureAlgorithmFromPublicKey(e){let n={Ed25519:"EdDSA","P-256":"ES256","P-384":"ES384","P-521":"ES512",secp256k1:"ES256K"};if(e.alg&&Object.values(n).includes(e.alg))return e.alg;if(e.crv&&Object.keys(n).includes(e.crv))return n[e.crv];throw new Error(`Unable to determine algorithm based on provided input: alg=${e.alg}, crv=${e.crv}. Supported 'alg' values: ${Object.values(n).join(", ")}. Supported 'crv' values: ${Object.keys(n).join(", ")}.`)}static randomBytes(e){return c(e)}static randomUuid(){return r.randomUUID()}static randomPin({length:e}){if(3>e||e>10)throw new Error("randomPin() can securely generate a PIN between 3 to 10 digits.");let n=Math.pow(10,e)-1,o;if(e<=6){let p=Math.pow(10,e);do{let i=t.randomBytes(Math.ceil(e/2));o=new DataView(i.buffer).getUint16(0,!1)%p}while(o>n)}else{let p=Math.pow(10,10);do{let i=t.randomBytes(4);o=new DataView(i.buffer).getUint32(0,!1)%p}while(o>n)}return o.toString().padStart(e,"0")}};function g(t){return t!==null&&typeof t=="object"&&"encrypt"in t&&typeof t.encrypt=="function"&&"decrypt"in t&&typeof t.decrypt=="function"}function x(t){return t!==null&&typeof t=="object"&&"exportKey"in t&&typeof t.exportKey=="function"}function b(t){return t!==null&&typeof t=="object"&&"importKey"in t&&typeof t.importKey=="function"}function h(t){return t!==null&&typeof t=="object"&&"wrapKey"in t&&typeof t.wrapKey=="function"&&"unwrapKey"in t&&typeof t.unwrapKey=="function"}export{u as CryptoUtils,g as isCipher,x as isKeyExporter,b as isKeyImporter,h as isKeyWrapper};
|
|
2
2
|
/*! Bundled license information:
|
|
3
3
|
|
|
4
4
|
@noble/hashes/esm/utils.js:
|
package/dist/utils.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../node_modules/.
|
|
4
|
-
"sourcesContent": ["// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.\n// See utils.ts for details.\ndeclare const globalThis: Record<string, any> | undefined;\nexport const crypto =\n typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;\n", "/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n\n// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.\n// node.js versions earlier than v19 don't declare it in global scope.\n// For node.js, package.json#exports field mapping rewrites import\n// from `crypto` to `cryptoNode`, which imports native module.\n// Makes the utils un-importable in browsers without a bundler.\n// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.\nimport { crypto } from '@noble/hashes/crypto';\nimport { bytes as abytes } from './_assert.js';\n// export { isBytes } from './_assert.js';\n// We can't reuse isBytes from _assert, because somehow this causes huge perf issues\nexport function isBytes(a: unknown): a is Uint8Array {\n return (\n a instanceof Uint8Array ||\n (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array')\n );\n}\n\n// prettier-ignore\nexport type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array |\n Uint16Array | Int16Array | Uint32Array | Int32Array;\n\n// Cast array to different type\nexport const u8 = (arr: TypedArray) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);\nexport const u32 = (arr: TypedArray) =>\n new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));\n\n// Cast array to view\nexport const createView = (arr: TypedArray) =>\n new DataView(arr.buffer, arr.byteOffset, arr.byteLength);\n\n// The rotate right (circular right shift) operation for uint32\nexport const rotr = (word: number, shift: number) => (word << (32 - shift)) | (word >>> shift);\n// The rotate left (circular left shift) operation for uint32\nexport const rotl = (word: number, shift: number) =>\n (word << shift) | ((word >>> (32 - shift)) >>> 0);\n\nexport const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;\n// The byte swap operation for uint32\nexport const byteSwap = (word: number) =>\n ((word << 24) & 0xff000000) |\n ((word << 8) & 0xff0000) |\n ((word >>> 8) & 0xff00) |\n ((word >>> 24) & 0xff);\n// Conditionally byte swap if on a big-endian platform\nexport const byteSwapIfBE = isLE ? (n: number) => n : (n: number) => byteSwap(n);\n\n// In place byte swap for Uint32Array\nexport function byteSwap32(arr: Uint32Array) {\n for (let i = 0; i < arr.length; i++) {\n arr[i] = byteSwap(arr[i]);\n }\n}\n\n// Array where index 0xf0 (240) is mapped to string 'f0'\nconst hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) =>\n i.toString(16).padStart(2, '0')\n);\n/**\n * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'\n */\nexport function bytesToHex(bytes: Uint8Array): string {\n abytes(bytes);\n // pre-caching improves the speed 6x\n let hex = '';\n for (let i = 0; i < bytes.length; i++) {\n hex += hexes[bytes[i]];\n }\n return hex;\n}\n\n// We use optimized technique to convert hex string to byte array\nconst asciis = { _0: 48, _9: 57, _A: 65, _F: 70, _a: 97, _f: 102 } as const;\nfunction asciiToBase16(char: number): number | undefined {\n if (char >= asciis._0 && char <= asciis._9) return char - asciis._0;\n if (char >= asciis._A && char <= asciis._F) return char - (asciis._A - 10);\n if (char >= asciis._a && char <= asciis._f) return char - (asciis._a - 10);\n return;\n}\n\n/**\n * @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])\n */\nexport function hexToBytes(hex: string): Uint8Array {\n if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex);\n const hl = hex.length;\n const al = hl / 2;\n if (hl % 2) throw new Error('padded hex string expected, got unpadded hex of length ' + hl);\n const array = new Uint8Array(al);\n for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {\n const n1 = asciiToBase16(hex.charCodeAt(hi));\n const n2 = asciiToBase16(hex.charCodeAt(hi + 1));\n if (n1 === undefined || n2 === undefined) {\n const char = hex[hi] + hex[hi + 1];\n throw new Error('hex string expected, got non-hex character \"' + char + '\" at index ' + hi);\n }\n array[ai] = n1 * 16 + n2;\n }\n return array;\n}\n\n// There is no setImmediate in browser and setTimeout is slow.\n// call of async fn will return Promise, which will be fullfiled only on\n// next scheduler queue processing step and this is exactly what we need.\nexport const nextTick = async () => {};\n\n// Returns control to thread each 'tick' ms to avoid blocking\nexport async function asyncLoop(iters: number, tick: number, cb: (i: number) => void) {\n let ts = Date.now();\n for (let i = 0; i < iters; i++) {\n cb(i);\n // Date.now() is not monotonic, so in case if clock goes backwards we return return control too\n const diff = Date.now() - ts;\n if (diff >= 0 && diff < tick) continue;\n await nextTick();\n ts += diff;\n }\n}\n\n// Global symbols in both browsers and Node.js since v11\n// See https://github.com/microsoft/TypeScript/issues/31535\ndeclare const TextEncoder: any;\n\n/**\n * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])\n */\nexport function utf8ToBytes(str: string): Uint8Array {\n if (typeof str !== 'string') throw new Error(`utf8ToBytes expected string, got ${typeof str}`);\n return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809\n}\n\nexport type Input = Uint8Array | string;\n/**\n * Normalizes (non-hex) string or Uint8Array to Uint8Array.\n * Warning: when Uint8Array is passed, it would NOT get copied.\n * Keep in mind for future mutable operations.\n */\nexport function toBytes(data: Input): Uint8Array {\n if (typeof data === 'string') data = utf8ToBytes(data);\n abytes(data);\n return data;\n}\n\n/**\n * Copies several Uint8Arrays into one.\n */\nexport function concatBytes(...arrays: Uint8Array[]): Uint8Array {\n let sum = 0;\n for (let i = 0; i < arrays.length; i++) {\n const a = arrays[i];\n abytes(a);\n sum += a.length;\n }\n const res = new Uint8Array(sum);\n for (let i = 0, pad = 0; i < arrays.length; i++) {\n const a = arrays[i];\n res.set(a, pad);\n pad += a.length;\n }\n return res;\n}\n\n// For runtime check if class implements interface\nexport abstract class Hash<T extends Hash<T>> {\n abstract blockLen: number; // Bytes per block\n abstract outputLen: number; // Bytes in output\n abstract update(buf: Input): this;\n // Writes digest into buf\n abstract digestInto(buf: Uint8Array): void;\n abstract digest(): Uint8Array;\n /**\n * Resets internal state. Makes Hash instance unusable.\n * Reset is impossible for keyed hashes if key is consumed into state. If digest is not consumed\n * by user, they will need to manually call `destroy()` when zeroing is necessary.\n */\n abstract destroy(): void;\n /**\n * Clones hash instance. Unsafe: doesn't check whether `to` is valid. Can be used as `clone()`\n * when no options are passed.\n * Reasons to use `_cloneInto` instead of clone: 1) performance 2) reuse instance => all internal\n * buffers are overwritten => causes buffer overwrite which is used for digest in some cases.\n * There are no guarantees for clean-up because it's impossible in JS.\n */\n abstract _cloneInto(to?: T): T;\n // Safe version that clones internal state\n clone(): T {\n return this._cloneInto();\n }\n}\n\n/**\n * XOF: streaming API to read digest in chunks.\n * Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.\n * When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot\n * destroy state, next call can require more bytes.\n */\nexport type HashXOF<T extends Hash<T>> = Hash<T> & {\n xof(bytes: number): Uint8Array; // Read 'bytes' bytes from digest stream\n xofInto(buf: Uint8Array): Uint8Array; // read buf.length bytes from digest stream into buf\n};\n\nconst toStr = {}.toString;\ntype EmptyObj = {};\nexport function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(\n defaults: T1,\n opts?: T2\n): T1 & T2 {\n if (opts !== undefined && toStr.call(opts) !== '[object Object]')\n throw new Error('Options should be object or undefined');\n const merged = Object.assign(defaults, opts);\n return merged as T1 & T2;\n}\n\nexport type CHash = ReturnType<typeof wrapConstructor>;\n\nexport function wrapConstructor<T extends Hash<T>>(hashCons: () => Hash<T>) {\n const hashC = (msg: Input): Uint8Array => hashCons().update(toBytes(msg)).digest();\n const tmp = hashCons();\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = () => hashCons();\n return hashC;\n}\n\nexport function wrapConstructorWithOpts<H extends Hash<H>, T extends Object>(\n hashCons: (opts?: T) => Hash<H>\n) {\n const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({} as T);\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts: T) => hashCons(opts);\n return hashC;\n}\n\nexport function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Object>(\n hashCons: (opts?: T) => HashXOF<H>\n) {\n const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({} as T);\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts: T) => hashCons(opts);\n return hashC;\n}\n\n/**\n * Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS.\n */\nexport function randomBytes(bytesLength = 32): Uint8Array {\n if (crypto && typeof crypto.getRandomValues === 'function') {\n return crypto.getRandomValues(new Uint8Array(bytesLength));\n }\n throw new Error('crypto.getRandomValues must be defined');\n}\n", "import type { Jwk } from './jose/jwk.js';\n\nimport { crypto } from '@noble/hashes/crypto';\nimport { randomBytes as nobleRandomBytes } from '@noble/hashes/utils';\n\n/**\n * A collection of cryptographic utility methods.\n */\nexport class CryptoUtils {\n\n /**\n * Determines the JOSE algorithm identifier of the digital signature algorithm based on the `alg` or\n * `crv` property of a {@link Jwk | JWK}.\n *\n * If the `alg` property is present, its value takes precedence and is returned. Otherwise, the\n * `crv` property is used to determine the algorithm.\n *\n * @memberof CryptoUtils\n * @see {@link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms | JOSE Algorithms}\n * @see {@link https://datatracker.ietf.org/doc/draft-ietf-jose-fully-specified-algorithms/ | Fully-Specified Algorithms for JOSE and COSE}\n *\n * @example\n * ```ts\n * const publicKey: Jwk = {\n * \"kty\": \"OKP\",\n * \"crv\": \"Ed25519\",\n * \"x\": \"FEJG7OakZi500EydXxuE8uMc8uaAzEJkmQeG8khXANw\"\n * }\n * const algorithm = getJoseSignatureAlgorithmFromPublicKey(publicKey);\n * console.log(algorithm); // Output: \"EdDSA\"\n * ```\n * @param publicKey - A JWK containing the `alg` and/or `crv` properties.\n * @returns The name of the algorithm associated with the key.\n * @throws Error if the algorithm cannot be determined from the provided input.\n */\n static getJoseSignatureAlgorithmFromPublicKey(publicKey: Jwk): string {\n const curveToJoseAlgorithm: Record<string, string> = {\n 'Ed25519' : 'EdDSA',\n 'P-256' : 'ES256',\n 'P-384' : 'ES384',\n 'P-521' : 'ES512',\n 'secp256k1' : 'ES256K',\n };\n\n // If the key contains an `alg` property that matches a JOSE registered algorithm identifier,\n // return its value.\n if (publicKey.alg && Object.values(curveToJoseAlgorithm).includes(publicKey.alg)) {\n return publicKey.alg;\n }\n\n // If the key contains a `crv` property, return the corresponding algorithm.\n if (publicKey.crv && Object.keys(curveToJoseAlgorithm).includes(publicKey.crv)) {\n return curveToJoseAlgorithm[publicKey.crv];\n }\n\n throw new Error(\n `Unable to determine algorithm based on provided input: alg=${publicKey.alg}, crv=${publicKey.crv}. ` +\n `Supported 'alg' values: ${Object.values(curveToJoseAlgorithm).join(', ')}. ` +\n `Supported 'crv' values: ${Object.keys(curveToJoseAlgorithm).join(', ')}.`\n );\n }\n\n /**\n * Generates secure pseudorandom values of the specified length using\n * `crypto.getRandomValues`, which defers to the operating system.\n *\n * @memberof CryptoUtils\n * @remarks\n * This function is a wrapper around `randomBytes` from the '@noble/hashes'\n * package. It's designed to be cryptographically strong, suitable for\n * generating initialization vectors, nonces, and other random values.\n *\n * @see {@link https://www.npmjs.com/package/@noble/hashes | @noble/hashes on NPM} for more\n * information about the underlying implementation.\n *\n * @example\n * ```ts\n * const bytes = randomBytes(32); // Generates 32 random bytes\n * ```\n *\n * @param bytesLength - The number of bytes to generate.\n * @returns A Uint8Array containing the generated random bytes.\n */\n static randomBytes(bytesLength: number): Uint8Array {\n return nobleRandomBytes(bytesLength);\n }\n\n /**\n * Generates a UUID (Universally Unique Identifier) using a\n * cryptographically strong random number generator following\n * the version 4 format, as specified in RFC 4122.\n *\n * A version 4 UUID is a randomly generated UUID. The 13th character\n * is set to '4' to denote version 4, and the 17th character is one\n * of '8', '9', 'A', or 'B' to comply with the variant 1 format of\n * UUIDs (the high bits are set to '10').\n *\n * The UUID is a 36 character string, including hyphens, and looks like this:\n * xxxxxxxx-xxxx-4xxx-axxx-xxxxxxxxxxxx\n *\n * Note that while UUIDs are not guaranteed to be unique, they are\n * practically unique\" given the large number of possible UUIDs and\n * the randomness of generation.\n * @memberof CryptoUtils\n * @example\n * ```ts\n * const uuid = randomUuid();\n * console.log(uuid); // Outputs a version 4 UUID, e.g., '123e4567-e89b-12d3-a456-426655440000'\n * ```\n *\n * @returns A string containing a randomly generated, 36 character long v4 UUID.\n */\n static randomUuid(): string {\n const uuid = crypto.randomUUID();\n\n return uuid;\n }\n\n\n /**\n * Generates a secure random PIN (Personal Identification Number) of a\n * specified length.\n *\n * This function ensures that the generated PIN is cryptographically secure and\n * uniformly distributed by using rejection sampling. It repeatedly generates\n * random numbers until it gets one in the desired range [0, max]. This avoids\n * bias introduced by simply taking the modulus or truncating the number.\n *\n * Note: The function can generate PINs of 3 to 10 digits in length.\n * Any request for a PIN outside this range will result in an error.\n *\n * Example usage:\n *\n * ```ts\n * const pin = randomPin({ length: 4 });\n * console.log(pin); // Outputs a 4-digit PIN, e.g., \"0231\"\n * ```\n * @memberof CryptoUtils\n * @param options - The options object containing the desired length of the generated PIN.\n * @param options.length - The desired length of the generated PIN. The value should be\n * an integer between 3 and 8 inclusive.\n *\n * @returns A string representing the generated PIN. The PIN will be zero-padded\n * to match the specified length, if necessary.\n *\n * @throws Will throw an error if the requested PIN length is less than 3 or greater than 8.\n */\n static randomPin({ length }: { length: number }): string {\n if (3 > length || length > 10) {\n throw new Error('randomPin() can securely generate a PIN between 3 to 10 digits.');\n }\n\n const max = Math.pow(10, length) - 1;\n\n let pin;\n\n if (length <= 6) {\n const rejectionRange = Math.pow(10, length);\n do {\n // Adjust the byte generation based on length.\n const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.\n const view = new DataView(randomBuffer.buffer);\n // Convert the buffer to integer and take modulus based on length.\n pin = view.getUint16(0, false) % rejectionRange;\n } while (pin > max);\n } else {\n const rejectionRange = Math.pow(10, 10); // For max 10 digit number.\n do {\n // Generates 4 random bytes.\n const randomBuffer = CryptoUtils.randomBytes(4);\n // Create a DataView to read from the randomBuffer.\n const view = new DataView(randomBuffer.buffer);\n // Transform bytes to number (big endian).\n pin = view.getUint32(0, false) % rejectionRange;\n } while (pin > max); // Reject if the number is outside the desired range.\n }\n\n // Pad the PIN with leading zeros to the desired length.\n return pin.toString().padStart(length, '0');\n }\n}\n"],
|
|
5
|
-
"mappings": "AAGO,IAAMA,EACX,OAAO,YAAe,UAAY,WAAY,WAAa,WAAW,OAAS,OCkC1E,IAAMC,EAAO,IAAI,WAAW,IAAI,YAAY,CAAC,SAAU,CAAC,EAAE,MAAM,EAAE,CAAC,IAAM,GAoKhF,IAAMC,EAAQ,CAAA,EAAG,SAgDX,SAAUC,EAAYC,EAAc,GAAE,CAC1C,GAAIC,GAAU,OAAOA,EAAO,iBAAoB,WAC9C,OAAOA,EAAO,gBAAgB,IAAI,WAAWD,CAAW,CAAC,EAE3D,MAAM,IAAI,MAAM,wCAAwC,CAC1D,
|
|
6
|
-
"names": ["crypto", "isLE", "toStr", "randomBytes", "bytesLength", "crypto", "CryptoUtils", "_CryptoUtils", "publicKey", "curveToJoseAlgorithm", "bytesLength", "randomBytes", "crypto", "length", "max", "pin", "rejectionRange", "randomBuffer"]
|
|
3
|
+
"sources": ["../../../node_modules/.bun/@noble+hashes@1.4.0/node_modules/@noble/hashes/src/crypto.ts", "../../../node_modules/.bun/@noble+hashes@1.4.0/node_modules/@noble/hashes/src/utils.ts", "../src/utils.ts"],
|
|
4
|
+
"sourcesContent": ["// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.\n// See utils.ts for details.\ndeclare const globalThis: Record<string, any> | undefined;\nexport const crypto =\n typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;\n", "/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n\n// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.\n// node.js versions earlier than v19 don't declare it in global scope.\n// For node.js, package.json#exports field mapping rewrites import\n// from `crypto` to `cryptoNode`, which imports native module.\n// Makes the utils un-importable in browsers without a bundler.\n// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.\nimport { crypto } from '@noble/hashes/crypto';\nimport { bytes as abytes } from './_assert.js';\n// export { isBytes } from './_assert.js';\n// We can't reuse isBytes from _assert, because somehow this causes huge perf issues\nexport function isBytes(a: unknown): a is Uint8Array {\n return (\n a instanceof Uint8Array ||\n (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array')\n );\n}\n\n// prettier-ignore\nexport type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array |\n Uint16Array | Int16Array | Uint32Array | Int32Array;\n\n// Cast array to different type\nexport const u8 = (arr: TypedArray) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);\nexport const u32 = (arr: TypedArray) =>\n new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));\n\n// Cast array to view\nexport const createView = (arr: TypedArray) =>\n new DataView(arr.buffer, arr.byteOffset, arr.byteLength);\n\n// The rotate right (circular right shift) operation for uint32\nexport const rotr = (word: number, shift: number) => (word << (32 - shift)) | (word >>> shift);\n// The rotate left (circular left shift) operation for uint32\nexport const rotl = (word: number, shift: number) =>\n (word << shift) | ((word >>> (32 - shift)) >>> 0);\n\nexport const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;\n// The byte swap operation for uint32\nexport const byteSwap = (word: number) =>\n ((word << 24) & 0xff000000) |\n ((word << 8) & 0xff0000) |\n ((word >>> 8) & 0xff00) |\n ((word >>> 24) & 0xff);\n// Conditionally byte swap if on a big-endian platform\nexport const byteSwapIfBE = isLE ? (n: number) => n : (n: number) => byteSwap(n);\n\n// In place byte swap for Uint32Array\nexport function byteSwap32(arr: Uint32Array) {\n for (let i = 0; i < arr.length; i++) {\n arr[i] = byteSwap(arr[i]);\n }\n}\n\n// Array where index 0xf0 (240) is mapped to string 'f0'\nconst hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) =>\n i.toString(16).padStart(2, '0')\n);\n/**\n * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'\n */\nexport function bytesToHex(bytes: Uint8Array): string {\n abytes(bytes);\n // pre-caching improves the speed 6x\n let hex = '';\n for (let i = 0; i < bytes.length; i++) {\n hex += hexes[bytes[i]];\n }\n return hex;\n}\n\n// We use optimized technique to convert hex string to byte array\nconst asciis = { _0: 48, _9: 57, _A: 65, _F: 70, _a: 97, _f: 102 } as const;\nfunction asciiToBase16(char: number): number | undefined {\n if (char >= asciis._0 && char <= asciis._9) return char - asciis._0;\n if (char >= asciis._A && char <= asciis._F) return char - (asciis._A - 10);\n if (char >= asciis._a && char <= asciis._f) return char - (asciis._a - 10);\n return;\n}\n\n/**\n * @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])\n */\nexport function hexToBytes(hex: string): Uint8Array {\n if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex);\n const hl = hex.length;\n const al = hl / 2;\n if (hl % 2) throw new Error('padded hex string expected, got unpadded hex of length ' + hl);\n const array = new Uint8Array(al);\n for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {\n const n1 = asciiToBase16(hex.charCodeAt(hi));\n const n2 = asciiToBase16(hex.charCodeAt(hi + 1));\n if (n1 === undefined || n2 === undefined) {\n const char = hex[hi] + hex[hi + 1];\n throw new Error('hex string expected, got non-hex character \"' + char + '\" at index ' + hi);\n }\n array[ai] = n1 * 16 + n2;\n }\n return array;\n}\n\n// There is no setImmediate in browser and setTimeout is slow.\n// call of async fn will return Promise, which will be fullfiled only on\n// next scheduler queue processing step and this is exactly what we need.\nexport const nextTick = async () => {};\n\n// Returns control to thread each 'tick' ms to avoid blocking\nexport async function asyncLoop(iters: number, tick: number, cb: (i: number) => void) {\n let ts = Date.now();\n for (let i = 0; i < iters; i++) {\n cb(i);\n // Date.now() is not monotonic, so in case if clock goes backwards we return return control too\n const diff = Date.now() - ts;\n if (diff >= 0 && diff < tick) continue;\n await nextTick();\n ts += diff;\n }\n}\n\n// Global symbols in both browsers and Node.js since v11\n// See https://github.com/microsoft/TypeScript/issues/31535\ndeclare const TextEncoder: any;\n\n/**\n * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])\n */\nexport function utf8ToBytes(str: string): Uint8Array {\n if (typeof str !== 'string') throw new Error(`utf8ToBytes expected string, got ${typeof str}`);\n return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809\n}\n\nexport type Input = Uint8Array | string;\n/**\n * Normalizes (non-hex) string or Uint8Array to Uint8Array.\n * Warning: when Uint8Array is passed, it would NOT get copied.\n * Keep in mind for future mutable operations.\n */\nexport function toBytes(data: Input): Uint8Array {\n if (typeof data === 'string') data = utf8ToBytes(data);\n abytes(data);\n return data;\n}\n\n/**\n * Copies several Uint8Arrays into one.\n */\nexport function concatBytes(...arrays: Uint8Array[]): Uint8Array {\n let sum = 0;\n for (let i = 0; i < arrays.length; i++) {\n const a = arrays[i];\n abytes(a);\n sum += a.length;\n }\n const res = new Uint8Array(sum);\n for (let i = 0, pad = 0; i < arrays.length; i++) {\n const a = arrays[i];\n res.set(a, pad);\n pad += a.length;\n }\n return res;\n}\n\n// For runtime check if class implements interface\nexport abstract class Hash<T extends Hash<T>> {\n abstract blockLen: number; // Bytes per block\n abstract outputLen: number; // Bytes in output\n abstract update(buf: Input): this;\n // Writes digest into buf\n abstract digestInto(buf: Uint8Array): void;\n abstract digest(): Uint8Array;\n /**\n * Resets internal state. Makes Hash instance unusable.\n * Reset is impossible for keyed hashes if key is consumed into state. If digest is not consumed\n * by user, they will need to manually call `destroy()` when zeroing is necessary.\n */\n abstract destroy(): void;\n /**\n * Clones hash instance. Unsafe: doesn't check whether `to` is valid. Can be used as `clone()`\n * when no options are passed.\n * Reasons to use `_cloneInto` instead of clone: 1) performance 2) reuse instance => all internal\n * buffers are overwritten => causes buffer overwrite which is used for digest in some cases.\n * There are no guarantees for clean-up because it's impossible in JS.\n */\n abstract _cloneInto(to?: T): T;\n // Safe version that clones internal state\n clone(): T {\n return this._cloneInto();\n }\n}\n\n/**\n * XOF: streaming API to read digest in chunks.\n * Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.\n * When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot\n * destroy state, next call can require more bytes.\n */\nexport type HashXOF<T extends Hash<T>> = Hash<T> & {\n xof(bytes: number): Uint8Array; // Read 'bytes' bytes from digest stream\n xofInto(buf: Uint8Array): Uint8Array; // read buf.length bytes from digest stream into buf\n};\n\nconst toStr = {}.toString;\ntype EmptyObj = {};\nexport function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(\n defaults: T1,\n opts?: T2\n): T1 & T2 {\n if (opts !== undefined && toStr.call(opts) !== '[object Object]')\n throw new Error('Options should be object or undefined');\n const merged = Object.assign(defaults, opts);\n return merged as T1 & T2;\n}\n\nexport type CHash = ReturnType<typeof wrapConstructor>;\n\nexport function wrapConstructor<T extends Hash<T>>(hashCons: () => Hash<T>) {\n const hashC = (msg: Input): Uint8Array => hashCons().update(toBytes(msg)).digest();\n const tmp = hashCons();\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = () => hashCons();\n return hashC;\n}\n\nexport function wrapConstructorWithOpts<H extends Hash<H>, T extends Object>(\n hashCons: (opts?: T) => Hash<H>\n) {\n const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({} as T);\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts: T) => hashCons(opts);\n return hashC;\n}\n\nexport function wrapXOFConstructorWithOpts<H extends HashXOF<H>, T extends Object>(\n hashCons: (opts?: T) => HashXOF<H>\n) {\n const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest();\n const tmp = hashCons({} as T);\n hashC.outputLen = tmp.outputLen;\n hashC.blockLen = tmp.blockLen;\n hashC.create = (opts: T) => hashCons(opts);\n return hashC;\n}\n\n/**\n * Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS.\n */\nexport function randomBytes(bytesLength = 32): Uint8Array {\n if (crypto && typeof crypto.getRandomValues === 'function') {\n return crypto.getRandomValues(new Uint8Array(bytesLength));\n }\n throw new Error('crypto.getRandomValues must be defined');\n}\n", "import type { Cipher } from './types/cipher.js';\nimport type { Jwk } from './jose/jwk.js';\nimport type { KeyWrapper } from './types/key-wrapper.js';\nimport type { KeyExporter, KeyImporter } from './types/key-io.js';\n\nimport { crypto } from '@noble/hashes/crypto';\nimport { randomBytes as nobleRandomBytes } from '@noble/hashes/utils';\n\n/**\n * A collection of cryptographic utility methods.\n */\nexport class CryptoUtils {\n\n /**\n * Determines the JOSE algorithm identifier of the digital signature algorithm based on the `alg` or\n * `crv` property of a {@link Jwk | JWK}.\n *\n * If the `alg` property is present, its value takes precedence and is returned. Otherwise, the\n * `crv` property is used to determine the algorithm.\n *\n * @memberof CryptoUtils\n * @see {@link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms | JOSE Algorithms}\n * @see {@link https://datatracker.ietf.org/doc/draft-ietf-jose-fully-specified-algorithms/ | Fully-Specified Algorithms for JOSE and COSE}\n *\n * @example\n * ```ts\n * const publicKey: Jwk = {\n * \"kty\": \"OKP\",\n * \"crv\": \"Ed25519\",\n * \"x\": \"FEJG7OakZi500EydXxuE8uMc8uaAzEJkmQeG8khXANw\"\n * }\n * const algorithm = getJoseSignatureAlgorithmFromPublicKey(publicKey);\n * console.log(algorithm); // Output: \"EdDSA\"\n * ```\n * @param publicKey - A JWK containing the `alg` and/or `crv` properties.\n * @returns The name of the algorithm associated with the key.\n * @throws Error if the algorithm cannot be determined from the provided input.\n */\n static getJoseSignatureAlgorithmFromPublicKey(publicKey: Jwk): string {\n const curveToJoseAlgorithm: Record<string, string> = {\n 'Ed25519' : 'EdDSA',\n 'P-256' : 'ES256',\n 'P-384' : 'ES384',\n 'P-521' : 'ES512',\n 'secp256k1' : 'ES256K',\n };\n\n // If the key contains an `alg` property that matches a JOSE registered algorithm identifier,\n // return its value.\n if (publicKey.alg && Object.values(curveToJoseAlgorithm).includes(publicKey.alg)) {\n return publicKey.alg;\n }\n\n // If the key contains a `crv` property, return the corresponding algorithm.\n if (publicKey.crv && Object.keys(curveToJoseAlgorithm).includes(publicKey.crv)) {\n return curveToJoseAlgorithm[publicKey.crv];\n }\n\n throw new Error(\n `Unable to determine algorithm based on provided input: alg=${publicKey.alg}, crv=${publicKey.crv}. ` +\n `Supported 'alg' values: ${Object.values(curveToJoseAlgorithm).join(', ')}. ` +\n `Supported 'crv' values: ${Object.keys(curveToJoseAlgorithm).join(', ')}.`\n );\n }\n\n /**\n * Generates secure pseudorandom values of the specified length using\n * `crypto.getRandomValues`, which defers to the operating system.\n *\n * @memberof CryptoUtils\n * @remarks\n * This function is a wrapper around `randomBytes` from the '@noble/hashes'\n * package. It's designed to be cryptographically strong, suitable for\n * generating initialization vectors, nonces, and other random values.\n *\n * @see {@link https://www.npmjs.com/package/@noble/hashes | @noble/hashes on NPM} for more\n * information about the underlying implementation.\n *\n * @example\n * ```ts\n * const bytes = randomBytes(32); // Generates 32 random bytes\n * ```\n *\n * @param bytesLength - The number of bytes to generate.\n * @returns A Uint8Array containing the generated random bytes.\n */\n static randomBytes(bytesLength: number): Uint8Array {\n return nobleRandomBytes(bytesLength);\n }\n\n /**\n * Generates a UUID (Universally Unique Identifier) using a\n * cryptographically strong random number generator following\n * the version 4 format, as specified in RFC 4122.\n *\n * A version 4 UUID is a randomly generated UUID. The 13th character\n * is set to '4' to denote version 4, and the 17th character is one\n * of '8', '9', 'A', or 'B' to comply with the variant 1 format of\n * UUIDs (the high bits are set to '10').\n *\n * The UUID is a 36 character string, including hyphens, and looks like this:\n * xxxxxxxx-xxxx-4xxx-axxx-xxxxxxxxxxxx\n *\n * Note that while UUIDs are not guaranteed to be unique, they are\n * practically unique\" given the large number of possible UUIDs and\n * the randomness of generation.\n * @memberof CryptoUtils\n * @example\n * ```ts\n * const uuid = randomUuid();\n * console.log(uuid); // Outputs a version 4 UUID, e.g., '123e4567-e89b-12d3-a456-426655440000'\n * ```\n *\n * @returns A string containing a randomly generated, 36 character long v4 UUID.\n */\n static randomUuid(): string {\n const uuid = crypto.randomUUID();\n\n return uuid;\n }\n\n\n /**\n * Generates a secure random PIN (Personal Identification Number) of a\n * specified length.\n *\n * This function ensures that the generated PIN is cryptographically secure and\n * uniformly distributed by using rejection sampling. It repeatedly generates\n * random numbers until it gets one in the desired range [0, max]. This avoids\n * bias introduced by simply taking the modulus or truncating the number.\n *\n * Note: The function can generate PINs of 3 to 10 digits in length.\n * Any request for a PIN outside this range will result in an error.\n *\n * Example usage:\n *\n * ```ts\n * const pin = randomPin({ length: 4 });\n * console.log(pin); // Outputs a 4-digit PIN, e.g., \"0231\"\n * ```\n * @memberof CryptoUtils\n * @param options - The options object containing the desired length of the generated PIN.\n * @param options.length - The desired length of the generated PIN. The value should be\n * an integer between 3 and 8 inclusive.\n *\n * @returns A string representing the generated PIN. The PIN will be zero-padded\n * to match the specified length, if necessary.\n *\n * @throws Will throw an error if the requested PIN length is less than 3 or greater than 8.\n */\n static randomPin({ length }: { length: number }): string {\n if (3 > length || length > 10) {\n throw new Error('randomPin() can securely generate a PIN between 3 to 10 digits.');\n }\n\n const max = Math.pow(10, length) - 1;\n\n let pin;\n\n if (length <= 6) {\n const rejectionRange = Math.pow(10, length);\n do {\n // Adjust the byte generation based on length.\n const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.\n const view = new DataView(randomBuffer.buffer);\n // Convert the buffer to integer and take modulus based on length.\n pin = view.getUint16(0, false) % rejectionRange;\n } while (pin > max);\n } else {\n const rejectionRange = Math.pow(10, 10); // For max 10 digit number.\n do {\n // Generates 4 random bytes.\n const randomBuffer = CryptoUtils.randomBytes(4);\n // Create a DataView to read from the randomBuffer.\n const view = new DataView(randomBuffer.buffer);\n // Transform bytes to number (big endian).\n pin = view.getUint32(0, false) % rejectionRange;\n } while (pin > max); // Reject if the number is outside the desired range.\n }\n\n // Pad the PIN with leading zeros to the desired length.\n return pin.toString().padStart(length, '0');\n }\n}\n\n/**\n * Type guard that checks whether the given object implements the {@link Cipher} interface.\n */\nexport function isCipher<EncryptInput, DecryptInput>(\n obj: unknown\n): obj is Cipher<EncryptInput, DecryptInput> {\n return (\n obj !== null && typeof obj === 'object'\n && 'encrypt' in obj && typeof obj.encrypt === 'function'\n && 'decrypt' in obj && typeof obj.decrypt === 'function'\n );\n}\n\n/**\n * Type guard that checks whether the given object implements the {@link KeyExporter} interface.\n */\nexport function isKeyExporter<ExportKeyInput, ExportKeyOutput>(\n obj: unknown\n): obj is KeyExporter<ExportKeyInput, ExportKeyOutput> {\n return (\n obj !== null && typeof obj === 'object'\n && 'exportKey' in obj && typeof obj.exportKey === 'function'\n );\n}\n\n/**\n * Type guard that checks whether the given object implements the {@link KeyImporter} interface.\n */\nexport function isKeyImporter<ImportKeyInput, ImportKeyOutput>(\n obj: unknown\n): obj is KeyImporter<ImportKeyInput, ImportKeyOutput> {\n return (\n obj !== null && typeof obj === 'object'\n && 'importKey' in obj && typeof obj.importKey === 'function'\n );\n}\n\n/**\n * Type guard that checks whether the given object implements the {@link KeyWrapper} interface.\n */\nexport function isKeyWrapper<WrapKeyInput, UnwrapKeyInput>(\n obj: unknown\n): obj is KeyWrapper<WrapKeyInput, UnwrapKeyInput> {\n return (\n obj !== null && typeof obj === 'object'\n && 'wrapKey' in obj && typeof obj.wrapKey === 'function'\n && 'unwrapKey' in obj && typeof obj.unwrapKey === 'function'\n );\n}\n"],
|
|
5
|
+
"mappings": "AAGO,IAAMA,EACX,OAAO,YAAe,UAAY,WAAY,WAAa,WAAW,OAAS,OCkC1E,IAAMC,EAAO,IAAI,WAAW,IAAI,YAAY,CAAC,SAAU,CAAC,EAAE,MAAM,EAAE,CAAC,IAAM,GAoKhF,IAAMC,EAAQ,CAAA,EAAG,SAgDX,SAAUC,EAAYC,EAAc,GAAE,CAC1C,GAAIC,GAAU,OAAOA,EAAO,iBAAoB,WAC9C,OAAOA,EAAO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,IAAI,SAASE,EAAa,MAAM,EAElC,UAAU,EAAG,EAAK,EAAID,CACnC,OAASD,EAAMD,EACjB,CAGA,OAAOC,EAAI,SAAS,EAAE,SAASF,EAAQ,GAAG,CAC5C,CACF,EAKO,SAASK,EACdC,EAC2C,CAC3C,OACEA,IAAQ,MAAQ,OAAOA,GAAQ,UAC5B,YAAaA,GAAO,OAAOA,EAAI,SAAY,YAC3C,YAAaA,GAAO,OAAOA,EAAI,SAAY,UAElD,CAKO,SAASC,EACdD,EACqD,CACrD,OACEA,IAAQ,MAAQ,OAAOA,GAAQ,UAC5B,cAAeA,GAAO,OAAOA,EAAI,WAAc,UAEtD,CAKO,SAASE,EACdF,EACqD,CACrD,OACEA,IAAQ,MAAQ,OAAOA,GAAQ,UAC5B,cAAeA,GAAO,OAAOA,EAAI,WAAc,UAEtD,CAKO,SAASG,EACdH,EACiD,CACjD,OACEA,IAAQ,MAAQ,OAAOA,GAAQ,UAC5B,YAAaA,GAAO,OAAOA,EAAI,SAAY,YAC3C,cAAeA,GAAO,OAAOA,EAAI,WAAc,UAEtD",
|
|
6
|
+
"names": ["crypto", "isLE", "toStr", "randomBytes", "bytesLength", "crypto", "CryptoUtils", "_CryptoUtils", "publicKey", "curveToJoseAlgorithm", "bytesLength", "randomBytes", "crypto", "length", "max", "pin", "rejectionRange", "randomBuffer", "isCipher", "obj", "isKeyExporter", "isKeyImporter", "isKeyWrapper"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enbox/crypto",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Web5 cryptographic library",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/
|
|
6
|
+
"main": "./dist/esm/index.js",
|
|
7
7
|
"module": "./dist/esm/index.js",
|
|
8
8
|
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"clean": "rimraf dist",
|
|
11
|
+
"build:esm": "rimraf dist/esm dist/types && bun tsc -p tsconfig.json",
|
|
12
|
+
"build:browser": "rimraf dist/browser.mjs && bun ../../build/browser-bundle.js --extra-entry src/utils.ts:dist/utils.js",
|
|
13
|
+
"build": "bun run clean && bun run build:esm && bun run build:browser",
|
|
14
|
+
"lint": "eslint . --max-warnings 0",
|
|
15
|
+
"lint:fix": "eslint . --fix",
|
|
16
|
+
"test:node": "bun test .test.ts",
|
|
17
|
+
"test:node:coverage": "bun test --coverage --coverage-reporter=text --coverage-reporter=lcov --coverage-dir=coverage .test.ts",
|
|
18
|
+
"test:browser": "bunx --bun vitest --config vitest.browser.config.ts --run",
|
|
19
|
+
"test:browser:coverage": "bunx --bun vitest --config vitest.browser.config.ts --run --coverage --coverage.provider=istanbul --coverage.reportsDirectory=./coverage-browser"
|
|
20
|
+
},
|
|
9
21
|
"homepage": "https://github.com/enboxorg/enbox/tree/main/packages/crypto#readme",
|
|
10
22
|
"bugs": "https://github.com/enboxorg/enbox/issues",
|
|
11
23
|
"repository": {
|
|
@@ -15,6 +27,10 @@
|
|
|
15
27
|
},
|
|
16
28
|
"license": "Apache-2.0",
|
|
17
29
|
"contributors": [
|
|
30
|
+
{
|
|
31
|
+
"name": "Liran Cohen",
|
|
32
|
+
"url": "https://github.com/LiranCohen"
|
|
33
|
+
},
|
|
18
34
|
{
|
|
19
35
|
"name": "Daniel Buchner",
|
|
20
36
|
"url": "https://github.com/csuwildcat"
|
|
@@ -35,13 +51,9 @@
|
|
|
35
51
|
"exports": {
|
|
36
52
|
".": {
|
|
37
53
|
"types": "./dist/types/index.d.ts",
|
|
38
|
-
"import": "./dist/esm/index.js"
|
|
39
|
-
"require": "./dist/cjs/index.js"
|
|
54
|
+
"import": "./dist/esm/index.js"
|
|
40
55
|
}
|
|
41
56
|
},
|
|
42
|
-
"browser": {
|
|
43
|
-
"node:crypto": false
|
|
44
|
-
},
|
|
45
57
|
"react-native": "./dist/esm/index.js",
|
|
46
58
|
"keywords": [
|
|
47
59
|
"cryptography",
|
|
@@ -53,51 +65,23 @@
|
|
|
53
65
|
"access": "public"
|
|
54
66
|
},
|
|
55
67
|
"engines": {
|
|
56
|
-
"
|
|
68
|
+
"bun": ">=1.0.0"
|
|
57
69
|
},
|
|
58
70
|
"dependencies": {
|
|
59
71
|
"@noble/ciphers": "0.5.3",
|
|
60
72
|
"@noble/curves": "1.3.0",
|
|
61
73
|
"@noble/hashes": "1.4.0",
|
|
62
|
-
"@enbox/common": "0.0.
|
|
74
|
+
"@enbox/common": "0.0.3"
|
|
63
75
|
},
|
|
64
76
|
"devDependencies": {
|
|
65
|
-
"@playwright/test": "1.45.3",
|
|
66
|
-
"@types/chai": "4.3.16",
|
|
67
|
-
"@types/chai-as-promised": "7.1.8",
|
|
68
|
-
"@types/eslint": "8.56.10",
|
|
69
|
-
"@types/mocha": "10.0.6",
|
|
70
77
|
"@types/node": "20.14.8",
|
|
71
|
-
"@
|
|
72
|
-
"@typescript-eslint/
|
|
73
|
-
"@
|
|
74
|
-
"@
|
|
75
|
-
"
|
|
76
|
-
"c8": "10.1.2",
|
|
77
|
-
"chai": "5.1.1",
|
|
78
|
-
"chai-as-promised": "8.0.0",
|
|
79
|
-
"esbuild": "0.23.0",
|
|
80
|
-
"eslint": "9.5.0",
|
|
81
|
-
"eslint-plugin-mocha": "10.4.3",
|
|
82
|
-
"mocha": "10.4.0",
|
|
83
|
-
"mocha-junit-reporter": "2.2.1",
|
|
84
|
-
"playwright": "1.45.3",
|
|
78
|
+
"@typescript-eslint/eslint-plugin": "8.32.1",
|
|
79
|
+
"@typescript-eslint/parser": "8.32.1",
|
|
80
|
+
"@vitest/browser-playwright": "4.0.18",
|
|
81
|
+
"@vitest/coverage-istanbul": "4.0.18",
|
|
82
|
+
"eslint": "9.7.0",
|
|
85
83
|
"rimraf": "5.0.7",
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"typescript": "5.4.5"
|
|
89
|
-
},
|
|
90
|
-
"scripts": {
|
|
91
|
-
"clean": "rimraf dist tests/compiled",
|
|
92
|
-
"build:esm": "rimraf dist/esm dist/types && pnpm tsc -p tsconfig.json",
|
|
93
|
-
"build:cjs": "rimraf dist/cjs && pnpm tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
|
|
94
|
-
"build:browser": "rimraf dist/browser.mjs dist/browser.js && node build/bundles.js",
|
|
95
|
-
"build:tests:node": "rimraf tests/compiled && pnpm tsc -p tests/tsconfig.json",
|
|
96
|
-
"build:tests:browser": "rimraf tests/compiled && node build/esbuild-tests.cjs",
|
|
97
|
-
"build": "pnpm clean && pnpm build:esm && pnpm build:cjs && pnpm build:browser",
|
|
98
|
-
"lint": "eslint . --max-warnings 0",
|
|
99
|
-
"lint:fix": "eslint . --fix",
|
|
100
|
-
"test:node": "pnpm build:tests:node && pnpm c8 mocha",
|
|
101
|
-
"test:browser": "pnpm build:tests:browser && web-test-runner"
|
|
84
|
+
"typescript": "5.5.4",
|
|
85
|
+
"vitest": "4.0.18"
|
|
102
86
|
}
|
|
103
|
-
}
|
|
87
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Jwk } from '../jose/jwk.js';
|
|
2
1
|
import type { Cipher } from '../types/cipher.js';
|
|
2
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
3
3
|
import type { KeyGenerator } from '../types/key-generator.js';
|
|
4
4
|
import type { DecryptParams, EncryptParams, GenerateKeyParams } from '../types/params-direct.js';
|
|
5
5
|
|
|
@@ -39,7 +39,7 @@ export interface AesCtrParams {
|
|
|
39
39
|
* decryption features.
|
|
40
40
|
*
|
|
41
41
|
* This class is typically accessed through implementations that extend the
|
|
42
|
-
* {@link
|
|
42
|
+
* {@link DsaApi | `DsaApi`} interface.
|
|
43
43
|
*/
|
|
44
44
|
export class AesCtrAlgorithm extends CryptoAlgorithm
|
|
45
45
|
implements Cipher<EncryptParams & AesCtrParams, DecryptParams & AesCtrParams>,
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AES_GCM_TAG_LENGTHS } from '../primitives/aes-gcm.js';
|
|
2
2
|
import type { Cipher } from '../types/cipher.js';
|
|
3
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
4
|
+
import type { KeyConverter } from '../types/key-converter.js';
|
|
3
5
|
import type { KeyGenerator } from '../types/key-generator.js';
|
|
4
|
-
import type { DecryptParams, EncryptParams, GenerateKeyParams } from '../types/params-direct.js';
|
|
6
|
+
import type { BytesToPrivateKeyParams, DecryptParams, EncryptParams, GenerateKeyParams, PrivateKeyToBytesParams } from '../types/params-direct.js';
|
|
5
7
|
|
|
8
|
+
import { AesGcm } from '../primitives/aes-gcm.js';
|
|
6
9
|
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
7
|
-
import { AesGcm, AES_GCM_TAG_LENGTHS } from '../primitives/aes-gcm.js';
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* The `AesGcmGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
@@ -62,12 +64,32 @@ export interface AesGcmParams {
|
|
|
62
64
|
* key generation, encryption, and decryption features.
|
|
63
65
|
*
|
|
64
66
|
* This class is typically accessed through implementations that extend the
|
|
65
|
-
* {@link
|
|
67
|
+
* {@link DsaApi | `DsaApi`} interface.
|
|
66
68
|
*/
|
|
67
69
|
export class AesGcmAlgorithm extends CryptoAlgorithm
|
|
68
70
|
implements Cipher<AesGcmParams, AesGcmParams>,
|
|
71
|
+
KeyConverter,
|
|
69
72
|
KeyGenerator<AesGcmGenerateKeyParams, Jwk> {
|
|
70
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Converts a private key from a byte array to JWK format, setting the `alg` property based on
|
|
76
|
+
* the key length.
|
|
77
|
+
*
|
|
78
|
+
* @param params - The parameters for the private key conversion.
|
|
79
|
+
* @param params.privateKeyBytes - The raw private key as a Uint8Array.
|
|
80
|
+
*
|
|
81
|
+
* @returns A Promise that resolves to the private key in JWK format.
|
|
82
|
+
*/
|
|
83
|
+
public async bytesToPrivateKey({ privateKeyBytes }: BytesToPrivateKeyParams): Promise<Jwk> {
|
|
84
|
+
// Convert the byte array to a JWK.
|
|
85
|
+
const privateKey = await AesGcm.bytesToPrivateKey({ privateKeyBytes });
|
|
86
|
+
|
|
87
|
+
// Set the `alg` property based on the key length.
|
|
88
|
+
privateKey.alg = { 16: 'A128GCM', 24: 'A192GCM', 32: 'A256GCM' }[privateKeyBytes.length];
|
|
89
|
+
|
|
90
|
+
return privateKey;
|
|
91
|
+
}
|
|
92
|
+
|
|
71
93
|
/**
|
|
72
94
|
* Decrypts the provided data using AES-GCM.
|
|
73
95
|
*
|
|
@@ -184,4 +206,19 @@ export class AesGcmAlgorithm extends CryptoAlgorithm
|
|
|
184
206
|
|
|
185
207
|
return privateKey;
|
|
186
208
|
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Converts a private key from JWK format to a byte array.
|
|
212
|
+
*
|
|
213
|
+
* @param params - The parameters for the private key conversion.
|
|
214
|
+
* @param params.privateKey - The private key in JWK format.
|
|
215
|
+
*
|
|
216
|
+
* @returns A Promise that resolves to the private key as a Uint8Array.
|
|
217
|
+
*/
|
|
218
|
+
public async privateKeyToBytes({ privateKey }: PrivateKeyToBytesParams): Promise<Uint8Array> {
|
|
219
|
+
// Convert the JWK to a byte array.
|
|
220
|
+
const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
|
|
221
|
+
|
|
222
|
+
return privateKeyBytes;
|
|
223
|
+
}
|
|
187
224
|
}
|