@enbox/crypto 0.0.2 → 0.0.3

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.
Files changed (182) hide show
  1. package/README.md +34 -102
  2. package/dist/browser.js +6 -10
  3. package/dist/browser.js.map +4 -4
  4. package/dist/browser.mjs +6 -10
  5. package/dist/browser.mjs.map +4 -4
  6. package/dist/esm/algorithms/aes-gcm.js +1 -1
  7. package/dist/esm/algorithms/aes-gcm.js.map +1 -1
  8. package/dist/esm/algorithms/ecdsa.js +9 -5
  9. package/dist/esm/algorithms/ecdsa.js.map +1 -1
  10. package/dist/esm/algorithms/eddsa.js +9 -5
  11. package/dist/esm/algorithms/eddsa.js.map +1 -1
  12. package/dist/esm/algorithms/sha-2.js +1 -1
  13. package/dist/esm/algorithms/sha-2.js.map +1 -1
  14. package/dist/esm/crypto-error.js +41 -0
  15. package/dist/esm/crypto-error.js.map +1 -0
  16. package/dist/esm/index.js +3 -0
  17. package/dist/esm/index.js.map +1 -1
  18. package/dist/esm/jose/jwk.js +52 -26
  19. package/dist/esm/jose/jwk.js.map +1 -1
  20. package/dist/esm/local-key-manager.js +3 -2
  21. package/dist/esm/local-key-manager.js.map +1 -1
  22. package/dist/esm/primitives/aes-ctr.js.map +1 -1
  23. package/dist/esm/primitives/aes-gcm.js.map +1 -1
  24. package/dist/esm/primitives/aes-kw.js +246 -0
  25. package/dist/esm/primitives/aes-kw.js.map +1 -0
  26. package/dist/esm/primitives/concat-kdf.js +1 -1
  27. package/dist/esm/primitives/concat-kdf.js.map +1 -1
  28. package/dist/esm/primitives/ed25519.js +3 -3
  29. package/dist/esm/primitives/ed25519.js.map +1 -1
  30. package/dist/esm/primitives/hkdf.js +79 -0
  31. package/dist/esm/primitives/hkdf.js.map +1 -0
  32. package/dist/esm/primitives/pbkdf2.js +49 -0
  33. package/dist/esm/primitives/pbkdf2.js.map +1 -1
  34. package/dist/esm/primitives/secp256k1.js +4 -4
  35. package/dist/esm/primitives/secp256k1.js.map +1 -1
  36. package/dist/esm/primitives/secp256r1.js +4 -4
  37. package/dist/esm/primitives/secp256r1.js.map +1 -1
  38. package/dist/esm/primitives/x25519.js +1 -1
  39. package/dist/esm/primitives/x25519.js.map +1 -1
  40. package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
  41. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
  42. package/dist/esm/primitives/xchacha20.js +1 -1
  43. package/dist/esm/primitives/xchacha20.js.map +1 -1
  44. package/dist/esm/utils.js.map +1 -1
  45. package/dist/types/algorithms/aes-ctr.d.ts +1 -1
  46. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
  47. package/dist/types/algorithms/aes-gcm.d.ts +2 -2
  48. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  49. package/dist/types/algorithms/ecdsa.d.ts +1 -1
  50. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  51. package/dist/types/algorithms/eddsa.d.ts +2 -2
  52. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  53. package/dist/types/algorithms/sha-2.d.ts +1 -1
  54. package/dist/types/algorithms/sha-2.d.ts.map +1 -1
  55. package/dist/types/crypto-error.d.ts +29 -0
  56. package/dist/types/crypto-error.d.ts.map +1 -0
  57. package/dist/types/index.d.ts +3 -0
  58. package/dist/types/index.d.ts.map +1 -1
  59. package/dist/types/jose/jwk.d.ts.map +1 -1
  60. package/dist/types/local-key-manager.d.ts +3 -3
  61. package/dist/types/local-key-manager.d.ts.map +1 -1
  62. package/dist/types/primitives/aes-kw.d.ts +103 -0
  63. package/dist/types/primitives/aes-kw.d.ts.map +1 -0
  64. package/dist/types/primitives/concat-kdf.d.ts +1 -1
  65. package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
  66. package/dist/types/primitives/hkdf.d.ts +90 -0
  67. package/dist/types/primitives/hkdf.d.ts.map +1 -0
  68. package/dist/types/primitives/pbkdf2.d.ts +58 -0
  69. package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
  70. package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
  71. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
  72. package/dist/types/types/cipher.d.ts +1 -1
  73. package/dist/types/types/crypto-api.d.ts +3 -3
  74. package/dist/types/types/crypto-api.d.ts.map +1 -1
  75. package/dist/types/types/params-direct.d.ts +79 -1
  76. package/dist/types/types/params-direct.d.ts.map +1 -1
  77. package/dist/utils.js.map +2 -2
  78. package/package.json +26 -39
  79. package/src/algorithms/aes-ctr.ts +1 -1
  80. package/src/algorithms/aes-gcm.ts +3 -2
  81. package/src/algorithms/ecdsa.ts +13 -7
  82. package/src/algorithms/eddsa.ts +9 -9
  83. package/src/algorithms/sha-2.ts +2 -2
  84. package/src/crypto-error.ts +45 -0
  85. package/src/index.ts +3 -0
  86. package/src/jose/jwk.ts +32 -32
  87. package/src/local-key-manager.ts +14 -13
  88. package/src/primitives/aes-ctr.ts +1 -1
  89. package/src/primitives/aes-gcm.ts +5 -5
  90. package/src/primitives/aes-kw.ts +269 -0
  91. package/src/primitives/concat-kdf.ts +4 -2
  92. package/src/primitives/ed25519.ts +6 -6
  93. package/src/primitives/hkdf.ts +121 -0
  94. package/src/primitives/pbkdf2.ts +91 -0
  95. package/src/primitives/secp256k1.ts +6 -6
  96. package/src/primitives/secp256r1.ts +6 -6
  97. package/src/primitives/x25519.ts +3 -3
  98. package/src/primitives/xchacha20-poly1305.ts +57 -4
  99. package/src/primitives/xchacha20.ts +1 -1
  100. package/src/types/cipher.ts +1 -1
  101. package/src/types/crypto-api.ts +5 -5
  102. package/src/types/params-direct.ts +97 -1
  103. package/src/utils.ts +2 -2
  104. package/dist/cjs/algorithms/aes-ctr.js +0 -188
  105. package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
  106. package/dist/cjs/algorithms/aes-gcm.js +0 -196
  107. package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
  108. package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
  109. package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
  110. package/dist/cjs/algorithms/ecdsa.js +0 -352
  111. package/dist/cjs/algorithms/ecdsa.js.map +0 -1
  112. package/dist/cjs/algorithms/eddsa.js +0 -325
  113. package/dist/cjs/algorithms/eddsa.js.map +0 -1
  114. package/dist/cjs/algorithms/sha-2.js +0 -119
  115. package/dist/cjs/algorithms/sha-2.js.map +0 -1
  116. package/dist/cjs/index.js +0 -41
  117. package/dist/cjs/index.js.map +0 -1
  118. package/dist/cjs/jose/jwe.js +0 -3
  119. package/dist/cjs/jose/jwe.js.map +0 -1
  120. package/dist/cjs/jose/jwk.js +0 -278
  121. package/dist/cjs/jose/jwk.js.map +0 -1
  122. package/dist/cjs/jose/jws.js +0 -3
  123. package/dist/cjs/jose/jws.js.map +0 -1
  124. package/dist/cjs/jose/jwt.js +0 -3
  125. package/dist/cjs/jose/jwt.js.map +0 -1
  126. package/dist/cjs/jose/utils.js +0 -60
  127. package/dist/cjs/jose/utils.js.map +0 -1
  128. package/dist/cjs/local-key-manager.js +0 -521
  129. package/dist/cjs/local-key-manager.js.map +0 -1
  130. package/dist/cjs/package.json +0 -1
  131. package/dist/cjs/primitives/aes-ctr.js +0 -398
  132. package/dist/cjs/primitives/aes-ctr.js.map +0 -1
  133. package/dist/cjs/primitives/aes-gcm.js +0 -425
  134. package/dist/cjs/primitives/aes-gcm.js.map +0 -1
  135. package/dist/cjs/primitives/concat-kdf.js +0 -215
  136. package/dist/cjs/primitives/concat-kdf.js.map +0 -1
  137. package/dist/cjs/primitives/ed25519.js +0 -651
  138. package/dist/cjs/primitives/ed25519.js.map +0 -1
  139. package/dist/cjs/primitives/pbkdf2.js +0 -120
  140. package/dist/cjs/primitives/pbkdf2.js.map +0 -1
  141. package/dist/cjs/primitives/secp256k1.js +0 -958
  142. package/dist/cjs/primitives/secp256k1.js.map +0 -1
  143. package/dist/cjs/primitives/secp256r1.js +0 -959
  144. package/dist/cjs/primitives/secp256r1.js.map +0 -1
  145. package/dist/cjs/primitives/sha256.js +0 -93
  146. package/dist/cjs/primitives/sha256.js.map +0 -1
  147. package/dist/cjs/primitives/x25519.js +0 -498
  148. package/dist/cjs/primitives/x25519.js.map +0 -1
  149. package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
  150. package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
  151. package/dist/cjs/primitives/xchacha20.js +0 -316
  152. package/dist/cjs/primitives/xchacha20.js.map +0 -1
  153. package/dist/cjs/types/cipher.js +0 -3
  154. package/dist/cjs/types/cipher.js.map +0 -1
  155. package/dist/cjs/types/crypto-api.js +0 -3
  156. package/dist/cjs/types/crypto-api.js.map +0 -1
  157. package/dist/cjs/types/hasher.js +0 -3
  158. package/dist/cjs/types/hasher.js.map +0 -1
  159. package/dist/cjs/types/identifier.js +0 -3
  160. package/dist/cjs/types/identifier.js.map +0 -1
  161. package/dist/cjs/types/key-compressor.js +0 -3
  162. package/dist/cjs/types/key-compressor.js.map +0 -1
  163. package/dist/cjs/types/key-converter.js +0 -3
  164. package/dist/cjs/types/key-converter.js.map +0 -1
  165. package/dist/cjs/types/key-deriver.js +0 -3
  166. package/dist/cjs/types/key-deriver.js.map +0 -1
  167. package/dist/cjs/types/key-generator.js +0 -3
  168. package/dist/cjs/types/key-generator.js.map +0 -1
  169. package/dist/cjs/types/key-io.js +0 -3
  170. package/dist/cjs/types/key-io.js.map +0 -1
  171. package/dist/cjs/types/key-wrapper.js +0 -3
  172. package/dist/cjs/types/key-wrapper.js.map +0 -1
  173. package/dist/cjs/types/params-direct.js +0 -3
  174. package/dist/cjs/types/params-direct.js.map +0 -1
  175. package/dist/cjs/types/params-enclosed.js +0 -3
  176. package/dist/cjs/types/params-enclosed.js.map +0 -1
  177. package/dist/cjs/types/params-kms.js +0 -3
  178. package/dist/cjs/types/params-kms.js.map +0 -1
  179. package/dist/cjs/types/signer.js +0 -3
  180. package/dist/cjs/types/signer.js.map +0 -1
  181. package/dist/cjs/utils.js +0 -173
  182. package/dist/cjs/utils.js.map +0 -1
