one 2.6.20 → 2.6.21

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 (111) hide show
  1. package/cpp/HybridOneCrypto.cpp +327 -1
  2. package/cpp/HybridOneCrypto.hpp +8 -0
  3. package/dist/cjs/index.cjs +25 -22
  4. package/dist/cjs/index.native.cjs +25 -22
  5. package/dist/cjs/index.native.js +25 -22
  6. package/dist/cjs/index.native.js.map +1 -1
  7. package/dist/cjs/one.cjs +1 -0
  8. package/dist/cjs/one.native.cjs +1 -0
  9. package/dist/cjs/one.native.js +1 -0
  10. package/dist/cjs/one.native.js.map +1 -1
  11. package/dist/cjs/platform/crypto/aes.cjs +259 -0
  12. package/dist/cjs/platform/crypto/aes.native.cjs +343 -0
  13. package/dist/cjs/platform/crypto/aes.native.js +345 -0
  14. package/dist/cjs/platform/crypto/aes.native.js.map +1 -0
  15. package/dist/cjs/platform/crypto/bytes.cjs +132 -0
  16. package/dist/cjs/platform/crypto/bytes.native.cjs +175 -0
  17. package/dist/cjs/platform/crypto/bytes.native.js +177 -0
  18. package/dist/cjs/platform/crypto/bytes.native.js.map +1 -0
  19. package/dist/cjs/platform/crypto/digest-api.cjs +65 -0
  20. package/dist/cjs/platform/crypto/digest-api.native.cjs +69 -0
  21. package/dist/cjs/platform/crypto/digest-api.native.js +71 -0
  22. package/dist/cjs/platform/crypto/digest-api.native.js.map +1 -0
  23. package/dist/cjs/platform/crypto/digest.cjs +26 -15
  24. package/dist/cjs/platform/crypto/digest.native.cjs +26 -15
  25. package/dist/cjs/platform/crypto/digest.native.js +26 -15
  26. package/dist/cjs/platform/crypto/digest.native.js.map +1 -1
  27. package/dist/cjs/platform/crypto/index.cjs +19 -1
  28. package/dist/cjs/platform/crypto/index.native.cjs +39 -1
  29. package/dist/cjs/platform/crypto/index.native.js +39 -1
  30. package/dist/cjs/platform/crypto/index.native.js.map +1 -1
  31. package/dist/cjs/platform/crypto/types.cjs +16 -0
  32. package/dist/cjs/platform/crypto/types.native.cjs +18 -0
  33. package/dist/cjs/platform/crypto/types.native.js +20 -0
  34. package/dist/cjs/platform/crypto/types.native.js.map +1 -0
  35. package/dist/cjs/platform/crypto/web.cjs +73 -0
  36. package/dist/cjs/platform/extras.cjs +4 -0
  37. package/dist/cjs/platform/extras.native.cjs +4 -0
  38. package/dist/cjs/platform/extras.native.js +4 -0
  39. package/dist/cjs/platform/extras.native.js.map +1 -1
  40. package/dist/esm/index.js +2 -2
  41. package/dist/esm/index.mjs +2 -2
  42. package/dist/esm/index.native.js +2 -2
  43. package/dist/esm/one.mjs +2 -1
  44. package/dist/esm/one.mjs.map +1 -1
  45. package/dist/esm/one.native.js +2 -1
  46. package/dist/esm/one.native.js.map +1 -1
  47. package/dist/esm/platform/crypto/aes.mjs +236 -0
  48. package/dist/esm/platform/crypto/aes.mjs.map +1 -0
  49. package/dist/esm/platform/crypto/aes.native.js +318 -0
  50. package/dist/esm/platform/crypto/aes.native.js.map +1 -0
  51. package/dist/esm/platform/crypto/bytes.mjs +104 -0
  52. package/dist/esm/platform/crypto/bytes.mjs.map +1 -0
  53. package/dist/esm/platform/crypto/bytes.native.js +145 -0
  54. package/dist/esm/platform/crypto/bytes.native.js.map +1 -0
  55. package/dist/esm/platform/crypto/digest-api.mjs +42 -0
  56. package/dist/esm/platform/crypto/digest-api.mjs.map +1 -0
  57. package/dist/esm/platform/crypto/digest-api.native.js +44 -0
  58. package/dist/esm/platform/crypto/digest-api.native.js.map +1 -0
  59. package/dist/esm/platform/crypto/digest.mjs +23 -15
  60. package/dist/esm/platform/crypto/digest.mjs.map +1 -1
  61. package/dist/esm/platform/crypto/digest.native.js +23 -15
  62. package/dist/esm/platform/crypto/digest.native.js.map +1 -1
  63. package/dist/esm/platform/crypto/index.mjs +14 -1
  64. package/dist/esm/platform/crypto/index.mjs.map +1 -1
  65. package/dist/esm/platform/crypto/index.native.js +34 -1
  66. package/dist/esm/platform/crypto/index.native.js.map +1 -1
  67. package/dist/esm/platform/crypto/types.mjs +0 -0
  68. package/dist/esm/platform/crypto/types.native.js +0 -0
  69. package/dist/esm/platform/crypto/web.mjs +50 -0
  70. package/dist/esm/platform/crypto/web.mjs.map +1 -0
  71. package/dist/esm/platform/extras.mjs +2 -1
  72. package/dist/esm/platform/extras.native.js +2 -1
  73. package/nitrogen/generated/shared/c++/HybridOneCryptoSpec.cpp +2 -0
  74. package/nitrogen/generated/shared/c++/HybridOneCryptoSpec.hpp +4 -1
  75. package/package.json +12 -12
  76. package/src/index.ts +1 -0
  77. package/src/one.ts +3 -0
  78. package/src/platform/crypto/aes.ts +345 -0
  79. package/src/platform/crypto/bytes.ts +120 -0
  80. package/src/platform/crypto/digest-api.ts +50 -0
  81. package/src/platform/crypto/digest.ts +24 -15
  82. package/src/platform/crypto/index.native.ts +68 -0
  83. package/src/platform/crypto/index.ts +31 -0
  84. package/src/platform/crypto/types.ts +64 -0
  85. package/src/platform/crypto/web.ts +69 -0
  86. package/src/platform/extras.ts +15 -0
  87. package/src/platform/specs/OneCrypto.nitro.ts +19 -0
  88. package/types/index.d.ts +1 -0
  89. package/types/index.d.ts.map +1 -1
  90. package/types/one.d.ts +2 -1
  91. package/types/one.d.ts.map +1 -1
  92. package/types/platform/crypto/aes.d.ts +211 -0
  93. package/types/platform/crypto/aes.d.ts.map +1 -0
  94. package/types/platform/crypto/bytes.d.ts +8 -0
  95. package/types/platform/crypto/bytes.d.ts.map +1 -0
  96. package/types/platform/crypto/digest-api.d.ts +9 -0
  97. package/types/platform/crypto/digest-api.d.ts.map +1 -0
  98. package/types/platform/crypto/digest.d.ts +5 -0
  99. package/types/platform/crypto/digest.d.ts.map +1 -1
  100. package/types/platform/crypto/index.d.ts +127 -0
  101. package/types/platform/crypto/index.d.ts.map +1 -1
  102. package/types/platform/crypto/index.native.d.ts +127 -0
  103. package/types/platform/crypto/index.native.d.ts.map +1 -1
  104. package/types/platform/crypto/types.d.ts +43 -0
  105. package/types/platform/crypto/types.d.ts.map +1 -0
  106. package/types/platform/crypto/web.d.ts +5 -0
  107. package/types/platform/crypto/web.d.ts.map +1 -0
  108. package/types/platform/extras.d.ts +2 -0
  109. package/types/platform/extras.d.ts.map +1 -1
  110. package/types/platform/specs/OneCrypto.nitro.d.ts +2 -0
  111. package/types/platform/specs/OneCrypto.nitro.d.ts.map +1 -1
