@enbox/crypto 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +708 -0
- package/dist/browser.js +64 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +64 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/algorithms/aes-ctr.js +188 -0
- package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
- package/dist/cjs/algorithms/aes-gcm.js +196 -0
- package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
- package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
- package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/cjs/algorithms/ecdsa.js +352 -0
- package/dist/cjs/algorithms/ecdsa.js.map +1 -0
- package/dist/cjs/algorithms/eddsa.js +325 -0
- package/dist/cjs/algorithms/eddsa.js.map +1 -0
- package/dist/cjs/algorithms/sha-2.js +119 -0
- package/dist/cjs/algorithms/sha-2.js.map +1 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jose/jwe.js +3 -0
- package/dist/cjs/jose/jwe.js.map +1 -0
- package/dist/cjs/jose/jwk.js +278 -0
- package/dist/cjs/jose/jwk.js.map +1 -0
- package/dist/cjs/jose/jws.js +3 -0
- package/dist/cjs/jose/jws.js.map +1 -0
- package/dist/cjs/jose/jwt.js +3 -0
- package/dist/cjs/jose/jwt.js.map +1 -0
- package/dist/cjs/jose/utils.js +60 -0
- package/dist/cjs/jose/utils.js.map +1 -0
- package/dist/cjs/local-key-manager.js +521 -0
- package/dist/cjs/local-key-manager.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/primitives/aes-ctr.js +398 -0
- package/dist/cjs/primitives/aes-ctr.js.map +1 -0
- package/dist/cjs/primitives/aes-gcm.js +425 -0
- package/dist/cjs/primitives/aes-gcm.js.map +1 -0
- package/dist/cjs/primitives/concat-kdf.js +215 -0
- package/dist/cjs/primitives/concat-kdf.js.map +1 -0
- package/dist/cjs/primitives/ed25519.js +651 -0
- package/dist/cjs/primitives/ed25519.js.map +1 -0
- package/dist/cjs/primitives/pbkdf2.js +120 -0
- package/dist/cjs/primitives/pbkdf2.js.map +1 -0
- package/dist/cjs/primitives/secp256k1.js +958 -0
- package/dist/cjs/primitives/secp256k1.js.map +1 -0
- package/dist/cjs/primitives/secp256r1.js +959 -0
- package/dist/cjs/primitives/secp256r1.js.map +1 -0
- package/dist/cjs/primitives/sha256.js +93 -0
- package/dist/cjs/primitives/sha256.js.map +1 -0
- package/dist/cjs/primitives/x25519.js +498 -0
- package/dist/cjs/primitives/x25519.js.map +1 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/cjs/primitives/xchacha20.js +316 -0
- package/dist/cjs/primitives/xchacha20.js.map +1 -0
- package/dist/cjs/types/cipher.js +3 -0
- package/dist/cjs/types/cipher.js.map +1 -0
- package/dist/cjs/types/crypto-api.js +3 -0
- package/dist/cjs/types/crypto-api.js.map +1 -0
- package/dist/cjs/types/hasher.js +3 -0
- package/dist/cjs/types/hasher.js.map +1 -0
- package/dist/cjs/types/identifier.js +3 -0
- package/dist/cjs/types/identifier.js.map +1 -0
- package/dist/cjs/types/key-compressor.js +3 -0
- package/dist/cjs/types/key-compressor.js.map +1 -0
- package/dist/cjs/types/key-converter.js +3 -0
- package/dist/cjs/types/key-converter.js.map +1 -0
- package/dist/cjs/types/key-deriver.js +3 -0
- package/dist/cjs/types/key-deriver.js.map +1 -0
- package/dist/cjs/types/key-generator.js +3 -0
- package/dist/cjs/types/key-generator.js.map +1 -0
- package/dist/cjs/types/key-io.js +3 -0
- package/dist/cjs/types/key-io.js.map +1 -0
- package/dist/cjs/types/key-wrapper.js +3 -0
- package/dist/cjs/types/key-wrapper.js.map +1 -0
- package/dist/cjs/types/params-direct.js +3 -0
- package/dist/cjs/types/params-direct.js.map +1 -0
- package/dist/cjs/types/params-enclosed.js +3 -0
- package/dist/cjs/types/params-enclosed.js.map +1 -0
- package/dist/cjs/types/params-kms.js +3 -0
- package/dist/cjs/types/params-kms.js.map +1 -0
- package/dist/cjs/types/signer.js +3 -0
- package/dist/cjs/types/signer.js.map +1 -0
- package/dist/cjs/utils.js +173 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/algorithms/aes-ctr.js +124 -0
- package/dist/esm/algorithms/aes-ctr.js.map +1 -0
- package/dist/esm/algorithms/aes-gcm.js +132 -0
- package/dist/esm/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/algorithms/crypto-algorithm.js +6 -0
- package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +237 -0
- package/dist/esm/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/algorithms/eddsa.js +213 -0
- package/dist/esm/algorithms/eddsa.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +57 -0
- package/dist/esm/algorithms/sha-2.js.map +1 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jose/jwe.js +2 -0
- package/dist/esm/jose/jwe.js.map +1 -0
- package/dist/esm/jose/jwk.js +241 -0
- package/dist/esm/jose/jwk.js.map +1 -0
- package/dist/esm/jose/jws.js +2 -0
- package/dist/esm/jose/jws.js.map +1 -0
- package/dist/esm/jose/jwt.js +2 -0
- package/dist/esm/jose/jwt.js.map +1 -0
- package/dist/esm/jose/utils.js +34 -0
- package/dist/esm/jose/utils.js.map +1 -0
- package/dist/esm/local-key-manager.js +417 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/primitives/aes-ctr.js +327 -0
- package/dist/esm/primitives/aes-ctr.js.map +1 -0
- package/dist/esm/primitives/aes-gcm.js +347 -0
- package/dist/esm/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +185 -0
- package/dist/esm/primitives/concat-kdf.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +521 -0
- package/dist/esm/primitives/ed25519.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +78 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/primitives/secp256k1.js +805 -0
- package/dist/esm/primitives/secp256k1.js.map +1 -0
- package/dist/esm/primitives/secp256r1.js +806 -0
- package/dist/esm/primitives/secp256r1.js.map +1 -0
- package/dist/esm/primitives/sha256.js +55 -0
- package/dist/esm/primitives/sha256.js.map +1 -0
- package/dist/esm/primitives/x25519.js +392 -0
- package/dist/esm/primitives/x25519.js.map +1 -0
- package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/esm/primitives/xchacha20.js +246 -0
- package/dist/esm/primitives/xchacha20.js.map +1 -0
- package/dist/esm/types/cipher.js +2 -0
- package/dist/esm/types/cipher.js.map +1 -0
- package/dist/esm/types/crypto-api.js +2 -0
- package/dist/esm/types/crypto-api.js.map +1 -0
- package/dist/esm/types/hasher.js +2 -0
- package/dist/esm/types/hasher.js.map +1 -0
- package/dist/esm/types/identifier.js +2 -0
- package/dist/esm/types/identifier.js.map +1 -0
- package/dist/esm/types/key-compressor.js +2 -0
- package/dist/esm/types/key-compressor.js.map +1 -0
- package/dist/esm/types/key-converter.js +2 -0
- package/dist/esm/types/key-converter.js.map +1 -0
- package/dist/esm/types/key-deriver.js +2 -0
- package/dist/esm/types/key-deriver.js.map +1 -0
- package/dist/esm/types/key-generator.js +2 -0
- package/dist/esm/types/key-generator.js.map +1 -0
- package/dist/esm/types/key-io.js +2 -0
- package/dist/esm/types/key-io.js.map +1 -0
- package/dist/esm/types/key-wrapper.js +2 -0
- package/dist/esm/types/key-wrapper.js.map +1 -0
- package/dist/esm/types/params-direct.js +2 -0
- package/dist/esm/types/params-direct.js.map +1 -0
- package/dist/esm/types/params-enclosed.js +2 -0
- package/dist/esm/types/params-enclosed.js.map +1 -0
- package/dist/esm/types/params-kms.js +2 -0
- package/dist/esm/types/params-kms.js.map +1 -0
- package/dist/esm/types/signer.js +2 -0
- package/dist/esm/types/signer.js.map +1 -0
- package/dist/esm/utils.js +165 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/algorithms/aes-ctr.d.ts +121 -0
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
- package/dist/types/algorithms/aes-gcm.d.ts +152 -0
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +154 -0
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/algorithms/eddsa.d.ts +151 -0
- package/dist/types/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +51 -0
- package/dist/types/algorithms/sha-2.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/jose/jwe.d.ts +135 -0
- package/dist/types/jose/jwe.d.ts.map +1 -0
- package/dist/types/jose/jwk.d.ts +439 -0
- package/dist/types/jose/jwk.d.ts.map +1 -0
- package/dist/types/jose/jws.d.ts +67 -0
- package/dist/types/jose/jws.d.ts.map +1 -0
- package/dist/types/jose/jwt.d.ts +139 -0
- package/dist/types/jose/jwt.d.ts.map +1 -0
- package/dist/types/jose/utils.d.ts +14 -0
- package/dist/types/jose/utils.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +307 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/primitives/aes-ctr.d.ts +219 -0
- package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
- package/dist/types/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +160 -0
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
- package/dist/types/primitives/ed25519.d.ts +359 -0
- package/dist/types/primitives/ed25519.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +94 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/primitives/secp256k1.d.ts +598 -0
- package/dist/types/primitives/secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/secp256r1.d.ts +599 -0
- package/dist/types/primitives/secp256r1.d.ts.map +1 -0
- package/dist/types/primitives/sha256.d.ts +42 -0
- package/dist/types/primitives/sha256.d.ts.map +1 -0
- package/dist/types/primitives/x25519.d.ts +283 -0
- package/dist/types/primitives/x25519.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20.d.ts +186 -0
- package/dist/types/primitives/xchacha20.d.ts.map +1 -0
- package/dist/types/types/cipher.d.ts +49 -0
- package/dist/types/types/cipher.d.ts.map +1 -0
- package/dist/types/types/crypto-api.d.ts +40 -0
- package/dist/types/types/crypto-api.d.ts.map +1 -0
- package/dist/types/types/hasher.d.ts +33 -0
- package/dist/types/types/hasher.d.ts.map +1 -0
- package/dist/types/types/identifier.d.ts +16 -0
- package/dist/types/types/identifier.d.ts.map +1 -0
- package/dist/types/types/key-compressor.d.ts +28 -0
- package/dist/types/types/key-compressor.d.ts.map +1 -0
- package/dist/types/types/key-converter.d.ts +57 -0
- package/dist/types/types/key-converter.d.ts.map +1 -0
- package/dist/types/types/key-deriver.d.ts +39 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -0
- package/dist/types/types/key-generator.d.ts +105 -0
- package/dist/types/types/key-generator.d.ts.map +1 -0
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -0
- package/dist/types/types/key-wrapper.d.ts +38 -0
- package/dist/types/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/types/params-direct.d.ts +90 -0
- package/dist/types/types/params-direct.d.ts.map +1 -0
- package/dist/types/types/params-enclosed.d.ts +47 -0
- package/dist/types/types/params-enclosed.d.ts.map +1 -0
- package/dist/types/types/params-kms.d.ts +131 -0
- package/dist/types/types/params-kms.d.ts.map +1 -0
- package/dist/types/types/signer.d.ts +46 -0
- package/dist/types/types/signer.d.ts.map +1 -0
- package/dist/types/utils.d.ts +112 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +7 -0
- package/package.json +103 -0
- package/src/algorithms/aes-ctr.ts +156 -0
- package/src/algorithms/aes-gcm.ts +187 -0
- package/src/algorithms/crypto-algorithm.ts +4 -0
- package/src/algorithms/ecdsa.ts +269 -0
- package/src/algorithms/eddsa.ts +243 -0
- package/src/algorithms/sha-2.ts +65 -0
- package/src/index.ts +42 -0
- package/src/jose/jwe.ts +196 -0
- package/src/jose/jwk.ts +632 -0
- package/src/jose/jws.ts +95 -0
- package/src/jose/jwt.ts +147 -0
- package/src/jose/utils.ts +34 -0
- package/src/local-key-manager.ts +540 -0
- package/src/primitives/aes-ctr.ts +352 -0
- package/src/primitives/aes-gcm.ts +378 -0
- package/src/primitives/concat-kdf.ts +240 -0
- package/src/primitives/ed25519.ts +548 -0
- package/src/primitives/pbkdf2.ts +122 -0
- package/src/primitives/secp256k1.ts +848 -0
- package/src/primitives/secp256r1.ts +850 -0
- package/src/primitives/sha256.ts +47 -0
- package/src/primitives/x25519.ts +403 -0
- package/src/primitives/xchacha20-poly1305.ts +274 -0
- package/src/primitives/xchacha20.ts +247 -0
- package/src/types/cipher.ts +53 -0
- package/src/types/crypto-api.ts +56 -0
- package/src/types/hasher.ts +32 -0
- package/src/types/identifier.ts +16 -0
- package/src/types/key-compressor.ts +25 -0
- package/src/types/key-converter.ts +53 -0
- package/src/types/key-deriver.ts +43 -0
- package/src/types/key-generator.ts +119 -0
- package/src/types/key-io.ts +42 -0
- package/src/types/key-wrapper.ts +42 -0
- package/src/types/params-direct.ts +106 -0
- package/src/types/params-enclosed.ts +50 -0
- package/src/types/params-kms.ts +156 -0
- package/src/types/signer.ts +50 -0
- package/src/utils.ts +181 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto-api.js","sourceRoot":"","sources":["../../../src/types/crypto-api.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasher.js","sourceRoot":"","sources":["../../../src/types/hasher.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../../src/types/identifier.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-compressor.js","sourceRoot":"","sources":["../../../src/types/key-compressor.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-converter.js","sourceRoot":"","sources":["../../../src/types/key-converter.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-deriver.js","sourceRoot":"","sources":["../../../src/types/key-deriver.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-generator.js","sourceRoot":"","sources":["../../../src/types/key-generator.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-io.js","sourceRoot":"","sources":["../../../src/types/key-io.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-wrapper.js","sourceRoot":"","sources":["../../../src/types/key-wrapper.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params-direct.js","sourceRoot":"","sources":["../../../src/types/params-direct.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params-enclosed.js","sourceRoot":"","sources":["../../../src/types/params-enclosed.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"params-kms.js","sourceRoot":"","sources":["../../../src/types/params-kms.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.js","sourceRoot":"","sources":["../../../src/types/signer.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CryptoUtils = void 0;
|
|
4
|
+
var crypto_1 = require("@noble/hashes/crypto");
|
|
5
|
+
var utils_1 = require("@noble/hashes/utils");
|
|
6
|
+
/**
|
|
7
|
+
* A collection of cryptographic utility methods.
|
|
8
|
+
*/
|
|
9
|
+
var CryptoUtils = /** @class */ (function () {
|
|
10
|
+
function CryptoUtils() {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Determines the JOSE algorithm identifier of the digital signature algorithm based on the `alg` or
|
|
14
|
+
* `crv` property of a {@link Jwk | JWK}.
|
|
15
|
+
*
|
|
16
|
+
* If the `alg` property is present, its value takes precedence and is returned. Otherwise, the
|
|
17
|
+
* `crv` property is used to determine the algorithm.
|
|
18
|
+
*
|
|
19
|
+
* @memberof CryptoUtils
|
|
20
|
+
* @see {@link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms | JOSE Algorithms}
|
|
21
|
+
* @see {@link https://datatracker.ietf.org/doc/draft-ietf-jose-fully-specified-algorithms/ | Fully-Specified Algorithms for JOSE and COSE}
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const publicKey: Jwk = {
|
|
26
|
+
* "kty": "OKP",
|
|
27
|
+
* "crv": "Ed25519",
|
|
28
|
+
* "x": "FEJG7OakZi500EydXxuE8uMc8uaAzEJkmQeG8khXANw"
|
|
29
|
+
* }
|
|
30
|
+
* const algorithm = getJoseSignatureAlgorithmFromPublicKey(publicKey);
|
|
31
|
+
* console.log(algorithm); // Output: "EdDSA"
|
|
32
|
+
* ```
|
|
33
|
+
* @param publicKey - A JWK containing the `alg` and/or `crv` properties.
|
|
34
|
+
* @returns The name of the algorithm associated with the key.
|
|
35
|
+
* @throws Error if the algorithm cannot be determined from the provided input.
|
|
36
|
+
*/
|
|
37
|
+
CryptoUtils.getJoseSignatureAlgorithmFromPublicKey = function (publicKey) {
|
|
38
|
+
var curveToJoseAlgorithm = {
|
|
39
|
+
'Ed25519': 'EdDSA',
|
|
40
|
+
'P-256': 'ES256',
|
|
41
|
+
'P-384': 'ES384',
|
|
42
|
+
'P-521': 'ES512',
|
|
43
|
+
'secp256k1': 'ES256K',
|
|
44
|
+
};
|
|
45
|
+
// If the key contains an `alg` property that matches a JOSE registered algorithm identifier,
|
|
46
|
+
// return its value.
|
|
47
|
+
if (publicKey.alg && Object.values(curveToJoseAlgorithm).includes(publicKey.alg)) {
|
|
48
|
+
return publicKey.alg;
|
|
49
|
+
}
|
|
50
|
+
// If the key contains a `crv` property, return the corresponding algorithm.
|
|
51
|
+
if (publicKey.crv && Object.keys(curveToJoseAlgorithm).includes(publicKey.crv)) {
|
|
52
|
+
return curveToJoseAlgorithm[publicKey.crv];
|
|
53
|
+
}
|
|
54
|
+
throw new Error("Unable to determine algorithm based on provided input: alg=".concat(publicKey.alg, ", crv=").concat(publicKey.crv, ". ") +
|
|
55
|
+
"Supported 'alg' values: ".concat(Object.values(curveToJoseAlgorithm).join(', '), ". ") +
|
|
56
|
+
"Supported 'crv' values: ".concat(Object.keys(curveToJoseAlgorithm).join(', '), "."));
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Generates secure pseudorandom values of the specified length using
|
|
60
|
+
* `crypto.getRandomValues`, which defers to the operating system.
|
|
61
|
+
*
|
|
62
|
+
* @memberof CryptoUtils
|
|
63
|
+
* @remarks
|
|
64
|
+
* This function is a wrapper around `randomBytes` from the '@noble/hashes'
|
|
65
|
+
* package. It's designed to be cryptographically strong, suitable for
|
|
66
|
+
* generating initialization vectors, nonces, and other random values.
|
|
67
|
+
*
|
|
68
|
+
* @see {@link https://www.npmjs.com/package/@noble/hashes | @noble/hashes on NPM} for more
|
|
69
|
+
* information about the underlying implementation.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const bytes = randomBytes(32); // Generates 32 random bytes
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @param bytesLength - The number of bytes to generate.
|
|
77
|
+
* @returns A Uint8Array containing the generated random bytes.
|
|
78
|
+
*/
|
|
79
|
+
CryptoUtils.randomBytes = function (bytesLength) {
|
|
80
|
+
return (0, utils_1.randomBytes)(bytesLength);
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Generates a UUID (Universally Unique Identifier) using a
|
|
84
|
+
* cryptographically strong random number generator following
|
|
85
|
+
* the version 4 format, as specified in RFC 4122.
|
|
86
|
+
*
|
|
87
|
+
* A version 4 UUID is a randomly generated UUID. The 13th character
|
|
88
|
+
* is set to '4' to denote version 4, and the 17th character is one
|
|
89
|
+
* of '8', '9', 'A', or 'B' to comply with the variant 1 format of
|
|
90
|
+
* UUIDs (the high bits are set to '10').
|
|
91
|
+
*
|
|
92
|
+
* The UUID is a 36 character string, including hyphens, and looks like this:
|
|
93
|
+
* xxxxxxxx-xxxx-4xxx-axxx-xxxxxxxxxxxx
|
|
94
|
+
*
|
|
95
|
+
* Note that while UUIDs are not guaranteed to be unique, they are
|
|
96
|
+
* practically unique" given the large number of possible UUIDs and
|
|
97
|
+
* the randomness of generation.
|
|
98
|
+
* @memberof CryptoUtils
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const uuid = randomUuid();
|
|
102
|
+
* console.log(uuid); // Outputs a version 4 UUID, e.g., '123e4567-e89b-12d3-a456-426655440000'
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @returns A string containing a randomly generated, 36 character long v4 UUID.
|
|
106
|
+
*/
|
|
107
|
+
CryptoUtils.randomUuid = function () {
|
|
108
|
+
var uuid = crypto_1.crypto.randomUUID();
|
|
109
|
+
return uuid;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Generates a secure random PIN (Personal Identification Number) of a
|
|
113
|
+
* specified length.
|
|
114
|
+
*
|
|
115
|
+
* This function ensures that the generated PIN is cryptographically secure and
|
|
116
|
+
* uniformly distributed by using rejection sampling. It repeatedly generates
|
|
117
|
+
* random numbers until it gets one in the desired range [0, max]. This avoids
|
|
118
|
+
* bias introduced by simply taking the modulus or truncating the number.
|
|
119
|
+
*
|
|
120
|
+
* Note: The function can generate PINs of 3 to 10 digits in length.
|
|
121
|
+
* Any request for a PIN outside this range will result in an error.
|
|
122
|
+
*
|
|
123
|
+
* Example usage:
|
|
124
|
+
*
|
|
125
|
+
* ```ts
|
|
126
|
+
* const pin = randomPin({ length: 4 });
|
|
127
|
+
* console.log(pin); // Outputs a 4-digit PIN, e.g., "0231"
|
|
128
|
+
* ```
|
|
129
|
+
* @memberof CryptoUtils
|
|
130
|
+
* @param options - The options object containing the desired length of the generated PIN.
|
|
131
|
+
* @param options.length - The desired length of the generated PIN. The value should be
|
|
132
|
+
* an integer between 3 and 8 inclusive.
|
|
133
|
+
*
|
|
134
|
+
* @returns A string representing the generated PIN. The PIN will be zero-padded
|
|
135
|
+
* to match the specified length, if necessary.
|
|
136
|
+
*
|
|
137
|
+
* @throws Will throw an error if the requested PIN length is less than 3 or greater than 8.
|
|
138
|
+
*/
|
|
139
|
+
CryptoUtils.randomPin = function (_a) {
|
|
140
|
+
var length = _a.length;
|
|
141
|
+
if (3 > length || length > 10) {
|
|
142
|
+
throw new Error('randomPin() can securely generate a PIN between 3 to 10 digits.');
|
|
143
|
+
}
|
|
144
|
+
var max = Math.pow(10, length) - 1;
|
|
145
|
+
var pin;
|
|
146
|
+
if (length <= 6) {
|
|
147
|
+
var rejectionRange = Math.pow(10, length);
|
|
148
|
+
do {
|
|
149
|
+
// Adjust the byte generation based on length.
|
|
150
|
+
var randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2)); // 2 digits per byte.
|
|
151
|
+
var view = new DataView(randomBuffer.buffer);
|
|
152
|
+
// Convert the buffer to integer and take modulus based on length.
|
|
153
|
+
pin = view.getUint16(0, false) % rejectionRange;
|
|
154
|
+
} while (pin > max);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
var rejectionRange = Math.pow(10, 10); // For max 10 digit number.
|
|
158
|
+
do {
|
|
159
|
+
// Generates 4 random bytes.
|
|
160
|
+
var randomBuffer = CryptoUtils.randomBytes(4);
|
|
161
|
+
// Create a DataView to read from the randomBuffer.
|
|
162
|
+
var view = new DataView(randomBuffer.buffer);
|
|
163
|
+
// Transform bytes to number (big endian).
|
|
164
|
+
pin = view.getUint32(0, false) % rejectionRange;
|
|
165
|
+
} while (pin > max); // Reject if the number is outside the desired range.
|
|
166
|
+
}
|
|
167
|
+
// Pad the PIN with leading zeros to the desired length.
|
|
168
|
+
return pin.toString().padStart(length, '0');
|
|
169
|
+
};
|
|
170
|
+
return CryptoUtils;
|
|
171
|
+
}());
|
|
172
|
+
exports.CryptoUtils = CryptoUtils;
|
|
173
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAEA,+CAA8C;AAC9C,6CAAsE;AAEtE;;GAEG;AACH;IAAA;IA4KA,CAAC;IA1KC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACI,kDAAsC,GAA7C,UAA8C,SAAc;QAC1D,IAAM,oBAAoB,GAA2B;YACnD,SAAS,EAAK,OAAO;YACrB,OAAO,EAAO,OAAO;YACrB,OAAO,EAAO,OAAO;YACrB,OAAO,EAAO,OAAO;YACrB,WAAW,EAAG,QAAQ;SACvB,CAAC;QAEF,6FAA6F;QAC7F,oBAAoB;QACpB,IAAI,SAAS,CAAC,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YACjF,OAAO,SAAS,CAAC,GAAG,CAAC;QACvB,CAAC;QAED,4EAA4E;QAC5E,IAAI,SAAS,CAAC,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;YAC/E,OAAO,oBAAoB,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7C,CAAC;QAED,MAAM,IAAI,KAAK,CACb,qEAA8D,SAAS,CAAC,GAAG,mBAAS,SAAS,CAAC,GAAG,OAAI;YACrG,kCAA2B,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,OAAI;YAC7E,kCAA2B,MAAM,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,MAAG,CAC3E,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,uBAAW,GAAlB,UAAmB,WAAmB;QACpC,OAAO,IAAA,mBAAgB,EAAC,WAAW,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACI,sBAAU,GAAjB;QACE,IAAM,IAAI,GAAG,eAAM,CAAC,UAAU,EAAE,CAAC;QAEjC,OAAO,IAAI,CAAC;IACd,CAAC;IAGD;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACI,qBAAS,GAAhB,UAAiB,EAA8B;YAA5B,MAAM,YAAA;QACvB,IAAI,CAAC,GAAG,MAAM,IAAI,MAAM,GAAG,EAAE,EAAE,CAAC;YAC9B,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;QACrF,CAAC;QAED,IAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;QAErC,IAAI,GAAG,CAAC;QAER,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;YAChB,IAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;YAC5C,GAAG,CAAC;gBACF,8CAA8C;gBAC9C,IAAM,YAAY,GAAG,WAAW,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAE,CAAC,CAAE,qBAAqB;gBAC5F,IAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;gBAC/C,kEAAkE;gBAClE,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,cAAc,CAAC;YAClD,CAAC,QAAQ,GAAG,GAAG,GAAG,EAAE;QACtB,CAAC;aAAM,CAAC;YACN,IAAM,cAAc,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,2BAA2B;YACpE,GAAG,CAAC;gBACJ,4BAA4B;gBAC1B,IAAM,YAAY,GAAG,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;gBAChD,mDAAmD;gBACnD,IAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;gBAC/C,0CAA0C;gBAC1C,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,cAAc,CAAC;YAClD,CAAC,QAAQ,GAAG,GAAG,GAAG,EAAE,CAAE,qDAAqD;QAC7E,CAAC;QAED,wDAAwD;QACxD,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9C,CAAC;IACH,kBAAC;AAAD,CAAC,AA5KD,IA4KC;AA5KY,kCAAW"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { AesCtr } from '../primitives/aes-ctr.js';
|
|
11
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
12
|
+
/**
|
|
13
|
+
* The `AesCtrAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
14
|
+
* the AES algorithm in Counter (CTR) mode. This class implements both {@link Cipher | `Cipher`} and
|
|
15
|
+
* { @link KeyGenerator | `KeyGenerator`} interfaces, providing key generation, encryption, and
|
|
16
|
+
* decryption features.
|
|
17
|
+
*
|
|
18
|
+
* This class is typically accessed through implementations that extend the
|
|
19
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
20
|
+
*/
|
|
21
|
+
export class AesCtrAlgorithm extends CryptoAlgorithm {
|
|
22
|
+
/**
|
|
23
|
+
* Decrypts the provided data using AES-CTR.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* This method performs AES-CTR decryption on the given encrypted data using the specified key.
|
|
27
|
+
* Similar to the encryption process, it requires an initial counter block and the length
|
|
28
|
+
* of the counter block, along with the encrypted data and the decryption key. The method
|
|
29
|
+
* returns the decrypted data as a Uint8Array.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
34
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
35
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block used during encryption
|
|
36
|
+
* const key = { ... }; // A Jwk object representing the same AES key used for encryption
|
|
37
|
+
* const decryptedData = await aesCtr.decrypt({
|
|
38
|
+
* data: encryptedData,
|
|
39
|
+
* counter,
|
|
40
|
+
* key,
|
|
41
|
+
* length: 128 // Length of the counter in bits
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param params - The parameters for the decryption operation.
|
|
46
|
+
*
|
|
47
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
48
|
+
*/
|
|
49
|
+
decrypt(params) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const plaintext = AesCtr.decrypt(params);
|
|
52
|
+
return plaintext;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Encrypts the provided data using AES-CTR.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* This method performs AES-CTR encryption on the given data using the specified key.
|
|
60
|
+
* It requires the initial counter block and the length of the counter block, alongside
|
|
61
|
+
* the data and key. The method is designed to work asynchronously and returns the
|
|
62
|
+
* encrypted data as a Uint8Array.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
67
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
68
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
|
|
69
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
70
|
+
* const encryptedData = await aesCtr.encrypt({
|
|
71
|
+
* data,
|
|
72
|
+
* counter,
|
|
73
|
+
* key,
|
|
74
|
+
* length: 128 // Length of the counter in bits
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param params - The parameters for the encryption operation.
|
|
79
|
+
*
|
|
80
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
81
|
+
*/
|
|
82
|
+
encrypt(params) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const ciphertext = AesCtr.encrypt(params);
|
|
85
|
+
return ciphertext;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Generates a symmetric key for AES in Counter (CTR) mode in JSON Web Key (JWK) format.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* This method generates a symmetric AES key for use in CTR mode, based on the specified
|
|
93
|
+
* `algorithm` parameter which determines the key length. It uses cryptographically secure random
|
|
94
|
+
* number generation to ensure the uniqueness and security of the key. The key is returned in JWK
|
|
95
|
+
* format.
|
|
96
|
+
*
|
|
97
|
+
* The generated key includes the following components:
|
|
98
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
99
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
100
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
105
|
+
* const privateKey = await aesCtr.generateKey({ algorithm: 'A256CTR' });
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @param params - The parameters for the key generation.
|
|
109
|
+
*
|
|
110
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
111
|
+
*/
|
|
112
|
+
generateKey(_a) {
|
|
113
|
+
return __awaiter(this, arguments, void 0, function* ({ algorithm }) {
|
|
114
|
+
// Map algorithm name to key length.
|
|
115
|
+
const length = { A128CTR: 128, A192CTR: 192, A256CTR: 256 }[algorithm];
|
|
116
|
+
// Generate a random private key.
|
|
117
|
+
const privateKey = yield AesCtr.generateKey({ length });
|
|
118
|
+
// Set the `alg` property based on the specified algorithm.
|
|
119
|
+
privateKey.alg = algorithm;
|
|
120
|
+
return privateKey;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=aes-ctr.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-ctr.js","sourceRoot":"","sources":["../../../src/algorithms/aes-ctr.ts"],"names":[],"mappings":";;;;;;;;;AAKA,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AA4BxD;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAgB,SAAQ,eAAe;IAIlD;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,OAAO,CAAC,MACS;;YAE5B,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAEzC,OAAO,SAAS,CAAC;QACnB,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,OAAO,CAAC,MACS;;YAE5B,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAE1C,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,WAAW;6DAAC,EAAE,SAAS,EACX;YAEvB,oCAAoC;YACpC,MAAM,MAAM,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAoB,CAAC;YAE1F,iCAAiC;YACjC,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;YAExD,2DAA2D;YAC3D,UAAU,CAAC,GAAG,GAAG,SAAS,CAAC;YAE3B,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;CACF"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
11
|
+
import { AesGcm } from '../primitives/aes-gcm.js';
|
|
12
|
+
/**
|
|
13
|
+
* The `AesGcmAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
14
|
+
* the AES algorithm in Galois/Counter Mode (GCM). This class implements both
|
|
15
|
+
* {@link Cipher | `Cipher`} and { @link KeyGenerator | `KeyGenerator`} interfaces, providing
|
|
16
|
+
* key generation, encryption, and decryption features.
|
|
17
|
+
*
|
|
18
|
+
* This class is typically accessed through implementations that extend the
|
|
19
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
20
|
+
*/
|
|
21
|
+
export class AesGcmAlgorithm extends CryptoAlgorithm {
|
|
22
|
+
/**
|
|
23
|
+
* Decrypts the provided data using AES-GCM.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* This method performs AES-GCM decryption on the given encrypted data using the specified key.
|
|
27
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
28
|
+
* and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
|
|
29
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
30
|
+
* tag used when encrypting the data. If not specified, the default tag length of 128 bits is
|
|
31
|
+
* used.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
36
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
37
|
+
* const iv = new Uint8Array([...]); // Initialization vector used during encryption
|
|
38
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
39
|
+
* const key = { ... }; // A Jwk object representing the AES key
|
|
40
|
+
* const decryptedData = await aesGcm.decrypt({
|
|
41
|
+
* data: encryptedData,
|
|
42
|
+
* iv,
|
|
43
|
+
* additionalData,
|
|
44
|
+
* key,
|
|
45
|
+
* tagLength: 128 // Optional tag length in bits
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param params - The parameters for the decryption operation.
|
|
50
|
+
*
|
|
51
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
52
|
+
*/
|
|
53
|
+
decrypt(params) {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const plaintext = AesGcm.decrypt(params);
|
|
56
|
+
return plaintext;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Encrypts the provided data using AES-GCM.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* This method performs AES-GCM encryption on the given data using the specified key.
|
|
64
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
65
|
+
* and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
|
|
66
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
67
|
+
* tag generated in the encryption operation and used for authentication in the corresponding
|
|
68
|
+
* decryption. If not specified, the default tag length of 128 bits is used.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
73
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
74
|
+
* const iv = new Uint8Array([...]); // Initialization vector
|
|
75
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
76
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
77
|
+
* const encryptedData = await aesGcm.encrypt({
|
|
78
|
+
* data,
|
|
79
|
+
* iv,
|
|
80
|
+
* additionalData,
|
|
81
|
+
* key,
|
|
82
|
+
* tagLength: 128 // Optional tag length in bits
|
|
83
|
+
* });
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @param params - The parameters for the encryption operation.
|
|
87
|
+
*
|
|
88
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
89
|
+
*/
|
|
90
|
+
encrypt(params) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
const ciphertext = AesGcm.encrypt(params);
|
|
93
|
+
return ciphertext;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
|
|
98
|
+
*
|
|
99
|
+
* @remarks
|
|
100
|
+
* This method generates a symmetric AES key for use in GCM mode, based on the specified
|
|
101
|
+
* `algorithm` parameter which determines the key length. It uses cryptographically secure random
|
|
102
|
+
* number generation to ensure the uniqueness and security of the key. The key is returned in JWK
|
|
103
|
+
* format.
|
|
104
|
+
*
|
|
105
|
+
* The generated key includes the following components:
|
|
106
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
107
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
108
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
113
|
+
* const privateKey = await aesGcm.generateKey({ algorithm: 'A256GCM' });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @param params - The parameters for the key generation.
|
|
117
|
+
*
|
|
118
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
119
|
+
*/
|
|
120
|
+
generateKey(_a) {
|
|
121
|
+
return __awaiter(this, arguments, void 0, function* ({ algorithm }) {
|
|
122
|
+
// Map algorithm name to key length.
|
|
123
|
+
const length = { A128GCM: 128, A192GCM: 192, A256GCM: 256 }[algorithm];
|
|
124
|
+
// Generate a random private key.
|
|
125
|
+
const privateKey = yield AesGcm.generateKey({ length });
|
|
126
|
+
// Set the `alg` property based on the specified algorithm.
|
|
127
|
+
privateKey.alg = algorithm;
|
|
128
|
+
return privateKey;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=aes-gcm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm.js","sourceRoot":"","sources":["../../../src/algorithms/aes-gcm.ts"],"names":[],"mappings":";;;;;;;;;AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,MAAM,EAAuB,MAAM,0BAA0B,CAAC;AAmDvE;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAgB,SAAQ,eAAe;IAIlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,OAAO,CAAC,MACS;;YAE5B,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAEzC,OAAO,SAAS,CAAC;QACnB,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,OAAO,CAAC,MACS;;YAE5B,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAE1C,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,WAAW;6DAAC,EAAE,SAAS,EACX;YAEvB,oCAAoC;YACpC,MAAM,MAAM,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAoB,CAAC;YAE1F,iCAAiC;YACjC,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;YAExD,2DAA2D;YAC3D,UAAU,CAAC,GAAG,GAAG,SAAS,CAAC;YAE3B,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto-algorithm.js","sourceRoot":"","sources":["../../../src/algorithms/crypto-algorithm.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAgB,eAAe;CAAG"}
|