@@ -1,425 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __rest = (this && this.__rest) || function (s, e) {
50
- var t = {};
51
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
- t[p] = s[p];
53
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
- t[p[i]] = s[p[i]];
57
- }
58
- return t;
59
- };
60
- Object.defineProperty(exports, "__esModule", { value: true });
61
- exports.AesGcm = exports.AES_GCM_TAG_LENGTHS = void 0;
62
- var common_1 = require("@enbox/common");
63
- var webcrypto_1 = require("@noble/ciphers/webcrypto");
64
- var jwk_js_1 = require("../jose/jwk.js");
65
- /**
66
- * Const defining the AES-GCM initialization vector (IV) length in bits.
67
- *
68
- * @remarks
69
- * NIST Special Publication 800-38D, Section 5.2.1.1 states that the IV length:
70
- * > For IVs, it is recommended that implementations restrict support to the length of 96 bits, to
71
- * > promote interoperability, efficiency, and simplicity of design.
72
- *
73
- * This implementation does not support IV lengths that are different from the value defined by
74
- * this constant.
75
- *
76
- * @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
77
- */
78
- var AES_GCM_IV_LENGTH = 96;
79
- /**
80
- * Constant defining the AES key length values in bits.
81
- *
82
- * @remarks
83
- * NIST publication FIPS 197 states:
84
- * > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
85
- * > and decrypt data in blocks of 128 bits.
86
- *
87
- * This implementation does not support key lengths that are different from the three values
88
- * defined by this constant.
89
- *
90
- * @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
91
- */
92
- var AES_KEY_LENGTHS = [128, 192, 256];
93
- /**
94
- * Constant defining the AES-GCM tag length values in bits.
95
- *
96
- * @remarks
97
- * NIST Special Publication 800-38D, Section 5.2.1.2 states that the tag length:
98
- * > may be any one of the following five values: 128, 120, 112, 104, or 96
99
- *
100
- * Although the NIST specification allows for tag lengths of 32 or 64 bits in certain applications,
101
- * the use of shorter tag lengths can be problematic for GCM due to targeted forgery attacks. As a
102
- * precaution, this implementation does not support tag lengths that are different from the five
103
- * values defined by this constant. See Appendix C of the NIST SP 800-38D specification for
104
- * additional guidance and details.
105
- *
106
- * @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
107
- */
108
- exports.AES_GCM_TAG_LENGTHS = [96, 104, 112, 120, 128];
109
- /**
110
- * The `AesGcm` class provides a comprehensive set of utilities for cryptographic operations
111
- * using the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM). This class includes
112
- * methods for key generation, encryption, decryption, and conversions between raw byte arrays
113
- * and JSON Web Key (JWK) formats. It is designed to support AES-GCM, a symmetric key algorithm
114
- * that is widely used for its efficiency, security, and provision of authenticated encryption.
115
- *
116
- * AES-GCM is particularly favored for scenarios that require both confidentiality and integrity
117
- * of data. It integrates the counter mode of encryption with the Galois mode of authentication,
118
- * offering high performance and parallel processing capabilities.
119
- *
120
- * Key Features:
121
- * - Key Generation: Generate AES symmetric keys in JWK format.
122
- * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
123
- * - Encryption: Encrypt data using AES-GCM with the provided symmetric key.
124
- * - Decryption: Decrypt data encrypted with AES-GCM using the corresponding symmetric key.
125
- *
126
- * The methods in this class are asynchronous, returning Promises to accommodate various
127
- * JavaScript environments.
128
- *
129
- * @example
130
- * ```ts
131
- * // Key Generation
132
- * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
133
- * const privateKey = await AesGcm.generateKey({ length });
134
- *
135
- * // Encryption
136
- * const data = new TextEncoder().encode('Messsage');
137
- * const iv = new Uint8Array(12); // 12-byte initialization vector
138
- * const encryptedData = await AesGcm.encrypt({
139
- * data,
140
- * iv,
141
- * key: privateKey
142
- * });
143
- *
144
- * // Decryption
145
- * const decryptedData = await AesGcm.decrypt({
146
- * data: encryptedData,
147
- * iv,
148
- * key: privateKey
149
- * });
150
- *
151
- * // Key Conversion
152
- * const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
153
- * ```
154
- */
155
- var AesGcm = /** @class */ (function () {
156
- function AesGcm() {
157
- }
158
- /**
159
- * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
160
- *
161
- * @remarks
162
- * This method accepts a symmetric key represented as a byte array (Uint8Array) and
163
- * converts it into a JWK object for use with AES-GCM (Advanced Encryption Standard -
164
- * Galois/Counter Mode). The conversion process involves encoding the key into
165
- * base64url format and setting the appropriate JWK parameters.
166
- *
167
- * The resulting JWK object includes the following properties:
168
- * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
169
- * - `k`: The symmetric key, base64url-encoded.
170
- * - `kid`: Key ID, generated based on the JWK thumbprint.
171
- *
172
- * @example
173
- * ```ts
174
- * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
175
- * const privateKey = await AesGcm.bytesToPrivateKey({ privateKeyBytes });
176
- * ```
177
- *
178
- * @param params - The parameters for the symmetric key conversion.
179
- * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
180
- *
181
- * @returns A Promise that resolves to the symmetric key in JWK format.
182
- */
183
- AesGcm.bytesToPrivateKey = function (_a) {
184
- return __awaiter(this, arguments, void 0, function (_b) {
185
- var privateKey, _c;
186
- var privateKeyBytes = _b.privateKeyBytes;
187
- return __generator(this, function (_d) {
188
- switch (_d.label) {
189
- case 0:
190
- privateKey = {
191
- k: common_1.Convert.uint8Array(privateKeyBytes).toBase64Url(),
192
- kty: 'oct'
193
- };
194
- // Compute the JWK thumbprint and set as the key ID.
195
- _c = privateKey;
196
- return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
197
- case 1:
198
- // Compute the JWK thumbprint and set as the key ID.
199
- _c.kid = _d.sent();
200
- return [2 /*return*/, privateKey];
201
- }
202
- });
203
- });
204
- };
205
- /**
206
- * Decrypts the provided data using AES-GCM.
207
- *
208
- * @remarks
209
- * This method performs AES-GCM decryption on the given encrypted data using the specified key.
210
- * It requires an initialization vector (IV), the encrypted data along with the decryption key,
211
- * and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
212
- * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
213
- * tag used when encrypting the data. If not specified, the default tag length of 128 bits is
214
- * used.
215
- *
216
- * @example
217
- * ```ts
218
- * const encryptedData = new Uint8Array([...]); // Encrypted data
219
- * const iv = new Uint8Array([...]); // Initialization vector used during encryption
220
- * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
221
- * const key = { ... }; // A Jwk object representing the AES key
222
- * const decryptedData = await AesGcm.decrypt({
223
- * data: encryptedData,
224
- * iv,
225
- * additionalData,
226
- * key,
227
- * tagLength: 128 // Optional tag length in bits
228
- * });
229
- * ```
230
- *
231
- * @param params - The parameters for the decryption operation.
232
- * @param params.key - The key to use for decryption, represented in JWK format.
233
- * @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
234
- * @param params.iv - The initialization vector, represented as a Uint8Array.
235
- * @param params.additionalData - Optional additional authenticated data. Optional.
236
- * @param params.tagLength - The length of the authentication tag in bits. Optional.
237
- *
238
- * @returns A Promise that resolves to the decrypted data as a Uint8Array.
239
- */
240
- AesGcm.decrypt = function (_a) {
241
- return __awaiter(this, arguments, void 0, function (_b) {
242
- var webCrypto, webCryptoKey, algorithm, plaintextBuffer, plaintext;
243
- var key = _b.key, data = _b.data, iv = _b.iv, additionalData = _b.additionalData, tagLength = _b.tagLength;
244
- return __generator(this, function (_c) {
245
- switch (_c.label) {
246
- case 0:
247
- // Validate the initialization vector length.
248
- if (iv.byteLength !== AES_GCM_IV_LENGTH / 8) {
249
- throw new TypeError("The initialization vector must be ".concat(AES_GCM_IV_LENGTH, " bits in length"));
250
- }
251
- // Validate the tag length.
252
- if (tagLength && !exports.AES_GCM_TAG_LENGTHS.includes(tagLength)) {
253
- throw new RangeError("The tag length is invalid: Must be ".concat(exports.AES_GCM_TAG_LENGTHS.join(', '), " bits"));
254
- }
255
- webCrypto = (0, webcrypto_1.getWebcryptoSubtle)();
256
- return [4 /*yield*/, webCrypto.importKey('jwk', key, { name: 'AES-GCM' }, true, ['decrypt'])];
257
- case 1:
258
- webCryptoKey = _c.sent();
259
- algorithm = __assign(__assign({ name: 'AES-GCM', iv: iv }, (tagLength && { tagLength: tagLength })), (additionalData && { additionalData: additionalData }));
260
- return [4 /*yield*/, webCrypto.decrypt(algorithm, webCryptoKey, data)];
261
- case 2:
262
- plaintextBuffer = _c.sent();
263
- plaintext = new Uint8Array(plaintextBuffer);
264
- return [2 /*return*/, plaintext];
265
- }
266
- });
267
- });
268
- };
269
- /**
270
- * Encrypts the provided data using AES-GCM.
271
- *
272
- * @remarks
273
- * This method performs AES-GCM encryption on the given data using the specified key.
274
- * It requires an initialization vector (IV), the encrypted data along with the decryption key,
275
- * and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
276
- * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
277
- * tag generated in the encryption operation and used for authentication in the corresponding
278
- * decryption. If not specified, the default tag length of 128 bits is used.
279
- *
280
- * @example
281
- * ```ts
282
- * const data = new TextEncoder().encode('Messsage');
283
- * const iv = new Uint8Array([...]); // Initialization vector
284
- * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
285
- * const key = { ... }; // A Jwk object representing an AES key
286
- * const encryptedData = await AesGcm.encrypt({
287
- * data,
288
- * iv,
289
- * additionalData,
290
- * key,
291
- * tagLength: 128 // Optional tag length in bits
292
- * });
293
- * ```
294
- *
295
- * @param params - The parameters for the encryption operation.
296
- * @param params.key - The key to use for encryption, represented in JWK format.
297
- * @param params.data - The data to encrypt, represented as a Uint8Array.
298
- * @param params.iv - The initialization vector, represented as a Uint8Array.
299
- * @param params.additionalData - Optional additional authenticated data. Optional.
300
- * @param params.tagLength - The length of the authentication tag in bits. Optional.
301
- *
302
- * @returns A Promise that resolves to the encrypted data as a Uint8Array.
303
- */
304
- AesGcm.encrypt = function (_a) {
305
- return __awaiter(this, arguments, void 0, function (_b) {
306
- var webCrypto, webCryptoKey, algorithm, ciphertextBuffer, ciphertext;
307
- var data = _b.data, iv = _b.iv, key = _b.key, additionalData = _b.additionalData, tagLength = _b.tagLength;
308
- return __generator(this, function (_c) {
309
- switch (_c.label) {
310
- case 0:
311
- // Validate the initialization vector length.
312
- if (iv.byteLength !== AES_GCM_IV_LENGTH / 8) {
313
- throw new TypeError("The initialization vector must be ".concat(AES_GCM_IV_LENGTH, " bits in length"));
314
- }
315
- // Validate the tag length.
316
- if (tagLength && !exports.AES_GCM_TAG_LENGTHS.includes(tagLength)) {
317
- throw new RangeError("The tag length is invalid: Must be ".concat(exports.AES_GCM_TAG_LENGTHS.join(', '), " bits"));
318
- }
319
- webCrypto = (0, webcrypto_1.getWebcryptoSubtle)();
320
- return [4 /*yield*/, webCrypto.importKey('jwk', key, { name: 'AES-GCM' }, true, ['encrypt'])];
321
- case 1:
322
- webCryptoKey = _c.sent();
323
- algorithm = __assign(__assign({ name: 'AES-GCM', iv: iv }, (tagLength && { tagLength: tagLength })), (additionalData && { additionalData: additionalData }));
324
- return [4 /*yield*/, webCrypto.encrypt(algorithm, webCryptoKey, data)];
325
- case 2:
326
- ciphertextBuffer = _c.sent();
327
- ciphertext = new Uint8Array(ciphertextBuffer);
328
- return [2 /*return*/, ciphertext];
329
- }
330
- });
331
- });
332
- };
333
- /**
334
- * Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
335
- *
336
- * @remarks
337
- * This method creates a new symmetric key of a specified length suitable for use with
338
- * AES-GCM encryption. It leverages cryptographically secure random number generation
339
- * to ensure the uniqueness and security of the key. The generated key adheres to the JWK
340
- * format, facilitating compatibility with common cryptographic standards and ease of use
341
- * in various cryptographic applications.
342
- *
343
- * The generated key includes these components:
344
- * - `kty`: Key Type, set to 'oct' for Octet Sequence, indicating a symmetric key.
345
- * - `k`: The symmetric key component, base64url-encoded.
346
- * - `kid`: Key ID, generated based on the JWK thumbprint, providing a unique identifier.
347
- *
348
- * @example
349
- * ```ts
350
- * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
351
- * const privateKey = await AesGcm.generateKey({ length });
352
- * ```
353
- *
354
- * @param params - The parameters for the key generation.
355
- * @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
356
- *
357
- * @returns A Promise that resolves to the generated symmetric key in JWK format.
358
- */
359
- AesGcm.generateKey = function (_a) {
360
- return __awaiter(this, arguments, void 0, function (_b) {
361
- var webCrypto, webCryptoKey, _c, ext, key_ops, privateKey, _d;
362
- var length = _b.length;
363
- return __generator(this, function (_e) {
364
- switch (_e.label) {
365
- case 0:
366
- // Validate the key length.
367
- if (!AES_KEY_LENGTHS.includes(length)) {
368
- throw new RangeError("The key length is invalid: Must be ".concat(AES_KEY_LENGTHS.join(', '), " bits"));
369
- }
370
- webCrypto = (0, webcrypto_1.getWebcryptoSubtle)();
371
- return [4 /*yield*/, webCrypto.generateKey({ name: 'AES-GCM', length: length }, true, ['encrypt'])];
372
- case 1:
373
- webCryptoKey = _e.sent();
374
- return [4 /*yield*/, webCrypto.exportKey('jwk', webCryptoKey)];
375
- case 2:
376
- _c = _e.sent(), ext = _c.ext, key_ops = _c.key_ops, privateKey = __rest(_c, ["ext", "key_ops"]);
377
- // Compute the JWK thumbprint and set as the key ID.
378
- _d = privateKey;
379
- return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
380
- case 3:
381
- // Compute the JWK thumbprint and set as the key ID.
382
- _d.kid = _e.sent();
383
- return [2 /*return*/, privateKey];
384
- }
385
- });
386
- });
387
- };
388
- /**
389
- * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
390
- *
391
- * @remarks
392
- * This method takes a symmetric key in JWK format and extracts its raw byte representation.
393
- * It focuses on the 'k' parameter of the JWK, which represents the symmetric key component
394
- * in base64url encoding. The method decodes this value into a byte array, providing
395
- * the symmetric key in its raw binary form.
396
- *
397
- * @example
398
- * ```ts
399
- * const privateKey = { ... }; // A symmetric key in JWK format
400
- * const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
401
- * ```
402
- *
403
- * @param params - The parameters for the symmetric key conversion.
404
- * @param params.privateKey - The symmetric key in JWK format.
405
- *
406
- * @returns A Promise that resolves to the symmetric key as a Uint8Array.
407
- */
408
- AesGcm.privateKeyToBytes = function (_a) {
409
- return __awaiter(this, arguments, void 0, function (_b) {
410
- var privateKeyBytes;
411
- var privateKey = _b.privateKey;
412
- return __generator(this, function (_c) {
413
- // Verify the provided JWK represents a valid oct private key.
414
- if (!(0, jwk_js_1.isOctPrivateJwk)(privateKey)) {
415
- throw new Error("AesGcm: The provided key is not a valid oct private key.");
416
- }
417
- privateKeyBytes = common_1.Convert.base64Url(privateKey.k).toUint8Array();
418
- return [2 /*return*/, privateKeyBytes];
419
- });
420
- });
421
- };
422
- return AesGcm;
423
- }());
424
- exports.AesGcm = AesGcm;
425
- //# sourceMappingURL=aes-gcm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"aes-gcm.js","sourceRoot":"","sources":["../../../src/primitives/aes-gcm.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAAwC;AACxC,sDAA8D;AAI9D,yCAAuE;AAEvE;;;;;;;;;;;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E,YAAY,CAAC,EAAA;;wBAAhF,KAAkC,SAA8C,EAA9E,GAAG,SAAA,EAAE,OAAO,aAAA,EAAK,UAAU,cAA7B,kBAA+B,CAAF;wBAEnC,oDAAoD;wBACpD,KAAA,UAAU,CAAA;wBAAO,qBAAM,IAAA,6BAAoB,EAAC,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,EAAA;;wBADhE,oDAAoD;wBACpD,GAAW,GAAG,GAAG,SAA+C,CAAC;wBAEjE,sBAAO,UAAU,EAAC;;;;KACnB;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACiB,wBAAiB,GAArC;4DAAsC,EAErC;;gBAFuC,UAAU,gBAAA;;gBAGhD,8DAA8D;gBAC9D,IAAI,CAAC,IAAA,wBAAe,EAAC,UAAU,CAAC,EAAE,CAAC;oBACjC,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;gBAC9E,CAAC;gBAGK,eAAe,GAAG,gBAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC;gBAEvE,sBAAO,eAAe,EAAC;;;KACxB;IACH,aAAC;AAAD,CAAC,AArRD,IAqRC;AArRY,wBAAM"}
@@ -1,215 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.ConcatKdf = void 0;
40
- var sha256_1 = require("@noble/hashes/sha256");
41
- var common_1 = require("@enbox/common");
42
- var utils_1 = require("@noble/hashes/utils");
43
- /**
44
- * An implementation of the Concatenation Key Derivation Function (ConcatKDF)
45
- * as specified in NIST.800-56A, a single-step key-derivation function (SSKDF).
46
- * ConcatKDF produces a derived key from a secret key (like a shared secret
47
- * from ECDH), and other optional public information. This implementation
48
- * specifically uses SHA-256 as the pseudorandom function (PRF).
49
- *
50
- * Note: This implementation allows for only a single round / repetition using the function
51
- * `K(1) = H(counter || Z || FixedInfo)`, where:
52
- * - `K(1)` is the derived key material after one round
53
- * - `H` is the SHA-256 hashing function
54
- * - `counter` is a 32-bit, big-endian bit string counter set to 0x00000001
55
- * - `Z` is the shared secret value obtained from a key agreement protocol
56
- * - `FixedInfo` is a bit string used to ensure that the derived keying material is adequately
57
- * "bound" to the key-agreement transaction.
58
- *
59
- * @example
60
- * ```ts
61
- * // Key Derivation
62
- * const derivedKeyingMaterial = await ConcatKdf.deriveKey({
63
- * sharedSecret: utils.randomBytes(32),
64
- * keyDataLen: 128,
65
- * fixedInfo: {
66
- * algorithmId: "A128GCM",
67
- * partyUInfo: "Alice",
68
- * partyVInfo: "Bob",
69
- * suppPubInfo: 128,
70
- * },
71
- * });
72
- * ```
73
- *
74
- * Additional Information:
75
- *
76
- * `Z`, or "shared secret":
77
- * The shared secret value obtained from a key agreement protocol, such as
78
- * Diffie-Hellman, ECDH (Elliptic Curve Diffie-Hellman). Importantly, this
79
- * shared secret is not directly used as the encryption or authentication
80
- * key, but as an input to a key derivation function (KDF), such as Concat
81
- * KDF, to generate the actual key. This adds an extra layer of security, as
82
- * even if the shared secret gets compromised, the actual encryption or
83
- * authentication key stays safe. This shared secret `Z` value is kept
84
- * confidential between the two parties in the key agreement protocol.
85
- *
86
- * @see {@link https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf | NIST.800-56A}
87
- * @see {@link https://datatracker.ietf.org/doc/html/rfc7518#section-4.6.2 | RFC 7518, Section 4.6.2}
88
- */
89
- var ConcatKdf = /** @class */ (function () {
90
- function ConcatKdf() {
91
- }
92
- /**
93
- * Derives a key of a specified length from the input parameters.
94
- *
95
- * @example
96
- * ```ts
97
- * // Key Derivation
98
- * const derivedKeyingMaterial = await ConcatKdf.deriveKey({
99
- * sharedSecret: utils.randomBytes(32),
100
- * keyDataLen: 128,
101
- * fixedInfo: {
102
- * algorithmId: "A128GCM",
103
- * partyUInfo: "Alice",
104
- * partyVInfo: "Bob",
105
- * suppPubInfo: 128,
106
- * },
107
- * });
108
- * ```
109
- *
110
- * @param params - Input parameters for key derivation.
111
- * @param params.keyDataLen - The desired length of the derived key in bits.
112
- * @param params.sharedSecret - The shared secret key to derive from.
113
- * @param params.fixedInfo - Additional public information to use in key derivation.
114
- * @returns The derived key as a Uint8Array.
115
- *
116
- * @throws {Error} If the `keyDataLen` would require multiple rounds.
117
- */
118
- ConcatKdf.deriveKey = function (_a) {
119
- return __awaiter(this, arguments, void 0, function (_b) {
120
- var hashLen, roundCount, counter, fixedInfoBytes, derivedKeyingMaterial;
121
- var keyDataLen = _b.keyDataLen, fixedInfo = _b.fixedInfo, sharedSecret = _b.sharedSecret;
122
- return __generator(this, function (_c) {
123
- hashLen = 256;
124
- roundCount = Math.ceil(keyDataLen / hashLen);
125
- if (roundCount !== 1) {
126
- throw new Error("Concat KDF with ".concat(roundCount, " rounds not supported."));
127
- }
128
- counter = new Uint8Array(4);
129
- new DataView(counter.buffer).setUint32(0, roundCount);
130
- fixedInfoBytes = ConcatKdf.computeFixedInfo(fixedInfo);
131
- derivedKeyingMaterial = (0, sha256_1.sha256)((0, utils_1.concatBytes)(counter, sharedSecret, fixedInfoBytes));
132
- // Return the bit string of derived keying material of length keyDataLen bits.
133
- return [2 /*return*/, derivedKeyingMaterial.slice(0, keyDataLen / 8)];
134
- });
135
- });
136
- };
137
- /**
138
- * Computes the `FixedInfo` parameter for Concat KDF, which binds the derived key material to the
139
- * context of the key agreement transaction.
140
- *
141
- * @remarks
142
- * This implementation follows the recommended format for `FixedInfo` specified in section
143
- * 5.8.1.2.1 of the NIST.800-56A publication.
144
- *
145
- * `FixedInfo` is a bit string equal to the following concatenation:
146
- * `AlgorithmID || PartyUInfo || PartyVInfo {|| SuppPubInfo }{|| SuppPrivInfo }`.
147
- *
148
- * `SuppPubInfo` is the key length in bits, big endian encoded as a 32-bit number. For example,
149
- * 128 would be [0, 0, 0, 128] and 256 would be [0, 0, 1, 0].
150
- *
151
- * @param params - Input data to construct FixedInfo.
152
- * @returns FixedInfo as a Uint8Array.
153
- */
154
- ConcatKdf.computeFixedInfo = function (params) {
155
- // Required sub-fields.
156
- var algorithmId = ConcatKdf.toDataLenData({ data: params.algorithmId });
157
- var partyUInfo = ConcatKdf.toDataLenData({ data: params.partyUInfo });
158
- var partyVInfo = ConcatKdf.toDataLenData({ data: params.partyVInfo });
159
- // Optional sub-fields.
160
- var suppPubInfo = ConcatKdf.toDataLenData({ data: params.suppPubInfo, variableLength: false });
161
- var suppPrivInfo = ConcatKdf.toDataLenData({ data: params.suppPrivInfo });
162
- // Concatenate AlgorithmID || PartyUInfo || PartyVInfo || SuppPubInfo || SuppPrivInfo.
163
- var fixedInfo = (0, utils_1.concatBytes)(algorithmId, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo);
164
- return fixedInfo;
165
- };
166
- /**
167
- * Encodes input data as a length-prefixed byte string, or
168
- * as a fixed-length bit string if specified.
169
- *
170
- * If variableLength = true, return the data in the form Datalen || Data,
171
- * where Data is a variable-length string of zero or more (eight-bit)
172
- * bytes, and Datalen is a fixed-length, big-endian counter that
173
- * indicates the length (in bytes) of Data.
174
- *
175
- * If variableLength = false, return the data formatted as a
176
- * fixed-length bit string.
177
- *
178
- * @param params - Input data and options for the conversion.
179
- * @param params.data - The input data to encode. Must be a type convertible to Uint8Array by the Convert class.
180
- * @param params.variableLength - Whether to output the data as variable length. Default is true.
181
- *
182
- * @returns The input data encoded as a Uint8Array.
183
- *
184
- * @throws {TypeError} If fixed-length data is not a number.
185
- */
186
- ConcatKdf.toDataLenData = function (_a) {
187
- var data = _a.data, _b = _a.variableLength, variableLength = _b === void 0 ? true : _b;
188
- var encodedData;
189
- var dataType = (0, common_1.universalTypeOf)(data);
190
- // Return an emtpy octet sequence if data is not specified.
191
- if (dataType === 'Undefined') {
192
- return new Uint8Array(0);
193
- }
194
- if (variableLength) {
195
- var dataU8A = (dataType === 'Uint8Array')
196
- ? data
197
- : new common_1.Convert(data, dataType).toUint8Array();
198
- var bufferLength = dataU8A.length;
199
- encodedData = new Uint8Array(4 + bufferLength);
200
- new DataView(encodedData.buffer).setUint32(0, bufferLength);
201
- encodedData.set(dataU8A, 4);
202
- }
203
- else {
204
- if (typeof data !== 'number') {
205
- throw TypeError('Fixed length input must be a number.');
206
- }
207
- encodedData = new Uint8Array(4);
208
- new DataView(encodedData.buffer).setUint32(0, data);
209
- }
210
- return encodedData;
211
- };
212
- return ConcatKdf;
213
- }());
214
- exports.ConcatKdf = ConcatKdf;
215
- //# sourceMappingURL=concat-kdf.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"concat-kdf.js","sourceRoot":"","sources":["../../../src/primitives/concat-kdf.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAA8C;AAC9C,wCAAyD;AACzD,6CAA8D;AA+C9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH;IAAA;IAgJA,CAAC;IA/IC;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACiB,mBAAS,GAA7B;4DAA8B,EAI7B;;gBAJ+B,UAAU,gBAAA,EAAE,SAAS,eAAA,EAAE,YAAY,kBAAA;;gBAS3D,OAAO,GAAG,GAAG,CAAC;gBAGd,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,CAAC;gBACnD,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;oBACrB,MAAM,IAAI,KAAK,CAAC,0BAAmB,UAAU,2BAAwB,CAAC,CAAC;gBACzE,CAAC;gBAGK,OAAO,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;gBAClC,IAAI,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;gBAGhD,cAAc,GAAG,SAAS,CAAC,gBAAgB,CAAC,SA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