@@ -0,0 +1,343 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
+ get: () => from[key],
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
+ var aes_exports = {};
24
+ __export(aes_exports, {
25
+ assertTagLength: () => assertTagLength,
26
+ createAesApi: () => createAesApi,
27
+ toBytes: () => toBytes
28
+ });
29
+ module.exports = __toCommonJS(aes_exports);
30
+ var import_bytes = require("./bytes.native.js");
31
+ function _class_call_check(instance, Constructor) {
32
+ if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
33
+ }
34
+ function _defineProperties(target, props) {
35
+ for (var i = 0; i < props.length; i++) {
36
+ var descriptor = props[i];
37
+ descriptor.enumerable = descriptor.enumerable || false;
38
+ descriptor.configurable = true;
39
+ if ("value" in descriptor) descriptor.writable = true;
40
+ Object.defineProperty(target, descriptor.key, descriptor);
41
+ }
42
+ }
43
+ function _create_class(Constructor, protoProps, staticProps) {
44
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
45
+ if (staticProps) _defineProperties(Constructor, staticProps);
46
+ return Constructor;
47
+ }
48
+ function _define_property(obj, key, value) {
49
+ if (key in obj) {
50
+ Object.defineProperty(obj, key, {
51
+ value,
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true
55
+ });
56
+ } else obj[key] = value;
57
+ return obj;
58
+ }
59
+ var KEY_SIZES = [
60
+ 128,
61
+ 192,
62
+ 256
63
+ ];
64
+ var TAG_LENGTHS = [
65
+ 16,
66
+ 15,
67
+ 14,
68
+ 13,
69
+ 12,
70
+ 8,
71
+ 4
72
+ ];
73
+ var DEFAULT_IV_LENGTH = 12;
74
+ var DEFAULT_TAG_LENGTH = 16;
75
+ function assertTagLength(tagLength, verb) {
76
+ if (!TAG_LENGTHS.includes(tagLength)) {
77
+ throw new Error(`${verb}: tagLength must be one of: ${TAG_LENGTHS.join(", ")}`);
78
+ }
79
+ }
80
+ function toBytes(input, verb) {
81
+ if (typeof input === "string") return (0, import_bytes.base64ToBytes)(input, verb);
82
+ if (input instanceof ArrayBuffer) return new Uint8Array(input.slice(0));
83
+ if (ArrayBuffer.isView(input) && input.buffer instanceof ArrayBuffer) {
84
+ return (0, import_bytes.exactBytes)(new Uint8Array(input.buffer, input.byteOffset, input.byteLength));
85
+ }
86
+ throw new TypeError(`${verb}: expected a base64 string, Uint8Array, or ArrayBuffer`);
87
+ }
88
+ var sealedParts = /* @__PURE__ */ new WeakMap();
89
+ var keyBytes = /* @__PURE__ */ new WeakMap();
90
+ function encodeParts(bytes, encoding) {
91
+ return encoding === "base64" ? (0, import_bytes.bytesToBase64)(bytes) : (0, import_bytes.exactBytes)(bytes);
92
+ }
93
+ function createAesApi(engine) {
94
+ var AESEncryptionKey = /* @__PURE__ */ (function() {
95
+ "use strict";
96
+ function AESEncryptionKey2(bytes) {
97
+ _class_call_check(this, AESEncryptionKey2);
98
+ _define_property(this, "size", void 0);
99
+ if (!KEY_SIZES.includes(bytes.length * 8)) {
100
+ throw new Error(`Crypto.AESEncryptionKey: key must be 128, 192, or 256 bits, got ${bytes.length * 8}`);
101
+ }
102
+ this.size = bytes.length * 8;
103
+ keyBytes.set(this, (0, import_bytes.exactBytes)(bytes));
104
+ }
105
+ _create_class(AESEncryptionKey2, [{
106
+ key: "bytes",
107
+ value: async function bytes() {
108
+ return (0, import_bytes.exactBytes)(keyBytes.get(this));
109
+ }
110
+ }, {
111
+ key: "encoded",
112
+ value: async function encoded(encoding) {
113
+ var bytes = keyBytes.get(this);
114
+ if (encoding === "hex") return (0, import_bytes.bytesToHex)(bytes);
115
+ if (encoding === "base64") return (0, import_bytes.bytesToBase64)(bytes);
116
+ throw new Error(`Crypto.AESEncryptionKey.encoded: encoding must be 'hex' or 'base64'`);
117
+ }
118
+ }], [{
119
+ key: "generate",
120
+ value: async function generate() {
121
+ var size = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 256;
122
+ if (!KEY_SIZES.includes(size)) {
123
+ throw new Error(`Crypto.AESEncryptionKey.generate: size must be one of: ${KEY_SIZES.join(", ")}`);
124
+ }
125
+ var bytes = new Uint8Array(size / 8);
126
+ engine.random(bytes);
127
+ return new AESEncryptionKey2(bytes);
128
+ }
129
+ }, {
130
+ key: "import",
131
+ value: async function _import(input, encoding) {
132
+ var verb = "Crypto.AESEncryptionKey.import";
133
+ var bytes;
134
+ if (typeof input === "string") {
135
+ if (encoding === "hex") bytes = (0, import_bytes.hexToBytes)(input, verb);
136
+ else if (encoding === "base64") bytes = (0, import_bytes.base64ToBytes)(input, verb);
137
+ else throw new Error(`${verb}: encoding must be 'hex' or 'base64'`);
138
+ } else if (input instanceof Uint8Array) {
139
+ bytes = (0, import_bytes.exactBytes)(input);
140
+ } else {
141
+ throw new TypeError(`${verb}: expected bytes or an encoded string`);
142
+ }
143
+ if (!KEY_SIZES.includes(bytes.length * 8)) {
144
+ throw new Error(`${verb}: key must be 128, 192, or 256 bits, got ${bytes.length * 8}`);
145
+ }
146
+ return new AESEncryptionKey2(bytes);
147
+ }
148
+ }]);
149
+ return AESEncryptionKey2;
150
+ })();
151
+ var AESSealedData = /* @__PURE__ */ (function() {
152
+ "use strict";
153
+ function AESSealedData2(parts) {
154
+ _class_call_check(this, AESSealedData2);
155
+ if (parts.iv.length === 0) throw new Error("Crypto.AESSealedData: iv must not be empty");
156
+ assertTagLength(parts.tag.length, "Crypto.AESSealedData");
157
+ sealedParts.set(this, parts);
158
+ }
159
+ _create_class(AESSealedData2, [
160
+ {
161
+ key: "ivSize",
162
+ get: function get() {
163
+ return sealedParts.get(this).iv.length;
164
+ }
165
+ },
166
+ {
167
+ key: "tagSize",
168
+ get: function get() {
169
+ return sealedParts.get(this).tag.length;
170
+ }
171
+ },
172
+ {
173
+ key: "combinedSize",
174
+ get: function get() {
175
+ var parts = sealedParts.get(this);
176
+ return parts.iv.length + parts.ciphertext.length + parts.tag.length;
177
+ }
178
+ },
179
+ {
180
+ key: "iv",
181
+ value: async function iv(encoding) {
182
+ return encodeParts(sealedParts.get(this).iv, encoding);
183
+ }
184
+ },
185
+ {
186
+ key: "tag",
187
+ value: async function tag(encoding) {
188
+ return encodeParts(sealedParts.get(this).tag, encoding);
189
+ }
190
+ },
191
+ {
192
+ key: "ciphertext",
193
+ value: async function ciphertext(options) {
194
+ var parts = sealedParts.get(this);
195
+ var bytes = (options === null || options === void 0 ? void 0 : options.includeTag) ? (0, import_bytes.concatBytes)([parts.ciphertext, parts.tag]) : parts.ciphertext;
196
+ return encodeParts(bytes, options === null || options === void 0 ? void 0 : options.encoding);
197
+ }
198
+ },
199
+ {
200
+ key: "combined",
201
+ value: async function combined(encoding) {
202
+ var parts = sealedParts.get(this);
203
+ return encodeParts((0, import_bytes.concatBytes)([
204
+ parts.iv,
205
+ parts.ciphertext,
206
+ parts.tag
207
+ ]), encoding);
208
+ }
209
+ }
210
+ ], [
211
+ {
212
+ key: "fromCombined",
213
+ value: function fromCombined(combined) {
214
+ var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
215
+ var _config_ivLength, _config_tagLength;
216
+ var verb = "Crypto.AESSealedData.fromCombined";
217
+ var ivLength = (_config_ivLength = config.ivLength) !== null && _config_ivLength !== void 0 ? _config_ivLength : DEFAULT_IV_LENGTH;
218
+ var tagLength = (_config_tagLength = config.tagLength) !== null && _config_tagLength !== void 0 ? _config_tagLength : DEFAULT_TAG_LENGTH;
219
+ assertTagLength(tagLength, verb);
220
+ if (!Number.isInteger(ivLength) || ivLength <= 0) {
221
+ throw new Error(`${verb}: ivLength must be a positive integer`);
222
+ }
223
+ var bytes = toBytes(combined, verb);
224
+ if (bytes.length < ivLength + tagLength) {
225
+ throw new Error(`${verb}: combined data is shorter than its iv and tag`);
226
+ }
227
+ return new AESSealedData2({
228
+ iv: (0, import_bytes.exactBytes)(bytes.subarray(0, ivLength)),
229
+ ciphertext: (0, import_bytes.exactBytes)(bytes.subarray(ivLength, bytes.length - tagLength)),
230
+ tag: (0, import_bytes.exactBytes)(bytes.subarray(bytes.length - tagLength))
231
+ });
232
+ }
233
+ },
234
+ {
235
+ key: "fromParts",
236
+ value: function fromParts(ivInput, ciphertextInput, tagOrLength) {
237
+ var verb = "Crypto.AESSealedData.fromParts";
238
+ var iv = toBytes(ivInput, verb);
239
+ var ciphertext = toBytes(ciphertextInput, verb);
240
+ if (tagOrLength === void 0 || typeof tagOrLength === "number") {
241
+ var tagLength = tagOrLength !== null && tagOrLength !== void 0 ? tagOrLength : DEFAULT_TAG_LENGTH;
242
+ assertTagLength(tagLength, verb);
243
+ if (ciphertext.length < tagLength) {
244
+ throw new Error(`${verb}: ciphertext is shorter than its tag`);
245
+ }
246
+ return new AESSealedData2({
247
+ iv,
248
+ ciphertext: (0, import_bytes.exactBytes)(ciphertext.subarray(0, ciphertext.length - tagLength)),
249
+ tag: (0, import_bytes.exactBytes)(ciphertext.subarray(ciphertext.length - tagLength))
250
+ });
251
+ }
252
+ var tag = toBytes(tagOrLength, verb);
253
+ assertTagLength(tag.length, verb);
254
+ return new AESSealedData2({
255
+ iv,
256
+ ciphertext,
257
+ tag
258
+ });
259
+ }
260
+ },
261
+ {
262
+ key: "fromSeal",
263
+ value: (function fromSeal(iv, ciphertextWithTag, tagLength) {
264
+ return new AESSealedData2({
265
+ iv,
266
+ ciphertext: (0, import_bytes.exactBytes)(ciphertextWithTag.subarray(0, ciphertextWithTag.length - tagLength)),
267
+ tag: (0, import_bytes.exactBytes)(ciphertextWithTag.subarray(ciphertextWithTag.length - tagLength))
268
+ });
269
+ })
270
+ }
271
+ ]);
272
+ return AESSealedData2;
273
+ })();
274
+ async function keyMaterial(key, verb) {
275
+ var own = keyBytes.get(key);
276
+ if (own) return (0, import_bytes.exactBytes)(own);
277
+ if (key && typeof key.bytes === "function") return (0, import_bytes.exactBytes)(await key.bytes());
278
+ throw new TypeError(`${verb}: expected an AESEncryptionKey`);
279
+ }
280
+ async function partsOf(sealed, verb) {
281
+ var own = sealedParts.get(sealed);
282
+ if (own) {
283
+ return {
284
+ iv: (0, import_bytes.exactBytes)(own.iv),
285
+ ciphertext: (0, import_bytes.exactBytes)(own.ciphertext),
286
+ tag: (0, import_bytes.exactBytes)(own.tag)
287
+ };
288
+ }
289
+ if (sealed && typeof sealed.iv === "function" && typeof sealed.combined === "function") {
290
+ var iv = (0, import_bytes.exactBytes)(await sealed.iv());
291
+ var tag = (0, import_bytes.exactBytes)(await sealed.tag());
292
+ var ciphertext = (0, import_bytes.exactBytes)(await sealed.ciphertext({
293
+ encoding: "bytes",
294
+ includeTag: false
295
+ }));
296
+ return {
297
+ iv,
298
+ ciphertext,
299
+ tag
300
+ };
301
+ }
302
+ throw new TypeError(`${verb}: expected an AESSealedData`);
303
+ }
304
+ async function aesEncrypt(plaintext, key) {
305
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
306
+ var _options_tagLength;
307
+ var verb = "Crypto.aesEncrypt";
308
+ var tagLength = (_options_tagLength = options.tagLength) !== null && _options_tagLength !== void 0 ? _options_tagLength : DEFAULT_TAG_LENGTH;
309
+ assertTagLength(tagLength, verb);
310
+ var iv;
311
+ var nonce = options.nonce;
312
+ if (nonce && "bytes" in nonce && nonce.bytes !== void 0) {
313
+ iv = toBytes(nonce.bytes, verb);
314
+ if (iv.length === 0) throw new Error(`${verb}: nonce bytes must not be empty`);
315
+ } else {
316
+ var length = nonce && "length" in nonce && nonce.length !== void 0 ? nonce.length : DEFAULT_IV_LENGTH;
317
+ if (!Number.isInteger(length) || length <= 0) {
318
+ throw new Error(`${verb}: nonce length must be a positive integer`);
319
+ }
320
+ iv = new Uint8Array(length);
321
+ engine.random(iv);
322
+ }
323
+ var additionalData = options.additionalData === void 0 ? void 0 : toBytes(options.additionalData, verb);
324
+ var plainBytes = toBytes(plaintext, verb);
325
+ var sealed = await engine.seal(await keyMaterial(key, verb), iv, plainBytes, additionalData, tagLength);
326
+ var actualTag = sealed.length - plainBytes.length;
327
+ return AESSealedData.fromSeal(iv, sealed, actualTag > 0 ? actualTag : tagLength);
328
+ }
329
+ async function aesDecrypt(sealedData, key) {
330
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
331
+ var verb = "Crypto.aesDecrypt";
332
+ var parts = await partsOf(sealedData, verb);
333
+ var additionalData = options.additionalData === void 0 ? void 0 : toBytes(options.additionalData, verb);
334
+ var plaintext = await engine.open(await keyMaterial(key, verb), parts.iv, (0, import_bytes.concatBytes)([parts.ciphertext, parts.tag]), additionalData, parts.tag.length);
335
+ return options.output === "base64" ? (0, import_bytes.bytesToBase64)(plaintext) : plaintext;
336
+ }
337
+ return {
338
+ AESEncryptionKey,
339
+ AESSealedData,
340
+ aesEncrypt,
341
+ aesDecrypt
342
+ };
343
+ }
@@ -0,0 +1,345 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
16
+ get: () => from[key],
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
+ var aes_exports = {};
24
+ __export(aes_exports, {
25
+ assertTagLength: () => assertTagLength,
26
+ createAesApi: () => createAesApi,
27
+ toBytes: () => toBytes
28
+ });
29
+ module.exports = __toCommonJS(aes_exports);
30
+ var import_bytes = require("./bytes.native.js");
31
+ function _class_call_check(instance, Constructor) {
32
+ if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
33
+ }
34
+ function _defineProperties(target, props) {
35
+ for (var i = 0; i < props.length; i++) {
36
+ var descriptor = props[i];
37
+ descriptor.enumerable = descriptor.enumerable || false;
38
+ descriptor.configurable = true;
39
+ if ("value" in descriptor) descriptor.writable = true;
40
+ Object.defineProperty(target, descriptor.key, descriptor);
41
+ }
42
+ }
43
+ function _create_class(Constructor, protoProps, staticProps) {
44
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
45
+ if (staticProps) _defineProperties(Constructor, staticProps);
46
+ return Constructor;
47
+ }
48
+ function _define_property(obj, key, value) {
49
+ if (key in obj) {
50
+ Object.defineProperty(obj, key, {
51
+ value,
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true
55
+ });
56
+ } else obj[key] = value;
57
+ return obj;
58
+ }
59
+ var KEY_SIZES = [
60
+ 128,
61
+ 192,
62
+ 256
63
+ ];
64
+ var TAG_LENGTHS = [
65
+ 16,
66
+ 15,
67
+ 14,
68
+ 13,
69
+ 12,
70
+ 8,
71
+ 4
72
+ ];
73
+ var DEFAULT_IV_LENGTH = 12;
74
+ var DEFAULT_TAG_LENGTH = 16;
75
+ function assertTagLength(tagLength, verb) {
76
+ if (!TAG_LENGTHS.includes(tagLength)) {
77
+ throw new Error(`${verb}: tagLength must be one of: ${TAG_LENGTHS.join(", ")}`);
78
+ }
79
+ }
80
+ function toBytes(input, verb) {
81
+ if (typeof input === "string") return (0, import_bytes.base64ToBytes)(input, verb);
82
+ if (input instanceof ArrayBuffer) return new Uint8Array(input.slice(0));
83
+ if (ArrayBuffer.isView(input) && input.buffer instanceof ArrayBuffer) {
84
+ return (0, import_bytes.exactBytes)(new Uint8Array(input.buffer, input.byteOffset, input.byteLength));
85
+ }
86
+ throw new TypeError(`${verb}: expected a base64 string, Uint8Array, or ArrayBuffer`);
87
+ }
88
+ var sealedParts = /* @__PURE__ */ new WeakMap();
89
+ var keyBytes = /* @__PURE__ */ new WeakMap();
90
+ function encodeParts(bytes, encoding) {
91
+ return encoding === "base64" ? (0, import_bytes.bytesToBase64)(bytes) : (0, import_bytes.exactBytes)(bytes);
92
+ }
93
+ function createAesApi(engine) {
94
+ var AESEncryptionKey = /* @__PURE__ */ (function() {
95
+ "use strict";
96
+ function AESEncryptionKey2(bytes) {
97
+ _class_call_check(this, AESEncryptionKey2);
98
+ _define_property(this, "size", void 0);
99
+ if (!KEY_SIZES.includes(bytes.length * 8)) {
100
+ throw new Error(`Crypto.AESEncryptionKey: key must be 128, 192, or 256 bits, got ${bytes.length * 8}`);
101
+ }
102
+ this.size = bytes.length * 8;
103
+ keyBytes.set(this, (0, import_bytes.exactBytes)(bytes));
104
+ }
105
+ _create_class(AESEncryptionKey2, [{
106
+ key: "bytes",
107
+ value: async function bytes() {
108
+ return (0, import_bytes.exactBytes)(keyBytes.get(this));
109
+ }
110
+ }, {
111
+ key: "encoded",
112
+ value: async function encoded(encoding) {
113
+ var bytes = keyBytes.get(this);
114
+ if (encoding === "hex") return (0, import_bytes.bytesToHex)(bytes);
115
+ if (encoding === "base64") return (0, import_bytes.bytesToBase64)(bytes);
116
+ throw new Error(`Crypto.AESEncryptionKey.encoded: encoding must be 'hex' or 'base64'`);
117
+ }
118
+ }], [{
119
+ key: "generate",
120
+ value: async function generate() {
121
+ var size = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : 256;
122
+ if (!KEY_SIZES.includes(size)) {
123
+ throw new Error(`Crypto.AESEncryptionKey.generate: size must be one of: ${KEY_SIZES.join(", ")}`);
124
+ }
125
+ var bytes = new Uint8Array(size / 8);
126
+ engine.random(bytes);
127
+ return new AESEncryptionKey2(bytes);
128
+ }
129
+ }, {
130
+ key: "import",
131
+ value: async function _import(input, encoding) {
132
+ var verb = "Crypto.AESEncryptionKey.import";
133
+ var bytes;
134
+ if (typeof input === "string") {
135
+ if (encoding === "hex") bytes = (0, import_bytes.hexToBytes)(input, verb);
136
+ else if (encoding === "base64") bytes = (0, import_bytes.base64ToBytes)(input, verb);
137
+ else throw new Error(`${verb}: encoding must be 'hex' or 'base64'`);
138
+ } else if (input instanceof Uint8Array) {
139
+ bytes = (0, import_bytes.exactBytes)(input);
140
+ } else {
141
+ throw new TypeError(`${verb}: expected bytes or an encoded string`);
142
+ }
143
+ if (!KEY_SIZES.includes(bytes.length * 8)) {
144
+ throw new Error(`${verb}: key must be 128, 192, or 256 bits, got ${bytes.length * 8}`);
145
+ }
146
+ return new AESEncryptionKey2(bytes);
147
+ }
148
+ }]);
149
+ return AESEncryptionKey2;
150
+ })();
151
+ var AESSealedData = /* @__PURE__ */ (function() {
152
+ "use strict";
153
+ function AESSealedData2(parts) {
154
+ _class_call_check(this, AESSealedData2);
155
+ if (parts.iv.length === 0) throw new Error("Crypto.AESSealedData: iv must not be empty");
156
+ assertTagLength(parts.tag.length, "Crypto.AESSealedData");
157
+ sealedParts.set(this, parts);
158
+ }
159
+ _create_class(AESSealedData2, [
160
+ {
161
+ key: "ivSize",
162
+ get: function get() {
163
+ return sealedParts.get(this).iv.length;
164
+ }
165
+ },
166
+ {
167
+ key: "tagSize",
168
+ get: function get() {
169
+ return sealedParts.get(this).tag.length;
170
+ }
171
+ },
172
+ {
173
+ key: "combinedSize",
174
+ get: function get() {
175
+ var parts = sealedParts.get(this);
176
+ return parts.iv.length + parts.ciphertext.length + parts.tag.length;
177
+ }
178
+ },
179
+ {
180
+ key: "iv",
181
+ value: async function iv(encoding) {
182
+ return encodeParts(sealedParts.get(this).iv, encoding);
183
+ }
184
+ },
185
+ {
186
+ key: "tag",
187
+ value: async function tag(encoding) {
188
+ return encodeParts(sealedParts.get(this).tag, encoding);
189
+ }
190
+ },
191
+ {
192
+ key: "ciphertext",
193
+ value: async function ciphertext(options) {
194
+ var parts = sealedParts.get(this);
195
+ var bytes = (options === null || options === void 0 ? void 0 : options.includeTag) ? (0, import_bytes.concatBytes)([parts.ciphertext, parts.tag]) : parts.ciphertext;
196
+ return encodeParts(bytes, options === null || options === void 0 ? void 0 : options.encoding);
197
+ }
198
+ },
199
+ {
200
+ key: "combined",
201
+ value: async function combined(encoding) {
202
+ var parts = sealedParts.get(this);
203
+ return encodeParts((0, import_bytes.concatBytes)([
204
+ parts.iv,
205
+ parts.ciphertext,
206
+ parts.tag
207
+ ]), encoding);
208
+ }
209
+ }
210
+ ], [
211
+ {
212
+ key: "fromCombined",
213
+ value: function fromCombined(combined) {
214
+ var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
215
+ var _config_ivLength, _config_tagLength;
216
+ var verb = "Crypto.AESSealedData.fromCombined";
217
+ var ivLength = (_config_ivLength = config.ivLength) !== null && _config_ivLength !== void 0 ? _config_ivLength : DEFAULT_IV_LENGTH;
218
+ var tagLength = (_config_tagLength = config.tagLength) !== null && _config_tagLength !== void 0 ? _config_tagLength : DEFAULT_TAG_LENGTH;
219
+ assertTagLength(tagLength, verb);
220
+ if (!Number.isInteger(ivLength) || ivLength <= 0) {
221
+ throw new Error(`${verb}: ivLength must be a positive integer`);
222
+ }
223
+ var bytes = toBytes(combined, verb);
224
+ if (bytes.length < ivLength + tagLength) {
225
+ throw new Error(`${verb}: combined data is shorter than its iv and tag`);
226
+ }
227
+ return new AESSealedData2({
228
+ iv: (0, import_bytes.exactBytes)(bytes.subarray(0, ivLength)),
229
+ ciphertext: (0, import_bytes.exactBytes)(bytes.subarray(ivLength, bytes.length - tagLength)),
230
+ tag: (0, import_bytes.exactBytes)(bytes.subarray(bytes.length - tagLength))
231
+ });
232
+ }
233
+ },
234
+ {
235
+ key: "fromParts",
236
+ value: function fromParts(ivInput, ciphertextInput, tagOrLength) {
237
+ var verb = "Crypto.AESSealedData.fromParts";
238
+ var iv = toBytes(ivInput, verb);
239
+ var ciphertext = toBytes(ciphertextInput, verb);
240
+ if (tagOrLength === void 0 || typeof tagOrLength === "number") {
241
+ var tagLength = tagOrLength !== null && tagOrLength !== void 0 ? tagOrLength : DEFAULT_TAG_LENGTH;
242
+ assertTagLength(tagLength, verb);
243
+ if (ciphertext.length < tagLength) {
244
+ throw new Error(`${verb}: ciphertext is shorter than its tag`);
245
+ }
246
+ return new AESSealedData2({
247
+ iv,
248
+ ciphertext: (0, import_bytes.exactBytes)(ciphertext.subarray(0, ciphertext.length - tagLength)),
249
+ tag: (0, import_bytes.exactBytes)(ciphertext.subarray(ciphertext.length - tagLength))
250
+ });
251
+ }
252
+ var tag = toBytes(tagOrLength, verb);
253
+ assertTagLength(tag.length, verb);
254
+ return new AESSealedData2({
255
+ iv,
256
+ ciphertext,
257
+ tag
258
+ });
259
+ }
260
+ },
261
+ {
262
+ key: "fromSeal",
263
+ value: (function fromSeal(iv, ciphertextWithTag, tagLength) {
264
+ return new AESSealedData2({
265
+ iv,
266
+ ciphertext: (0, import_bytes.exactBytes)(ciphertextWithTag.subarray(0, ciphertextWithTag.length - tagLength)),
267
+ tag: (0, import_bytes.exactBytes)(ciphertextWithTag.subarray(ciphertextWithTag.length - tagLength))
268
+ });
269
+ })
270
+ }
271
+ ]);
272
+ return AESSealedData2;
273
+ })();
274
+ async function keyMaterial(key, verb) {
275
+ var own = keyBytes.get(key);
276
+ if (own) return (0, import_bytes.exactBytes)(own);
277
+ if (key && typeof key.bytes === "function") return (0, import_bytes.exactBytes)(await key.bytes());
278
+ throw new TypeError(`${verb}: expected an AESEncryptionKey`);
279
+ }
280
+ async function partsOf(sealed, verb) {
281
+ var own = sealedParts.get(sealed);
282
+ if (own) {
283
+ return {
284
+ iv: (0, import_bytes.exactBytes)(own.iv),
285
+ ciphertext: (0, import_bytes.exactBytes)(own.ciphertext),
286
+ tag: (0, import_bytes.exactBytes)(own.tag)
287
+ };
288
+ }
289
+ if (sealed && typeof sealed.iv === "function" && typeof sealed.combined === "function") {
290
+ var iv = (0, import_bytes.exactBytes)(await sealed.iv());
291
+ var tag = (0, import_bytes.exactBytes)(await sealed.tag());
292
+ var ciphertext = (0, import_bytes.exactBytes)(await sealed.ciphertext({
293
+ encoding: "bytes",
294
+ includeTag: false
295
+ }));
296
+ return {
297
+ iv,
298
+ ciphertext,
299
+ tag
300
+ };
301
+ }
302
+ throw new TypeError(`${verb}: expected an AESSealedData`);
303
+ }
304
+ async function aesEncrypt(plaintext, key) {
305
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
306
+ var _options_tagLength;
307
+ var verb = "Crypto.aesEncrypt";
308
+ var tagLength = (_options_tagLength = options.tagLength) !== null && _options_tagLength !== void 0 ? _options_tagLength : DEFAULT_TAG_LENGTH;
309
+ assertTagLength(tagLength, verb);
310
+ var iv;
311
+ var nonce = options.nonce;
312
+ if (nonce && "bytes" in nonce && nonce.bytes !== void 0) {
313
+ iv = toBytes(nonce.bytes, verb);
314
+ if (iv.length === 0) throw new Error(`${verb}: nonce bytes must not be empty`);
315
+ } else {
316
+ var length = nonce && "length" in nonce && nonce.length !== void 0 ? nonce.length : DEFAULT_IV_LENGTH;
317
+ if (!Number.isInteger(length) || length <= 0) {
318
+ throw new Error(`${verb}: nonce length must be a positive integer`);
319
+ }
320
+ iv = new Uint8Array(length);
321
+ engine.random(iv);
322
+ }
323
+ var additionalData = options.additionalData === void 0 ? void 0 : toBytes(options.additionalData, verb);
324
+ var plainBytes = toBytes(plaintext, verb);
325
+ var sealed = await engine.seal(await keyMaterial(key, verb), iv, plainBytes, additionalData, tagLength);
326
+ var actualTag = sealed.length - plainBytes.length;
327
+ return AESSealedData.fromSeal(iv, sealed, actualTag > 0 ? actualTag : tagLength);
328
+ }
329
+ async function aesDecrypt(sealedData, key) {
330
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
331
+ var verb = "Crypto.aesDecrypt";
332
+ var parts = await partsOf(sealedData, verb);
333
+ var additionalData = options.additionalData === void 0 ? void 0 : toBytes(options.additionalData, verb);
334
+ var plaintext = await engine.open(await keyMaterial(key, verb), parts.iv, (0, import_bytes.concatBytes)([parts.ciphertext, parts.tag]), additionalData, parts.tag.length);
335
+ return options.output === "base64" ? (0, import_bytes.bytesToBase64)(plaintext) : plaintext;
336
+ }
337
+ return {
338
+ AESEncryptionKey,
339
+ AESSealedData,
340
+ aesEncrypt,
341
+ aesDecrypt
342
+ };
343
+ }
344
+
345
+ //# sourceMappingURL=aes.native.js.map