@tstdl/base 0.93.178 → 0.93.180
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/api/response.js +4 -3
- package/api/server/gateway.js +9 -3
- package/audit/auditor.d.ts +1 -2
- package/audit/drizzle/{0000_lumpy_thunderball.sql → 0000_shallow_elektra.sql} +1 -1
- package/audit/drizzle/meta/0000_snapshot.json +2 -2
- package/audit/drizzle/meta/_journal.json +2 -2
- package/authentication/README.md +87 -42
- package/authentication/authentication.api.d.ts +392 -53
- package/authentication/authentication.api.js +133 -28
- package/authentication/client/api.client.d.ts +3 -3
- package/authentication/client/api.client.js +4 -4
- package/authentication/client/authentication.service.d.ts +93 -23
- package/authentication/client/authentication.service.js +113 -28
- package/authentication/client/http-client.middleware.d.ts +1 -1
- package/authentication/client/http-client.middleware.js +5 -4
- package/authentication/client/module.d.ts +1 -1
- package/authentication/client/module.js +2 -2
- package/authentication/errors/index.d.ts +1 -1
- package/authentication/errors/index.js +1 -1
- package/authentication/errors/password-requirements.error.d.ts +5 -0
- package/authentication/errors/{secret-requirements.error.js → password-requirements.error.js} +2 -2
- package/authentication/models/authentication-password.model.d.ts +8 -0
- package/authentication/models/{authentication-credentials.model.js → authentication-password.model.js} +11 -17
- package/authentication/models/authentication-session.model.d.ts +0 -2
- package/authentication/models/authentication-session.model.js +1 -7
- package/authentication/models/authentication-totp-recovery-code.model.d.ts +6 -0
- package/authentication/models/authentication-totp-recovery-code.model.js +34 -0
- package/authentication/models/authentication-totp.model.d.ts +19 -0
- package/authentication/models/authentication-totp.model.js +51 -0
- package/authentication/models/authentication-used-totp-token.model.d.ts +5 -0
- package/authentication/models/authentication-used-totp-token.model.js +32 -0
- package/authentication/models/index.d.ts +6 -3
- package/authentication/models/index.js +6 -3
- package/authentication/models/{init-secret-reset-data.model.d.ts → init-password-reset-data.model.d.ts} +3 -3
- package/authentication/models/{init-secret-reset-data.model.js → init-password-reset-data.model.js} +5 -5
- package/authentication/models/password-check-result.model.d.ts +3 -0
- package/authentication/models/{secret-check-result.model.js → password-check-result.model.js} +6 -6
- package/authentication/models/subject.model.d.ts +0 -6
- package/authentication/models/subject.model.js +0 -6
- package/authentication/models/token.model.d.ts +16 -2
- package/authentication/server/authentication-ancillary.service.d.ts +6 -6
- package/authentication/server/authentication-ancillary.service.js +1 -1
- package/authentication/server/authentication-password-requirements.validator.d.ts +55 -0
- package/authentication/server/{authentication-secret-requirements.validator.js → authentication-password-requirements.validator.js} +22 -22
- package/authentication/server/authentication.api-controller.d.ts +55 -27
- package/authentication/server/authentication.api-controller.js +214 -39
- package/authentication/server/authentication.audit.d.ts +42 -5
- package/authentication/server/authentication.service.d.ts +182 -93
- package/authentication/server/authentication.service.js +628 -206
- package/authentication/server/drizzle/{0000_soft_tag.sql → 0000_odd_echo.sql} +59 -13
- package/authentication/server/drizzle/meta/0000_snapshot.json +345 -32
- package/authentication/server/drizzle/meta/_journal.json +2 -2
- package/authentication/server/helper.d.ts +16 -16
- package/authentication/server/helper.js +33 -34
- package/authentication/server/index.d.ts +1 -1
- package/authentication/server/index.js +1 -1
- package/authentication/server/module.d.ts +2 -2
- package/authentication/server/module.js +4 -2
- package/authentication/server/schemas.d.ts +11 -7
- package/authentication/server/schemas.js +7 -3
- package/authentication/tests/authentication-password-requirements.validator.test.js +29 -0
- package/authentication/tests/authentication.api-controller.test.js +49 -15
- package/authentication/tests/authentication.client-error-handling.test.js +3 -2
- package/authentication/tests/authentication.client-middleware.test.js +5 -5
- package/authentication/tests/authentication.client-service-methods.test.js +28 -14
- package/authentication/tests/authentication.client-service-refresh.test.js +7 -6
- package/authentication/tests/authentication.client-service.test.js +10 -8
- package/authentication/tests/authentication.service.test.js +37 -29
- package/authentication/tests/authentication.test-ancillary-service.d.ts +1 -1
- package/authentication/tests/authentication.test-ancillary-service.js +1 -1
- package/authentication/tests/brute-force-protection.test.js +211 -0
- package/authentication/tests/helper.test.js +25 -21
- package/authentication/tests/password-requirements.error.test.js +14 -0
- package/authentication/tests/remember.api.test.js +22 -14
- package/authentication/tests/remember.service.test.js +23 -16
- package/authentication/tests/subject.service.test.js +2 -2
- package/authentication/tests/suspended-subject.test.d.ts +1 -0
- package/authentication/tests/suspended-subject.test.js +120 -0
- package/authentication/tests/totp.enrollment.test.d.ts +1 -0
- package/authentication/tests/totp.enrollment.test.js +123 -0
- package/authentication/tests/totp.login.test.d.ts +1 -0
- package/authentication/tests/totp.login.test.js +213 -0
- package/authentication/tests/totp.recovery-codes.test.d.ts +1 -0
- package/authentication/tests/totp.recovery-codes.test.js +97 -0
- package/authentication/tests/totp.status.test.d.ts +1 -0
- package/authentication/tests/totp.status.test.js +72 -0
- package/circuit-breaker/postgres/drizzle/{0000_cooing_korath.sql → 0000_same_captain_cross.sql} +1 -1
- package/circuit-breaker/postgres/drizzle/meta/0000_snapshot.json +2 -2
- package/circuit-breaker/postgres/drizzle/meta/_journal.json +2 -2
- package/cryptography/cryptography.d.ts +336 -0
- package/cryptography/cryptography.js +328 -0
- package/cryptography/index.d.ts +4 -0
- package/cryptography/index.js +4 -0
- package/{utils → cryptography}/jwt.d.ts +22 -4
- package/{utils → cryptography}/jwt.js +36 -18
- package/cryptography/module.d.ts +35 -0
- package/cryptography/module.js +148 -0
- package/cryptography/tests/cryptography.test.d.ts +1 -0
- package/cryptography/tests/cryptography.test.js +175 -0
- package/cryptography/tests/jwt.test.d.ts +1 -0
- package/cryptography/tests/jwt.test.js +54 -0
- package/cryptography/tests/modern.test.d.ts +1 -0
- package/cryptography/tests/modern.test.js +105 -0
- package/cryptography/tests/module.test.d.ts +1 -0
- package/cryptography/tests/module.test.js +100 -0
- package/cryptography/tests/totp.test.d.ts +1 -0
- package/cryptography/tests/totp.test.js +108 -0
- package/cryptography/totp.d.ts +96 -0
- package/cryptography/totp.js +123 -0
- package/document-management/server/drizzle/{0000_curious_nighthawk.sql → 0000_sharp_scream.sql} +21 -21
- package/document-management/server/drizzle/meta/0000_snapshot.json +22 -22
- package/document-management/server/drizzle/meta/_journal.json +2 -2
- package/document-management/server/services/document-file.service.js +1 -1
- package/errors/errors.localization.d.ts +2 -2
- package/errors/errors.localization.js +2 -2
- package/errors/index.d.ts +1 -0
- package/errors/index.js +1 -0
- package/errors/too-many-requests.error.d.ts +5 -0
- package/errors/too-many-requests.error.js +7 -0
- package/examples/api/authentication.js +5 -5
- package/examples/api/custom-authentication.js +4 -3
- package/file/server/mime-type.js +1 -1
- package/http/http-body.d.ts +1 -0
- package/http/http-body.js +3 -0
- package/image-service/imgproxy/imgproxy-image-service.d.ts +0 -1
- package/image-service/imgproxy/imgproxy-image-service.js +9 -27
- package/key-value-store/postgres/drizzle/{0000_shocking_slipstream.sql → 0000_moaning_calypso.sql} +1 -1
- package/key-value-store/postgres/drizzle/meta/0000_snapshot.json +2 -2
- package/key-value-store/postgres/drizzle/meta/_journal.json +2 -2
- package/lock/postgres/drizzle/{0000_busy_tattoo.sql → 0000_nappy_wraith.sql} +1 -1
- package/lock/postgres/drizzle/meta/0000_snapshot.json +2 -2
- package/lock/postgres/drizzle/meta/_journal.json +2 -2
- package/logger/formatters/json.js +1 -1
- package/logger/formatters/pretty-print.js +1 -1
- package/mail/drizzle/{0000_numerous_the_watchers.sql → 0000_cultured_quicksilver.sql} +2 -2
- package/mail/drizzle/meta/0000_snapshot.json +4 -4
- package/mail/drizzle/meta/_journal.json +2 -9
- package/notification/server/drizzle/{0000_wise_pyro.sql → 0000_new_tenebrous.sql} +6 -6
- package/notification/server/drizzle/meta/0000_snapshot.json +7 -7
- package/notification/server/drizzle/meta/_journal.json +2 -2
- package/notification/tests/notification-flow.test.js +1 -8
- package/notification/tests/notification-type.service.test.js +3 -3
- package/openid-connect/oidc.service.js +2 -3
- package/orm/data-types/common.js +1 -1
- package/orm/server/drizzle/schema-converter.js +9 -4
- package/orm/server/encryption.js +1 -1
- package/orm/server/module.d.ts +0 -1
- package/orm/server/module.js +0 -4
- package/orm/server/repository.d.ts +2 -1
- package/orm/server/repository.js +7 -10
- package/orm/tests/encryption.test.js +4 -6
- package/orm/tests/repository-extra-coverage.test.js +0 -2
- package/orm/tests/repository-regression.test.js +0 -3
- package/package.json +9 -8
- package/password/README.md +1 -1
- package/password/have-i-been-pwned.js +1 -1
- package/rate-limit/postgres/drizzle/{0000_watery_rage.sql → 0000_serious_sauron.sql} +1 -1
- package/rate-limit/postgres/drizzle/meta/0000_snapshot.json +2 -2
- package/rate-limit/postgres/drizzle/meta/_journal.json +2 -2
- package/rate-limit/postgres/postgres-rate-limiter.d.ts +1 -1
- package/rate-limit/postgres/postgres-rate-limiter.js +1 -1
- package/rate-limit/rate-limiter.d.ts +1 -1
- package/rpc/tests/rpc.integration.test.js +25 -31
- package/supports.d.ts +1 -0
- package/supports.js +1 -0
- package/task-queue/postgres/drizzle/{0000_faithful_daimon_hellstrom.sql → 0000_dark_ronan.sql} +5 -5
- package/task-queue/postgres/drizzle/meta/0000_snapshot.json +10 -10
- package/task-queue/postgres/drizzle/meta/_journal.json +2 -9
- package/task-queue/postgres/task-queue.js +2 -2
- package/task-queue/tests/coverage-enhancement.test.js +2 -2
- package/test/drizzle/{0000_natural_cannonball.sql → 0000_organic_gamora.sql} +2 -2
- package/test/drizzle/meta/0000_snapshot.json +3 -4
- package/test/drizzle/meta/_journal.json +2 -9
- package/testing/integration-setup.d.ts +7 -3
- package/testing/integration-setup.js +119 -96
- package/utils/alphabet.d.ts +1 -0
- package/utils/alphabet.js +1 -0
- package/utils/base32.d.ts +4 -0
- package/utils/base32.js +49 -0
- package/utils/base64.d.ts +0 -2
- package/utils/base64.js +6 -70
- package/utils/equals.d.ts +13 -3
- package/utils/equals.js +29 -9
- package/utils/index.d.ts +1 -2
- package/utils/index.js +1 -2
- package/utils/random.d.ts +1 -0
- package/utils/random.js +14 -8
- package/authentication/errors/secret-requirements.error.d.ts +0 -5
- package/authentication/models/authentication-credentials.model.d.ts +0 -10
- package/authentication/models/secret-check-result.model.d.ts +0 -3
- package/authentication/server/authentication-secret-requirements.validator.d.ts +0 -55
- package/authentication/tests/authentication-ancillary.service.test.js +0 -13
- package/authentication/tests/authentication-secret-requirements.validator.test.js +0 -29
- package/authentication/tests/secret-requirements.error.test.js +0 -14
- package/mail/drizzle/0001_married_tarantula.sql +0 -12
- package/mail/drizzle/meta/0001_snapshot.json +0 -69
- package/orm/server/tokens.d.ts +0 -1
- package/orm/server/tokens.js +0 -2
- package/task-queue/postgres/drizzle/0001_rapid_infant_terrible.sql +0 -16
- package/task-queue/postgres/drizzle/meta/0001_snapshot.json +0 -753
- package/test/drizzle/0001_closed_the_captain.sql +0 -2
- package/test/drizzle/meta/0001_snapshot.json +0 -117
- package/utils/cryptography.d.ts +0 -137
- package/utils/cryptography.js +0 -201
- /package/authentication/tests/{authentication-ancillary.service.test.d.ts → authentication-password-requirements.validator.test.d.ts} +0 -0
- /package/authentication/tests/{authentication-secret-requirements.validator.test.d.ts → brute-force-protection.test.d.ts} +0 -0
- /package/authentication/tests/{secret-requirements.error.test.d.ts → password-requirements.error.test.d.ts} +0 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import type { TypedExtract, TypedOmit } from '../types/index.js';
|
|
2
|
+
declare global {
|
|
3
|
+
interface SubtleCrypto {
|
|
4
|
+
encapsulateKey(algorithm: string | Algorithm, publicKey: CryptoKey, sharedKeyAlgorithm: string | Algorithm | HmacImportParams | AesKeyAlgorithm | KmacImportParams, extractable: boolean, usages: KeyUsage[]): Promise<{
|
|
5
|
+
ciphertext: ArrayBuffer;
|
|
6
|
+
sharedKey: CryptoKey;
|
|
7
|
+
}>;
|
|
8
|
+
decapsulateKey(algorithm: string | Algorithm, privateKey: CryptoKey, ciphertext: BufferSource, sharedKeyAlgorithm: string | Algorithm | HmacImportParams | AesKeyAlgorithm | KmacImportParams, extractable: boolean, usages: KeyUsage[]): Promise<CryptoKey>;
|
|
9
|
+
encapsulateBits(algorithm: string | Algorithm, publicKey: CryptoKey): Promise<{
|
|
10
|
+
ciphertext: ArrayBuffer;
|
|
11
|
+
sharedKey: ArrayBuffer;
|
|
12
|
+
}>;
|
|
13
|
+
decapsulateBits(algorithm: string | Algorithm, privateKey: CryptoKey, ciphertext: BufferSource): Promise<ArrayBuffer>;
|
|
14
|
+
getPublicKey(key: CryptoKey, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export type AesMode = 'CBC' | 'CTR' | 'GCM' | 'KW' | 'OCB';
|
|
18
|
+
export type EcdsaCurve = 'P-256' | 'P-384' | 'P-521';
|
|
19
|
+
export type HashAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512' | 'SHA3-256' | 'SHA3-384' | 'SHA3-512' | 'cSHAKE128' | 'cSHAKE256';
|
|
20
|
+
export type SymmetricAlgorithm = `AES-${AesMode}` | 'ChaCha20-Poly1305' | 'KMAC128' | 'KMAC256';
|
|
21
|
+
export type AsymmetricAlgorithm = 'RSASSA-PKCS1-v1_5' | 'RSA-PSS' | 'RSA-OAEP' | 'ECDSA' | 'ECDH' | 'Ed25519' | 'Ed448' | 'X25519' | 'X448' | 'ML-DSA-44' | 'ML-DSA-65' | 'ML-DSA-87';
|
|
22
|
+
export type KEMAlgorithm = 'ML-KEM-512' | 'ML-KEM-768' | 'ML-KEM-1024';
|
|
23
|
+
export type DerivationAlgorithmName = 'PBKDF2' | 'HKDF' | 'Argon2d' | 'Argon2i' | 'Argon2id' | 'ECDH' | 'X25519' | 'X448';
|
|
24
|
+
export type HashAlgorithmIdentifier = HashAlgorithm | {
|
|
25
|
+
name: HashAlgorithm;
|
|
26
|
+
};
|
|
27
|
+
export interface AeadParams extends SpecificAlgorithm<Algorithm, 'AES-GCM' | 'AES-OCB' | 'ChaCha20-Poly1305'> {
|
|
28
|
+
additionalData?: BufferSource;
|
|
29
|
+
iv: BufferSource;
|
|
30
|
+
tagLength?: number;
|
|
31
|
+
}
|
|
32
|
+
export interface Pbkdf2Params extends SpecificAlgorithm<TypedOmit<globalThis.Pbkdf2Params, 'hash'>, 'PBKDF2'> {
|
|
33
|
+
hash: HashAlgorithmIdentifier;
|
|
34
|
+
}
|
|
35
|
+
export interface HkdfParams extends SpecificAlgorithm<globalThis.HkdfParams, 'HKDF'> {
|
|
36
|
+
}
|
|
37
|
+
export interface Argon2Params extends SpecificAlgorithm<Algorithm, 'Argon2d' | 'Argon2i' | 'Argon2id'> {
|
|
38
|
+
memory: number;
|
|
39
|
+
parallelism: number;
|
|
40
|
+
passes: number;
|
|
41
|
+
nonce: BufferSource;
|
|
42
|
+
secretValue?: BufferSource;
|
|
43
|
+
associatedData?: BufferSource;
|
|
44
|
+
version?: number;
|
|
45
|
+
}
|
|
46
|
+
export interface CShakeParams extends SpecificAlgorithm<Algorithm, 'cSHAKE128' | 'cSHAKE256'> {
|
|
47
|
+
outputLength: number;
|
|
48
|
+
functionName?: BufferSource;
|
|
49
|
+
customization?: BufferSource;
|
|
50
|
+
}
|
|
51
|
+
export interface KmacParams extends SpecificAlgorithm<Algorithm, 'KMAC128' | 'KMAC256'> {
|
|
52
|
+
outputLength: number;
|
|
53
|
+
customization?: BufferSource;
|
|
54
|
+
}
|
|
55
|
+
export interface KmacImportParams extends SpecificAlgorithm<Algorithm, 'KMAC128' | 'KMAC256'> {
|
|
56
|
+
length?: number;
|
|
57
|
+
}
|
|
58
|
+
export interface ContextParams extends SpecificAlgorithm<Algorithm, 'Ed448' | 'ML-DSA-44' | 'ML-DSA-65' | 'ML-DSA-87'> {
|
|
59
|
+
context?: BufferSource;
|
|
60
|
+
}
|
|
61
|
+
export interface AesCbcParams extends SpecificAlgorithm<globalThis.AesCbcParams, 'AES-CBC'> {
|
|
62
|
+
}
|
|
63
|
+
export interface AesCtrParams extends SpecificAlgorithm<globalThis.AesCtrParams, 'AES-CTR'> {
|
|
64
|
+
}
|
|
65
|
+
export interface RsaPssParams extends SpecificAlgorithm<globalThis.RsaPssParams, 'RSA-PSS'> {
|
|
66
|
+
}
|
|
67
|
+
export interface EcdsaParams extends SpecificAlgorithm<globalThis.EcdsaParams, 'ECDSA'> {
|
|
68
|
+
}
|
|
69
|
+
export interface EcdhKeyDeriveParams extends SpecificAlgorithm<globalThis.EcdhKeyDeriveParams, 'ECDH' | 'X25519' | 'X448'> {
|
|
70
|
+
}
|
|
71
|
+
export interface HmacParams extends SpecificAlgorithm<globalThis.HmacImportParams, 'HMAC'> {
|
|
72
|
+
}
|
|
73
|
+
type SpecificAlgorithm<T extends Algorithm, Name extends string> = TypedOmit<T, 'name'> & {
|
|
74
|
+
name: Name;
|
|
75
|
+
};
|
|
76
|
+
export type DigestAlgorithm = HashAlgorithm | CShakeParams;
|
|
77
|
+
export type CryptionAlgorithm = 'AES-CBC' | 'AES-CTR' | 'AES-GCM' | 'AES-KW' | 'AES-OCB' | 'ChaCha20-Poly1305' | AesCbcParams | AesCtrParams | AeadParams | SpecificAlgorithm<Algorithm, 'AES-KW'>;
|
|
78
|
+
export type SignAlgorithm = 'HMAC' | 'RSASSA-PKCS1-v1_5' | 'ECDSA' | 'Ed25519' | 'Ed448' | 'ML-DSA-44' | 'ML-DSA-65' | 'ML-DSA-87' | KmacParams | ContextParams | RsaPssParams | EcdsaParams | HmacParams | SpecificAlgorithm<Algorithm, 'RSASSA-PKCS1-v1_5' | 'Ed25519'>;
|
|
79
|
+
export type DeriveAlgorithm = DerivationAlgorithmName | Argon2Params | Pbkdf2Params | EcdhKeyDeriveParams | HkdfParams;
|
|
80
|
+
export type ImportAlgorithm = RsaHashedImportParams | EcKeyImportParams | HmacImportParams | KmacImportParams | EcdhKeyDeriveParams | (TypedOmit<AesKeyAlgorithm, 'name' | 'length'> & {
|
|
81
|
+
name: SymmetricAlgorithm;
|
|
82
|
+
length?: number;
|
|
83
|
+
}) | TypedOmit<Argon2Params, 'memory' | 'parallelism' | 'passes' | 'nonce'> | TypedOmit<Pbkdf2Params, 'hash' | 'iterations' | 'salt'> | TypedOmit<HkdfParams, 'hash' | 'info' | 'salt'>;
|
|
84
|
+
export type KeyFormat = globalThis.KeyFormat | 'raw-public' | 'raw-secret' | 'raw-seed';
|
|
85
|
+
export type KeyData = JsonWebKey | BufferSource;
|
|
86
|
+
export type ScryptOptions = {
|
|
87
|
+
cost?: number;
|
|
88
|
+
blockSize?: number;
|
|
89
|
+
parallelization?: number;
|
|
90
|
+
maximumMemory?: number;
|
|
91
|
+
};
|
|
92
|
+
export interface CryptionResult {
|
|
93
|
+
toBuffer(): Promise<ArrayBuffer>;
|
|
94
|
+
toUint8Array(): Promise<Uint8Array<ArrayBuffer>>;
|
|
95
|
+
toHex(): Promise<string>;
|
|
96
|
+
toBase64(): Promise<string>;
|
|
97
|
+
toBase64Url(): Promise<string>;
|
|
98
|
+
toZBase32(): Promise<string>;
|
|
99
|
+
}
|
|
100
|
+
export interface DecryptionResult extends CryptionResult {
|
|
101
|
+
toUtf8(): Promise<string>;
|
|
102
|
+
}
|
|
103
|
+
export type DigestResult = CryptionResult;
|
|
104
|
+
export type SignResult = CryptionResult;
|
|
105
|
+
/**
|
|
106
|
+
* Encrypt data
|
|
107
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
108
|
+
* @param key key
|
|
109
|
+
* @param data data to encrypt. Encodes string to utf8
|
|
110
|
+
*/
|
|
111
|
+
export declare function encrypt(algorithm: CryptionAlgorithm, key: CryptoKey, data: BufferSource | string): CryptionResult;
|
|
112
|
+
/**
|
|
113
|
+
* Decrypt data
|
|
114
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
115
|
+
* @param key key
|
|
116
|
+
* @param bytes data to decrypt
|
|
117
|
+
*/
|
|
118
|
+
export declare function decrypt(algorithm: CryptionAlgorithm, key: CryptoKey, bytes: BufferSource): DecryptionResult;
|
|
119
|
+
/**
|
|
120
|
+
* Hashes data
|
|
121
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
122
|
+
* @param data data to encrypt. Encodes string to utf8
|
|
123
|
+
*/
|
|
124
|
+
export declare function digest(algorithm: DigestAlgorithm, data: BufferSource | string): DigestResult;
|
|
125
|
+
/**
|
|
126
|
+
* Signs data
|
|
127
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
128
|
+
* @param key key
|
|
129
|
+
* @param data data to sign
|
|
130
|
+
*/
|
|
131
|
+
export declare function sign(algorithm: SignAlgorithm, key: CryptoKey, data: BufferSource | string): SignResult;
|
|
132
|
+
/**
|
|
133
|
+
* Verifies data
|
|
134
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
135
|
+
* @param key key
|
|
136
|
+
* @param signature signature
|
|
137
|
+
* @param data data to verify using provided signature
|
|
138
|
+
*/
|
|
139
|
+
export declare function verify(algorithm: SignAlgorithm, key: CryptoKey, signature: BufferSource | string, data: BufferSource | string): Promise<boolean>;
|
|
140
|
+
/**
|
|
141
|
+
* Import a key
|
|
142
|
+
* @param format format of the key
|
|
143
|
+
* @param keyData key data
|
|
144
|
+
* @param algorithm algorithm
|
|
145
|
+
* @param extractable whether the key can be used for exportKey
|
|
146
|
+
* @param keyUsages usages for the key
|
|
147
|
+
*/
|
|
148
|
+
export declare function importKey(format: 'jwk', keyData: JsonWebKey, algorithm: ImportAlgorithm, extractable: boolean, keyUsages: readonly KeyUsage[]): Promise<CryptoKey>;
|
|
149
|
+
export declare function importKey(format: Exclude<KeyFormat, 'jwk'>, keyData: BufferSource, algorithm: ImportAlgorithm, extractable: boolean, keyUsages: readonly KeyUsage[]): Promise<CryptoKey>;
|
|
150
|
+
/**
|
|
151
|
+
* Exports a key
|
|
152
|
+
* @param format format to export to
|
|
153
|
+
* @param key key to export
|
|
154
|
+
*/
|
|
155
|
+
export declare function exportKey(format: 'jwk', key: CryptoKey): Promise<JsonWebKey>;
|
|
156
|
+
export declare function exportKey(format: Exclude<KeyFormat, 'jwk'>, key: CryptoKey): Promise<ArrayBuffer>;
|
|
157
|
+
/**
|
|
158
|
+
* Wraps a key
|
|
159
|
+
* @param format format to export the key in
|
|
160
|
+
* @param key key to wrap
|
|
161
|
+
* @param wrappingKey key to use for wrapping
|
|
162
|
+
* @param wrapAlgorithm algorithm to use for wrapping
|
|
163
|
+
*/
|
|
164
|
+
export declare function wrapKey(format: KeyFormat, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: CryptionAlgorithm): Promise<ArrayBuffer>;
|
|
165
|
+
/**
|
|
166
|
+
* Unwraps a key
|
|
167
|
+
* @param format format of the wrapped key
|
|
168
|
+
* @param wrappedKey wrapped key
|
|
169
|
+
* @param unwrappingKey key to use for unwrapping
|
|
170
|
+
* @param unwrapAlgorithm algorithm to use for unwrapping
|
|
171
|
+
* @param unwrappedKeyAlgorithm algorithm of the unwrapped key
|
|
172
|
+
* @param extractable whether the unwrapped key can be used for exportKey
|
|
173
|
+
* @param usages usages for the unwrapped key
|
|
174
|
+
*/
|
|
175
|
+
export declare function unwrapKey(format: KeyFormat, wrappedKey: BufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: CryptionAlgorithm, unwrappedKeyAlgorithm: ImportAlgorithm, extractable: boolean, usages: KeyUsage[]): Promise<CryptoKey>;
|
|
176
|
+
/**
|
|
177
|
+
* Imports a HMAC CryptoKey
|
|
178
|
+
* @param algorithm hash algorithm
|
|
179
|
+
* @param key JWK or binary key
|
|
180
|
+
* @param extractable whether the key can be used for exportKey
|
|
181
|
+
*/
|
|
182
|
+
export declare function importHmacKey(format: KeyFormat, algorithm: HashAlgorithmIdentifier, keyData: KeyData, extractable?: boolean): Promise<CryptoKey>;
|
|
183
|
+
/**
|
|
184
|
+
* Imports a KMAC CryptoKey
|
|
185
|
+
* @param algorithm KMAC algorithm
|
|
186
|
+
* @param key JWK or binary key
|
|
187
|
+
* @param extractable whether the key can be used for exportKey
|
|
188
|
+
*/
|
|
189
|
+
export declare function importKmacKey(format: KeyFormat, algorithm: 'KMAC128' | 'KMAC256', keyData: KeyData, extractable?: boolean): Promise<CryptoKey>;
|
|
190
|
+
/**
|
|
191
|
+
* Imports a CryptoKey for symmetric encryption
|
|
192
|
+
* @param algorithm symmetric algorithm or algorithm object with length
|
|
193
|
+
* @param key JWK or binary key
|
|
194
|
+
* @param extractable whether the key can be used for exportKey
|
|
195
|
+
* @param format optional format
|
|
196
|
+
*/
|
|
197
|
+
export declare function importSymmetricKey(format: KeyFormat, algorithm: {
|
|
198
|
+
name: SymmetricAlgorithm;
|
|
199
|
+
length?: number;
|
|
200
|
+
}, keyData: KeyData, extractable?: boolean): Promise<CryptoKey>;
|
|
201
|
+
/**
|
|
202
|
+
* Imports an ECDSA CryptoKey
|
|
203
|
+
* @param curve ECDSA curve
|
|
204
|
+
* @param key JWK or DER encoded key
|
|
205
|
+
* @param extractable whether the key can be used for exportKey
|
|
206
|
+
*/
|
|
207
|
+
export declare function importEcdsaKey(format: KeyFormat, curve: EcdsaCurve, keyData: KeyData, extractable?: boolean): Promise<CryptoKey>;
|
|
208
|
+
/**
|
|
209
|
+
* Import a HKDF CryptoKey
|
|
210
|
+
* @param keyData binary key
|
|
211
|
+
* @param extractable whether the key can be used for exportKey
|
|
212
|
+
*/
|
|
213
|
+
export declare function importHkdfKey(format: KeyFormat, keyData: KeyData, extractable?: boolean): Promise<CryptoKey>;
|
|
214
|
+
/**
|
|
215
|
+
* Import a pbkdf2 CryptoKey
|
|
216
|
+
* @param keyData binary key
|
|
217
|
+
* @param extractable whether the key can be used for exportKey
|
|
218
|
+
*/
|
|
219
|
+
export declare function importPbkdf2Key(format: KeyFormat, keyData: KeyData, extractable?: boolean): Promise<CryptoKey>;
|
|
220
|
+
/**
|
|
221
|
+
* Generates a HMAC CryptoKey
|
|
222
|
+
* @param algorithm hash algorithm
|
|
223
|
+
* @param extractable whether the key can be used for exportKey
|
|
224
|
+
*/
|
|
225
|
+
export declare function generateHmacKey(algorithm: HashAlgorithmIdentifier, extractable?: boolean): Promise<CryptoKey>;
|
|
226
|
+
/**
|
|
227
|
+
* Generates a KMAC CryptoKey
|
|
228
|
+
* @param algorithm KMAC algorithm
|
|
229
|
+
* @param extractable whether the key can be used for exportKey
|
|
230
|
+
*/
|
|
231
|
+
export declare function generateKmacKey(algorithm: 'KMAC128' | 'KMAC256', extractable?: boolean): Promise<CryptoKey>;
|
|
232
|
+
/**
|
|
233
|
+
* Generates a CryptoKey for symmetric encryption
|
|
234
|
+
* @param algorithm symmetric algorithm or algorithm object with length
|
|
235
|
+
* @param extractable whether the key can be used for exportKey
|
|
236
|
+
*/
|
|
237
|
+
export declare function generateSymmetricKey(algorithm: SymmetricAlgorithm | {
|
|
238
|
+
name: SymmetricAlgorithm;
|
|
239
|
+
length?: number;
|
|
240
|
+
}, extractable?: boolean): Promise<CryptoKey>;
|
|
241
|
+
/**
|
|
242
|
+
* Generates a new ECDSA CryptoKeyPair
|
|
243
|
+
* @param curve ECDSA cruve to use
|
|
244
|
+
* @param extractable whether the key can be used for exportKey
|
|
245
|
+
* @param usages whether to generate a key for signing, verifiying or both. Defaults to both
|
|
246
|
+
*/
|
|
247
|
+
export declare function generateEcdsaKey(curve: EcdsaCurve, extractable?: boolean, usages?: TypedExtract<KeyUsage, 'sign' | 'verify'>[]): Promise<CryptoKeyPair>;
|
|
248
|
+
/**
|
|
249
|
+
* Generates a pbkdf2 CryptoKey
|
|
250
|
+
* @param extractable whether the key can be used for exportKey
|
|
251
|
+
*/
|
|
252
|
+
export declare function generatePbkdf2Key(extractable?: boolean): Promise<CryptoKey>;
|
|
253
|
+
/**
|
|
254
|
+
* Derive byte array from key
|
|
255
|
+
* @param algorithm algorithm to derive with
|
|
256
|
+
* @param baseKey key to derive from
|
|
257
|
+
* @param length length in bytes
|
|
258
|
+
*/
|
|
259
|
+
export declare function deriveBytes(algorithm: DeriveAlgorithm, baseKey: CryptoKey, length: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
260
|
+
/**
|
|
261
|
+
* Derive CryptoKey from key
|
|
262
|
+
* @param algorithm algorithm to derive with
|
|
263
|
+
* @param baseKey key to derive from
|
|
264
|
+
* @param derivedKeyAlgorithm algorithm of the derived key
|
|
265
|
+
* @param extractable whether the derived key can be used for exportKey
|
|
266
|
+
* @param keyUsages usages for the derived key
|
|
267
|
+
*/
|
|
268
|
+
export declare function deriveKey(algorithm: DeriveAlgorithm, baseKey: CryptoKey, derivedKeyAlgorithm: ImportAlgorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
269
|
+
/**
|
|
270
|
+
* Encapsulates a key using KEM
|
|
271
|
+
* @param algorithm KEM algorithm
|
|
272
|
+
* @param publicKey public key
|
|
273
|
+
* @param sharedKeyAlgorithm algorithm for the shared key
|
|
274
|
+
* @param extractable whether the shared key can be used for exportKey
|
|
275
|
+
* @param usages usages for the shared key
|
|
276
|
+
*/
|
|
277
|
+
export declare function encapsulateKey(algorithm: KEMAlgorithm | {
|
|
278
|
+
name: KEMAlgorithm;
|
|
279
|
+
}, publicKey: CryptoKey, sharedKeyAlgorithm: HmacImportParams | AesKeyAlgorithm | KmacImportParams, extractable: boolean, usages: KeyUsage[]): Promise<{
|
|
280
|
+
ciphertext: ArrayBuffer;
|
|
281
|
+
sharedKey: CryptoKey;
|
|
282
|
+
}>;
|
|
283
|
+
/**
|
|
284
|
+
* Decapsulates a key using KEM
|
|
285
|
+
* @param algorithm KEM algorithm
|
|
286
|
+
* @param privateKey private key
|
|
287
|
+
* @param ciphertext ciphertext
|
|
288
|
+
* @param sharedKeyAlgorithm algorithm for the shared key
|
|
289
|
+
* @param extractable whether the shared key can be used for exportKey
|
|
290
|
+
* @param usages usages for the shared key
|
|
291
|
+
*/
|
|
292
|
+
export declare function decapsulateKey(algorithm: KEMAlgorithm | {
|
|
293
|
+
name: KEMAlgorithm;
|
|
294
|
+
}, privateKey: CryptoKey, ciphertext: ArrayBuffer, sharedKeyAlgorithm: HmacImportParams | AesKeyAlgorithm | KmacImportParams, extractable: boolean, usages: KeyUsage[]): Promise<CryptoKey>;
|
|
295
|
+
/**
|
|
296
|
+
* Encapsulates bits using KEM
|
|
297
|
+
* @param algorithm KEM algorithm
|
|
298
|
+
* @param publicKey public key
|
|
299
|
+
*/
|
|
300
|
+
export declare function encapsulateBits(algorithm: KEMAlgorithm | {
|
|
301
|
+
name: KEMAlgorithm;
|
|
302
|
+
}, publicKey: CryptoKey): Promise<{
|
|
303
|
+
ciphertext: ArrayBuffer;
|
|
304
|
+
sharedKey: ArrayBuffer;
|
|
305
|
+
}>;
|
|
306
|
+
/**
|
|
307
|
+
* Decapsulates bits using KEM
|
|
308
|
+
* @param algorithm KEM algorithm
|
|
309
|
+
* @param privateKey private key
|
|
310
|
+
* @param ciphertext ciphertext
|
|
311
|
+
*/
|
|
312
|
+
export declare function decapsulateBits(algorithm: KEMAlgorithm | {
|
|
313
|
+
name: KEMAlgorithm;
|
|
314
|
+
}, privateKey: CryptoKey, ciphertext: ArrayBuffer): Promise<ArrayBuffer>;
|
|
315
|
+
/**
|
|
316
|
+
* Gets the public key from a private key or a key pair
|
|
317
|
+
* @param key private key or key pair
|
|
318
|
+
* @param keyUsages usages for the new public key
|
|
319
|
+
*/
|
|
320
|
+
export declare function getPublicKey(key: CryptoKey, keyUsages: KeyUsage[]): Promise<CryptoKey>;
|
|
321
|
+
/**
|
|
322
|
+
* Check if the runtime supports a specific operation and algorithm
|
|
323
|
+
* @param operation operation to check
|
|
324
|
+
* @param algorithm algorithm to check
|
|
325
|
+
* @param lengthOrAdditionalAlgorithm optional length or additional algorithm
|
|
326
|
+
*/
|
|
327
|
+
export declare function supports(operation: string, algorithm: string | (Algorithm & Record<string, unknown>), lengthOrAdditionalAlgorithm?: number | string | Algorithm | null): Promise<boolean>;
|
|
328
|
+
/**
|
|
329
|
+
* Generates an asymmetric CryptoKeyPair
|
|
330
|
+
* @param algorithm asymmetric algorithm
|
|
331
|
+
* @param extractable whether the key can be used for exportKey
|
|
332
|
+
* @param usages usages for the key
|
|
333
|
+
*/
|
|
334
|
+
export declare function generateAsymmetricKey(algorithm: AsymmetricAlgorithm | RsaHashedKeyGenParams | EcKeyGenParams, extractable?: boolean, usages?: KeyUsage[]): Promise<CryptoKeyPair>;
|
|
335
|
+
export declare function isBufferSource(keyData: KeyData | string): keyData is BufferSource;
|
|
336
|
+
export {};
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { encodeBase64, encodeBase64Url } from '../utils/base64.js';
|
|
2
|
+
import { decodeText, encodeHex, encodeUtf8 } from '../utils/encoding.js';
|
|
3
|
+
import { getRandomBytes } from '../utils/random.js';
|
|
4
|
+
import { isNotString, isNumber, isString } from '../utils/type-guards.js';
|
|
5
|
+
import { zBase32Encode } from '../utils/z-base32.js';
|
|
6
|
+
/**
|
|
7
|
+
* Encrypt data
|
|
8
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
9
|
+
* @param key key
|
|
10
|
+
* @param data data to encrypt. Encodes string to utf8
|
|
11
|
+
*/
|
|
12
|
+
export function encrypt(algorithm, key, data) {
|
|
13
|
+
const bytes = isString(data) ? encodeUtf8(data) : data;
|
|
14
|
+
const encryptedBuffer = globalThis.crypto.subtle.encrypt(algorithm, key, bytes);
|
|
15
|
+
return {
|
|
16
|
+
toBuffer: async () => await encryptedBuffer,
|
|
17
|
+
toUint8Array: async () => new Uint8Array(await encryptedBuffer),
|
|
18
|
+
toHex: async () => encodeHex(await encryptedBuffer),
|
|
19
|
+
toBase64: async () => encodeBase64(await encryptedBuffer),
|
|
20
|
+
toBase64Url: async () => encodeBase64Url(await encryptedBuffer),
|
|
21
|
+
toZBase32: async () => zBase32Encode(await encryptedBuffer),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Decrypt data
|
|
26
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
27
|
+
* @param key key
|
|
28
|
+
* @param bytes data to decrypt
|
|
29
|
+
*/
|
|
30
|
+
export function decrypt(algorithm, key, bytes) {
|
|
31
|
+
const decryptedBuffer = globalThis.crypto.subtle.decrypt(algorithm, key, bytes);
|
|
32
|
+
return {
|
|
33
|
+
toBuffer: async () => await decryptedBuffer,
|
|
34
|
+
toUint8Array: async () => new Uint8Array(await decryptedBuffer),
|
|
35
|
+
toHex: async () => encodeHex(await decryptedBuffer),
|
|
36
|
+
toBase64: async () => encodeBase64(await decryptedBuffer),
|
|
37
|
+
toBase64Url: async () => encodeBase64Url(await decryptedBuffer),
|
|
38
|
+
toZBase32: async () => zBase32Encode(await decryptedBuffer),
|
|
39
|
+
toUtf8: async () => decodeText(await decryptedBuffer),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Hashes data
|
|
44
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
45
|
+
* @param data data to encrypt. Encodes string to utf8
|
|
46
|
+
*/
|
|
47
|
+
export function digest(algorithm, data) {
|
|
48
|
+
const bytes = isString(data) ? encodeUtf8(data) : data;
|
|
49
|
+
const arrayBufferPromise = globalThis.crypto.subtle.digest(algorithm, bytes);
|
|
50
|
+
const result = {
|
|
51
|
+
toBuffer: async () => await arrayBufferPromise,
|
|
52
|
+
toUint8Array: async () => new Uint8Array(await arrayBufferPromise),
|
|
53
|
+
toHex: async () => encodeHex(await arrayBufferPromise),
|
|
54
|
+
toBase64: async () => encodeBase64(await arrayBufferPromise),
|
|
55
|
+
toBase64Url: async () => encodeBase64Url(await arrayBufferPromise),
|
|
56
|
+
toZBase32: async () => zBase32Encode(await arrayBufferPromise),
|
|
57
|
+
};
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Signs data
|
|
62
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
63
|
+
* @param key key
|
|
64
|
+
* @param data data to sign
|
|
65
|
+
*/
|
|
66
|
+
export function sign(algorithm, key, data) {
|
|
67
|
+
const bytes = isString(data) ? encodeUtf8(data) : data;
|
|
68
|
+
// TODO: TEMPORARY WORKAROUND for Node.js versions prior to PR #61875
|
|
69
|
+
let finalAlgorithm = algorithm;
|
|
70
|
+
if (isNotString(algorithm) && algorithm.name.startsWith('KMAC') && ('outputLength' in algorithm)) {
|
|
71
|
+
finalAlgorithm = {
|
|
72
|
+
...algorithm,
|
|
73
|
+
// Older Node.js versions require 'length' in bits instead of 'outputLength' in bytes
|
|
74
|
+
length: algorithm.outputLength * 8,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const arrayBufferPromise = globalThis.crypto.subtle.sign(finalAlgorithm, key, bytes);
|
|
78
|
+
const result = {
|
|
79
|
+
toBuffer: async () => await arrayBufferPromise,
|
|
80
|
+
toUint8Array: async () => new Uint8Array(await arrayBufferPromise),
|
|
81
|
+
toHex: async () => encodeHex(await arrayBufferPromise),
|
|
82
|
+
toBase64: async () => encodeBase64(await arrayBufferPromise),
|
|
83
|
+
toBase64Url: async () => encodeBase64Url(await arrayBufferPromise),
|
|
84
|
+
toZBase32: async () => zBase32Encode(await arrayBufferPromise),
|
|
85
|
+
};
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Verifies data
|
|
90
|
+
* @param algorithm algorithm as supported by Web Crypto API
|
|
91
|
+
* @param key key
|
|
92
|
+
* @param signature signature
|
|
93
|
+
* @param data data to verify using provided signature
|
|
94
|
+
*/
|
|
95
|
+
export async function verify(algorithm, key, signature, data) {
|
|
96
|
+
const signatureBytes = isString(signature) ? encodeUtf8(signature) : signature;
|
|
97
|
+
const dataBytes = isString(data) ? encodeUtf8(data) : data;
|
|
98
|
+
// TODO: TEMPORARY WORKAROUND for Node.js versions prior to PR #61875
|
|
99
|
+
let finalAlgorithm = algorithm;
|
|
100
|
+
if (isNotString(algorithm) && algorithm.name.startsWith('KMAC') && ('outputLength' in algorithm)) {
|
|
101
|
+
finalAlgorithm = {
|
|
102
|
+
...algorithm,
|
|
103
|
+
// Older Node.js versions require 'length' in bits instead of 'outputLength' in bytes
|
|
104
|
+
length: algorithm.outputLength * 8,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return await globalThis.crypto.subtle.verify(finalAlgorithm, key, signatureBytes, dataBytes);
|
|
108
|
+
}
|
|
109
|
+
export async function importKey(format, keyData, algorithm, extractable, keyUsages) {
|
|
110
|
+
return await globalThis.crypto.subtle.importKey(format, keyData, algorithm, extractable, keyUsages);
|
|
111
|
+
}
|
|
112
|
+
export async function exportKey(format, key) {
|
|
113
|
+
return await globalThis.crypto.subtle.exportKey(format, key);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Wraps a key
|
|
117
|
+
* @param format format to export the key in
|
|
118
|
+
* @param key key to wrap
|
|
119
|
+
* @param wrappingKey key to use for wrapping
|
|
120
|
+
* @param wrapAlgorithm algorithm to use for wrapping
|
|
121
|
+
*/
|
|
122
|
+
export async function wrapKey(format, key, wrappingKey, wrapAlgorithm) {
|
|
123
|
+
return await globalThis.crypto.subtle.wrapKey(format, key, wrappingKey, wrapAlgorithm);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Unwraps a key
|
|
127
|
+
* @param format format of the wrapped key
|
|
128
|
+
* @param wrappedKey wrapped key
|
|
129
|
+
* @param unwrappingKey key to use for unwrapping
|
|
130
|
+
* @param unwrapAlgorithm algorithm to use for unwrapping
|
|
131
|
+
* @param unwrappedKeyAlgorithm algorithm of the unwrapped key
|
|
132
|
+
* @param extractable whether the unwrapped key can be used for exportKey
|
|
133
|
+
* @param usages usages for the unwrapped key
|
|
134
|
+
*/
|
|
135
|
+
export async function unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, usages) {
|
|
136
|
+
return await globalThis.crypto.subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, usages);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Imports a HMAC CryptoKey
|
|
140
|
+
* @param algorithm hash algorithm
|
|
141
|
+
* @param key JWK or binary key
|
|
142
|
+
* @param extractable whether the key can be used for exportKey
|
|
143
|
+
*/
|
|
144
|
+
export async function importHmacKey(format, algorithm, keyData, extractable = false) {
|
|
145
|
+
return await importKey(format, keyData, { name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Imports a KMAC CryptoKey
|
|
149
|
+
* @param algorithm KMAC algorithm
|
|
150
|
+
* @param key JWK or binary key
|
|
151
|
+
* @param extractable whether the key can be used for exportKey
|
|
152
|
+
*/
|
|
153
|
+
export async function importKmacKey(format, algorithm, keyData, extractable = false) {
|
|
154
|
+
return await importKey(format, keyData, { name: algorithm }, extractable, ['sign', 'verify']);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Imports a CryptoKey for symmetric encryption
|
|
158
|
+
* @param algorithm symmetric algorithm or algorithm object with length
|
|
159
|
+
* @param key JWK or binary key
|
|
160
|
+
* @param extractable whether the key can be used for exportKey
|
|
161
|
+
* @param format optional format
|
|
162
|
+
*/
|
|
163
|
+
export async function importSymmetricKey(format, algorithm, keyData, extractable = false) {
|
|
164
|
+
const importAlgorithm = isString(algorithm) ? { name: algorithm } : algorithm;
|
|
165
|
+
const usages = importAlgorithm.name.startsWith('KMAC') ? ['sign', 'verify'] : ['encrypt', 'decrypt'];
|
|
166
|
+
return await importKey(format, keyData, importAlgorithm, extractable, usages);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Imports an ECDSA CryptoKey
|
|
170
|
+
* @param curve ECDSA curve
|
|
171
|
+
* @param key JWK or DER encoded key
|
|
172
|
+
* @param extractable whether the key can be used for exportKey
|
|
173
|
+
*/
|
|
174
|
+
export async function importEcdsaKey(format, curve, keyData, extractable = false) {
|
|
175
|
+
return await importKey(format, keyData, { name: 'ECDSA', namedCurve: curve }, extractable, ['verify']);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Import a HKDF CryptoKey
|
|
179
|
+
* @param keyData binary key
|
|
180
|
+
* @param extractable whether the key can be used for exportKey
|
|
181
|
+
*/
|
|
182
|
+
export async function importHkdfKey(format, keyData, extractable = false) {
|
|
183
|
+
return await importKey(format, keyData, { name: 'HKDF' }, extractable, ['deriveKey', 'deriveBits']);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Import a pbkdf2 CryptoKey
|
|
187
|
+
* @param keyData binary key
|
|
188
|
+
* @param extractable whether the key can be used for exportKey
|
|
189
|
+
*/
|
|
190
|
+
export async function importPbkdf2Key(format, keyData, extractable = false) {
|
|
191
|
+
return await importKey(format, keyData, { name: 'PBKDF2' }, extractable, ['deriveKey', 'deriveBits']);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Generates a HMAC CryptoKey
|
|
195
|
+
* @param algorithm hash algorithm
|
|
196
|
+
* @param extractable whether the key can be used for exportKey
|
|
197
|
+
*/
|
|
198
|
+
export async function generateHmacKey(algorithm, extractable = false) {
|
|
199
|
+
return await globalThis.crypto.subtle.generateKey({ name: 'HMAC', hash: algorithm }, extractable, ['sign', 'verify']);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Generates a KMAC CryptoKey
|
|
203
|
+
* @param algorithm KMAC algorithm
|
|
204
|
+
* @param extractable whether the key can be used for exportKey
|
|
205
|
+
*/
|
|
206
|
+
export async function generateKmacKey(algorithm, extractable = false) {
|
|
207
|
+
return await globalThis.crypto.subtle.generateKey({ name: algorithm }, extractable, ['sign', 'verify']);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Generates a CryptoKey for symmetric encryption
|
|
211
|
+
* @param algorithm symmetric algorithm or algorithm object with length
|
|
212
|
+
* @param extractable whether the key can be used for exportKey
|
|
213
|
+
*/
|
|
214
|
+
export async function generateSymmetricKey(algorithm, extractable = false) {
|
|
215
|
+
const params = isString(algorithm) ? { name: algorithm } : algorithm;
|
|
216
|
+
const usages = params.name.startsWith('KMAC') ? ['sign', 'verify'] : ['encrypt', 'decrypt'];
|
|
217
|
+
return await globalThis.crypto.subtle.generateKey(params, extractable, usages);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Generates a new ECDSA CryptoKeyPair
|
|
221
|
+
* @param curve ECDSA cruve to use
|
|
222
|
+
* @param extractable whether the key can be used for exportKey
|
|
223
|
+
* @param usages whether to generate a key for signing, verifiying or both. Defaults to both
|
|
224
|
+
*/
|
|
225
|
+
export async function generateEcdsaKey(curve, extractable = false, usages = ['sign', 'verify']) {
|
|
226
|
+
return await globalThis.crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: curve }, extractable, usages);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Generates a pbkdf2 CryptoKey
|
|
230
|
+
* @param extractable whether the key can be used for exportKey
|
|
231
|
+
*/
|
|
232
|
+
export async function generatePbkdf2Key(extractable = false) {
|
|
233
|
+
const key = getRandomBytes(16);
|
|
234
|
+
return await importPbkdf2Key('raw', key, extractable);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Derive byte array from key
|
|
238
|
+
* @param algorithm algorithm to derive with
|
|
239
|
+
* @param baseKey key to derive from
|
|
240
|
+
* @param length length in bytes
|
|
241
|
+
*/
|
|
242
|
+
export async function deriveBytes(algorithm, baseKey, length) {
|
|
243
|
+
const bytes = await globalThis.crypto.subtle.deriveBits(algorithm, baseKey, length * 8);
|
|
244
|
+
return new Uint8Array(bytes);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Derive CryptoKey from key
|
|
248
|
+
* @param algorithm algorithm to derive with
|
|
249
|
+
* @param baseKey key to derive from
|
|
250
|
+
* @param derivedKeyAlgorithm algorithm of the derived key
|
|
251
|
+
* @param extractable whether the derived key can be used for exportKey
|
|
252
|
+
* @param keyUsages usages for the derived key
|
|
253
|
+
*/
|
|
254
|
+
export async function deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages) {
|
|
255
|
+
return await globalThis.crypto.subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Encapsulates a key using KEM
|
|
259
|
+
* @param algorithm KEM algorithm
|
|
260
|
+
* @param publicKey public key
|
|
261
|
+
* @param sharedKeyAlgorithm algorithm for the shared key
|
|
262
|
+
* @param extractable whether the shared key can be used for exportKey
|
|
263
|
+
* @param usages usages for the shared key
|
|
264
|
+
*/
|
|
265
|
+
export async function encapsulateKey(algorithm, publicKey, sharedKeyAlgorithm, extractable, usages) {
|
|
266
|
+
return await globalThis.crypto.subtle.encapsulateKey(algorithm, publicKey, sharedKeyAlgorithm, extractable, usages);
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Decapsulates a key using KEM
|
|
270
|
+
* @param algorithm KEM algorithm
|
|
271
|
+
* @param privateKey private key
|
|
272
|
+
* @param ciphertext ciphertext
|
|
273
|
+
* @param sharedKeyAlgorithm algorithm for the shared key
|
|
274
|
+
* @param extractable whether the shared key can be used for exportKey
|
|
275
|
+
* @param usages usages for the shared key
|
|
276
|
+
*/
|
|
277
|
+
export async function decapsulateKey(algorithm, privateKey, ciphertext, sharedKeyAlgorithm, extractable, usages) {
|
|
278
|
+
return await globalThis.crypto.subtle.decapsulateKey(algorithm, privateKey, ciphertext, sharedKeyAlgorithm, extractable, usages);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Encapsulates bits using KEM
|
|
282
|
+
* @param algorithm KEM algorithm
|
|
283
|
+
* @param publicKey public key
|
|
284
|
+
*/
|
|
285
|
+
export async function encapsulateBits(algorithm, publicKey) {
|
|
286
|
+
return await globalThis.crypto.subtle.encapsulateBits(algorithm, publicKey);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Decapsulates bits using KEM
|
|
290
|
+
* @param algorithm KEM algorithm
|
|
291
|
+
* @param privateKey private key
|
|
292
|
+
* @param ciphertext ciphertext
|
|
293
|
+
*/
|
|
294
|
+
export async function decapsulateBits(algorithm, privateKey, ciphertext) {
|
|
295
|
+
return await globalThis.crypto.subtle.decapsulateBits(algorithm, privateKey, ciphertext);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Gets the public key from a private key or a key pair
|
|
299
|
+
* @param key private key or key pair
|
|
300
|
+
* @param keyUsages usages for the new public key
|
|
301
|
+
*/
|
|
302
|
+
export async function getPublicKey(key, keyUsages) {
|
|
303
|
+
return await globalThis.crypto.subtle.getPublicKey(key, keyUsages);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Check if the runtime supports a specific operation and algorithm
|
|
307
|
+
* @param operation operation to check
|
|
308
|
+
* @param algorithm algorithm to check
|
|
309
|
+
* @param lengthOrAdditionalAlgorithm optional length or additional algorithm
|
|
310
|
+
*/
|
|
311
|
+
export async function supports(operation, algorithm, lengthOrAdditionalAlgorithm) {
|
|
312
|
+
if (('SubtleCrypto' in globalThis) && ('supports' in globalThis.SubtleCrypto)) {
|
|
313
|
+
return await globalThis.SubtleCrypto.supports(operation, algorithm, lengthOrAdditionalAlgorithm);
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Generates an asymmetric CryptoKeyPair
|
|
319
|
+
* @param algorithm asymmetric algorithm
|
|
320
|
+
* @param extractable whether the key can be used for exportKey
|
|
321
|
+
* @param usages usages for the key
|
|
322
|
+
*/
|
|
323
|
+
export async function generateAsymmetricKey(algorithm, extractable = false, usages = []) {
|
|
324
|
+
return await globalThis.crypto.subtle.generateKey(algorithm, extractable, usages);
|
|
325
|
+
}
|
|
326
|
+
export function isBufferSource(keyData) {
|
|
327
|
+
return isNumber(keyData.byteLength);
|
|
328
|
+
}
|