@stryke/crypto 0.5.33 → 0.5.35

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 (56) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/base-64.cjs +440 -1
  3. package/dist/base-64.mjs +437 -1
  4. package/dist/base-64.mjs.map +1 -1
  5. package/dist/convert/src/array-buffer-to-string.cjs +19 -1
  6. package/dist/convert/src/array-buffer-to-string.mjs +18 -1
  7. package/dist/convert/src/array-buffer-to-string.mjs.map +1 -1
  8. package/dist/convert/src/neutral.cjs +7 -1
  9. package/dist/convert/src/neutral.mjs +9 -1
  10. package/dist/convert/src/parse-type-definition.cjs +1 -1
  11. package/dist/convert/src/parse-type-definition.mjs +3 -1
  12. package/dist/convert/src/string-to-uint8-array.cjs +24 -1
  13. package/dist/convert/src/string-to-uint8-array.mjs +22 -1
  14. package/dist/convert/src/string-to-uint8-array.mjs.map +1 -1
  15. package/dist/convert/src/string-to-utf8-array.cjs +15 -1
  16. package/dist/convert/src/string-to-utf8-array.mjs +14 -1
  17. package/dist/convert/src/string-to-utf8-array.mjs.map +1 -1
  18. package/dist/convert/src/uint8-array-to-stream.cjs +22 -1
  19. package/dist/convert/src/uint8-array-to-stream.mjs +21 -1
  20. package/dist/convert/src/uint8-array-to-stream.mjs.map +1 -1
  21. package/dist/convert/src/uint8-array-to-string.cjs +15 -1
  22. package/dist/convert/src/uint8-array-to-string.mjs +15 -1
  23. package/dist/convert/src/uint8-array-to-string.mjs.map +1 -1
  24. package/dist/convert/src/utf8-array-to-string.cjs +15 -1
  25. package/dist/convert/src/utf8-array-to-string.mjs +14 -1
  26. package/dist/convert/src/utf8-array-to-string.mjs.map +1 -1
  27. package/dist/encryption.cjs +134 -1
  28. package/dist/encryption.mjs +128 -1
  29. package/dist/encryption.mjs.map +1 -1
  30. package/dist/encryption.node.cjs +63 -1
  31. package/dist/encryption.node.mjs +61 -1
  32. package/dist/encryption.node.mjs.map +1 -1
  33. package/dist/hex.cjs +62 -1
  34. package/dist/hex.mjs +60 -1
  35. package/dist/hex.mjs.map +1 -1
  36. package/dist/index.cjs +13 -1
  37. package/dist/index.mjs +5 -1
  38. package/dist/neutral.cjs +17 -1
  39. package/dist/neutral.mjs +5 -1
  40. package/dist/type-checks/src/index.cjs +5 -1
  41. package/dist/type-checks/src/index.mjs +7 -1
  42. package/dist/type-checks/src/is-buffer.cjs +12 -1
  43. package/dist/type-checks/src/is-buffer.mjs +11 -1
  44. package/dist/type-checks/src/is-buffer.mjs.map +1 -1
  45. package/dist/type-checks/src/is-collection.cjs +1 -1
  46. package/dist/type-checks/src/is-collection.mjs +3 -1
  47. package/dist/type-checks/src/is-string.cjs +12 -1
  48. package/dist/type-checks/src/is-string.mjs +11 -1
  49. package/dist/type-checks/src/is-string.mjs.map +1 -1
  50. package/dist/type-checks/src/is-undefined.cjs +8 -1
  51. package/dist/type-checks/src/is-undefined.mjs +7 -1
  52. package/dist/type-checks/src/is-undefined.mjs.map +1 -1
  53. package/dist/type-checks/src/type-detect.cjs +15 -1
  54. package/dist/type-checks/src/type-detect.mjs +16 -1
  55. package/dist/type-checks/src/type-detect.mjs.map +1 -1
  56. package/package.json +5 -5
@@ -1,2 +1,15 @@
1
- const e=new TextEncoder;function t(t){return e.encode(t)}export{t as stringToUtf8Array};
1
+ //#region ../convert/src/string-to-utf8-array.ts
2
+ const encoder = new TextEncoder();
3
+ /**
4
+ * Convert a string to a utf-8 array
5
+ *
6
+ * @param input - The string to convert
7
+ * @returns The converted utf-8 array
8
+ */
9
+ function stringToUtf8Array(input) {
10
+ return encoder.encode(input);
11
+ }
12
+
13
+ //#endregion
14
+ export { stringToUtf8Array };
2
15
  //# sourceMappingURL=string-to-utf8-array.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"string-to-utf8-array.mjs","names":[],"sources":["../../../../convert/src/string-to-utf8-array.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst encoder = new TextEncoder();\n\n/**\n * Convert a string to a utf-8 array\n *\n * @param input - The string to convert\n * @returns The converted utf-8 array\n */\nexport function stringToUtf8Array(input: string): Uint8Array {\n return encoder.encode(input);\n}\n"],"mappings":"AAkBA,MAAM,EAAU,IAAI,YAQpB,SAAgB,EAAkB,EAA2B,CAC3D,OAAO,EAAQ,OAAO,EAAM"}
1
+ {"version":3,"file":"string-to-utf8-array.mjs","names":[],"sources":["../../../../convert/src/string-to-utf8-array.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst encoder = new TextEncoder();\n\n/**\n * Convert a string to a utf-8 array\n *\n * @param input - The string to convert\n * @returns The converted utf-8 array\n */\nexport function stringToUtf8Array(input: string): Uint8Array {\n return encoder.encode(input);\n}\n"],"mappings":";AAkBA,MAAM,UAAU,IAAI,aAAa;;;;;;;AAQjC,SAAgB,kBAAkB,OAA2B;AAC3D,QAAO,QAAQ,OAAO,MAAM"}
@@ -1 +1,22 @@
1
- function e(e){let t=0;for(let n of e)t+=n.length;let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;return n}exports.concatUint8Array=e;
1
+
2
+ //#region ../convert/src/uint8-array-to-stream.ts
3
+ /**
4
+ * Concatenate an array of Uint8Array chunks into a single Uint8Array
5
+ *
6
+ * @param chunks - Array of Uint8Array chunks to concatenate
7
+ * @returns The concatenated Uint8Array
8
+ */
9
+ function concatUint8Array(chunks) {
10
+ let total = 0;
11
+ for (const chunk of chunks) total += chunk.length;
12
+ const result = new Uint8Array(total);
13
+ let offset = 0;
14
+ for (const chunk of chunks) {
15
+ result.set(chunk, offset);
16
+ offset += chunk.length;
17
+ }
18
+ return result;
19
+ }
20
+
21
+ //#endregion
22
+ exports.concatUint8Array = concatUint8Array;
@@ -1,2 +1,22 @@
1
- function e(e){let t=0;for(let n of e)t+=n.length;let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;return n}export{e as concatUint8Array};
1
+ //#region ../convert/src/uint8-array-to-stream.ts
2
+ /**
3
+ * Concatenate an array of Uint8Array chunks into a single Uint8Array
4
+ *
5
+ * @param chunks - Array of Uint8Array chunks to concatenate
6
+ * @returns The concatenated Uint8Array
7
+ */
8
+ function concatUint8Array(chunks) {
9
+ let total = 0;
10
+ for (const chunk of chunks) total += chunk.length;
11
+ const result = new Uint8Array(total);
12
+ let offset = 0;
13
+ for (const chunk of chunks) {
14
+ result.set(chunk, offset);
15
+ offset += chunk.length;
16
+ }
17
+ return result;
18
+ }
19
+
20
+ //#endregion
21
+ export { concatUint8Array };
2
22
  //# sourceMappingURL=uint8-array-to-stream.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"uint8-array-to-stream.mjs","names":[],"sources":["../../../../convert/src/uint8-array-to-stream.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Convert a Uint8Array to a ReadableStream\n *\n * @param data - The Uint8Array to convert\n * @returns The converted ReadableStream\n */\nexport function uint8ArrayToStream(\n data: Uint8Array\n): ReadableStream<Uint8Array> {\n return new ReadableStream({\n start(controller) {\n controller.enqueue(data);\n controller.close();\n }\n });\n}\n\n/**\n * Concatenate an array of Uint8Array chunks into a single Uint8Array\n *\n * @param chunks - Array of Uint8Array chunks to concatenate\n * @returns The concatenated Uint8Array\n */\nexport function concatUint8Array(chunks: Uint8Array[]): Uint8Array {\n let total = 0;\n for (const chunk of chunks) {\n total += chunk.length;\n }\n const result = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n return result;\n}\n\n/**\n * Concatenate a ReadableStream of Uint8Array chunks into a single Uint8Array\n *\n * @param stream - The ReadableStream of Uint8Array chunks to concatenate\n * @returns A promise that resolves to the concatenated Uint8Array\n */\nexport async function concatUint8ArrayStream(\n stream: ReadableStream<Uint8Array>\n): Promise<Uint8Array> {\n const chunks: Uint8Array[] = [];\n await stream.pipeTo(\n new WritableStream({\n write(chunk) {\n chunks.push(chunk);\n }\n })\n );\n\n return concatUint8Array(chunks);\n}\n"],"mappings":"AAyCA,SAAgB,EAAiB,EAAkC,CACjE,IAAI,EAAQ,EACZ,IAAK,IAAM,KAAS,EAClB,GAAS,EAAM,OAEjB,IAAM,EAAS,IAAI,WAAW,EAAM,CAChC,EAAS,EACb,IAAK,IAAM,KAAS,EAClB,EAAO,IAAI,EAAO,EAAO,CACzB,GAAU,EAAM,OAElB,OAAO"}
1
+ {"version":3,"file":"uint8-array-to-stream.mjs","names":[],"sources":["../../../../convert/src/uint8-array-to-stream.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Convert a Uint8Array to a ReadableStream\n *\n * @param data - The Uint8Array to convert\n * @returns The converted ReadableStream\n */\nexport function uint8ArrayToStream(\n data: Uint8Array\n): ReadableStream<Uint8Array> {\n return new ReadableStream({\n start(controller) {\n controller.enqueue(data);\n controller.close();\n }\n });\n}\n\n/**\n * Concatenate an array of Uint8Array chunks into a single Uint8Array\n *\n * @param chunks - Array of Uint8Array chunks to concatenate\n * @returns The concatenated Uint8Array\n */\nexport function concatUint8Array(chunks: Uint8Array[]): Uint8Array {\n let total = 0;\n for (const chunk of chunks) {\n total += chunk.length;\n }\n const result = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n result.set(chunk, offset);\n offset += chunk.length;\n }\n return result;\n}\n\n/**\n * Concatenate a ReadableStream of Uint8Array chunks into a single Uint8Array\n *\n * @param stream - The ReadableStream of Uint8Array chunks to concatenate\n * @returns A promise that resolves to the concatenated Uint8Array\n */\nexport async function concatUint8ArrayStream(\n stream: ReadableStream<Uint8Array>\n): Promise<Uint8Array> {\n const chunks: Uint8Array[] = [];\n await stream.pipeTo(\n new WritableStream({\n write(chunk) {\n chunks.push(chunk);\n }\n })\n );\n\n return concatUint8Array(chunks);\n}\n"],"mappings":";;;;;;;AAyCA,SAAgB,iBAAiB,QAAkC;CACjE,IAAI,QAAQ;AACZ,MAAK,MAAM,SAAS,OAClB,UAAS,MAAM;CAEjB,MAAM,SAAS,IAAI,WAAW,MAAM;CACpC,IAAI,SAAS;AACb,MAAK,MAAM,SAAS,QAAQ;AAC1B,SAAO,IAAI,OAAO,OAAO;AACzB,YAAU,MAAM;;AAElB,QAAO"}
@@ -1 +1,15 @@
1
- const e=require(`./array-buffer-to-string.cjs`);function t(t){return btoa(e.arrayBufferToString(t))}exports.uint8ArrayToString=t;
1
+ const require_array_buffer_to_string = require('./array-buffer-to-string.cjs');
2
+
3
+ //#region ../convert/src/uint8-array-to-string.ts
4
+ /**
5
+ * Convert a Uint8Array to a base64 string
6
+ *
7
+ * @param buffer - The Uint8Array to convert
8
+ * @returns The converted base64 string
9
+ */
10
+ function uint8ArrayToString(buffer) {
11
+ return btoa(require_array_buffer_to_string.arrayBufferToString(buffer));
12
+ }
13
+
14
+ //#endregion
15
+ exports.uint8ArrayToString = uint8ArrayToString;
@@ -1,2 +1,16 @@
1
- import{arrayBufferToString as e}from"./array-buffer-to-string.mjs";function t(t){return btoa(e(t))}export{t as uint8ArrayToString};
1
+ import { arrayBufferToString } from "./array-buffer-to-string.mjs";
2
+
3
+ //#region ../convert/src/uint8-array-to-string.ts
4
+ /**
5
+ * Convert a Uint8Array to a base64 string
6
+ *
7
+ * @param buffer - The Uint8Array to convert
8
+ * @returns The converted base64 string
9
+ */
10
+ function uint8ArrayToString(buffer) {
11
+ return btoa(arrayBufferToString(buffer));
12
+ }
13
+
14
+ //#endregion
15
+ export { uint8ArrayToString };
2
16
  //# sourceMappingURL=uint8-array-to-string.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"uint8-array-to-string.mjs","names":[],"sources":["../../../../convert/src/uint8-array-to-string.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { arrayBufferToString } from \"./array-buffer-to-string\";\n\n/**\n * Convert a Uint8Array to a base64 string\n *\n * @param buffer - The Uint8Array to convert\n * @returns The converted base64 string\n */\nexport function uint8ArrayToString(buffer: Uint8Array): string {\n return btoa(arrayBufferToString(buffer));\n}\n"],"mappings":"mEA0BA,SAAgB,EAAmB,EAA4B,CAC7D,OAAO,KAAK,EAAoB,EAAO,CAAC"}
1
+ {"version":3,"file":"uint8-array-to-string.mjs","names":[],"sources":["../../../../convert/src/uint8-array-to-string.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { arrayBufferToString } from \"./array-buffer-to-string\";\n\n/**\n * Convert a Uint8Array to a base64 string\n *\n * @param buffer - The Uint8Array to convert\n * @returns The converted base64 string\n */\nexport function uint8ArrayToString(buffer: Uint8Array): string {\n return btoa(arrayBufferToString(buffer));\n}\n"],"mappings":";;;;;;;;;AA0BA,SAAgB,mBAAmB,QAA4B;AAC7D,QAAO,KAAK,oBAAoB,OAAO,CAAC"}
@@ -1 +1,15 @@
1
- const e=new TextDecoder;function t(t){return e.decode(t)}exports.utf8ArrayToString=t;
1
+
2
+ //#region ../convert/src/utf8-array-to-string.ts
3
+ const decoder = new TextDecoder();
4
+ /**
5
+ * Convert a utf-8 array to string
6
+ *
7
+ * @param input - Utf-8 Array
8
+ * @returns The converted string
9
+ */
10
+ function utf8ArrayToString(input) {
11
+ return decoder.decode(input);
12
+ }
13
+
14
+ //#endregion
15
+ exports.utf8ArrayToString = utf8ArrayToString;
@@ -1,2 +1,15 @@
1
- const e=new TextDecoder;function t(t){return e.decode(t)}export{t as utf8ArrayToString};
1
+ //#region ../convert/src/utf8-array-to-string.ts
2
+ const decoder = new TextDecoder();
3
+ /**
4
+ * Convert a utf-8 array to string
5
+ *
6
+ * @param input - Utf-8 Array
7
+ * @returns The converted string
8
+ */
9
+ function utf8ArrayToString(input) {
10
+ return decoder.decode(input);
11
+ }
12
+
13
+ //#endregion
14
+ export { utf8ArrayToString };
2
15
  //# sourceMappingURL=utf8-array-to-string.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utf8-array-to-string.mjs","names":[],"sources":["../../../../convert/src/utf8-array-to-string.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst decoder = new TextDecoder();\n\n/**\n * Convert a utf-8 array to string\n *\n * @param input - Utf-8 Array\n * @returns The converted string\n */\nexport function utf8ArrayToString(\n input: NodeJS.ArrayBufferView | ArrayBuffer\n): string {\n return decoder.decode(input);\n}\n"],"mappings":"AAkBA,MAAM,EAAU,IAAI,YAQpB,SAAgB,EACd,EACQ,CACR,OAAO,EAAQ,OAAO,EAAM"}
1
+ {"version":3,"file":"utf8-array-to-string.mjs","names":[],"sources":["../../../../convert/src/utf8-array-to-string.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nconst decoder = new TextDecoder();\n\n/**\n * Convert a utf-8 array to string\n *\n * @param input - Utf-8 Array\n * @returns The converted string\n */\nexport function utf8ArrayToString(\n input: NodeJS.ArrayBufferView | ArrayBuffer\n): string {\n return decoder.decode(input);\n}\n"],"mappings":";AAkBA,MAAM,UAAU,IAAI,aAAa;;;;;;;AAQjC,SAAgB,kBACd,OACQ;AACR,QAAO,QAAQ,OAAO,MAAM"}
@@ -1 +1,134 @@
1
- const e=require(`./convert/src/string-to-uint8-array.cjs`),t=require(`./convert/src/string-to-utf8-array.cjs`),n=require(`./convert/src/uint8-array-to-stream.cjs`),r=require(`./convert/src/uint8-array-to-string.cjs`),i=require(`./convert/src/utf8-array-to-string.cjs`);require(`./convert/src/neutral.cjs`);const a=require(`./base-64.cjs`),o=require(`./hex.cjs`);async function s(){return crypto.subtle.generateKey({name:`AES-GCM`,length:256},!0,[`encrypt`,`decrypt`])}async function c(e){let t=await crypto.subtle.exportKey(`raw`,e);return a.encodeBase64(new Uint8Array(t)).toString()}async function l(e){let t=a.decodeBase64(e);return crypto.subtle.importKey(`raw`,t.buffer,`AES-GCM`,!0,[`encrypt`,`decrypt`])}const u=24;async function d(e,n){let r=crypto.getRandomValues(new Uint8Array(24/2)),i=await crypto.subtle.encrypt({name:`AES-GCM`,iv:r},e,t.stringToUtf8Array(n));return o.encodeHex(r)+a.encodeBase64(new Uint8Array(i))}async function f(e,t){return i.utf8ArrayToString(await crypto.subtle.decrypt({name:`AES-GCM`,iv:o.decodeHex(t.slice(0,24))},e,a.decodeBase64(t.slice(24))))}async function p(e,t){let i=crypto.getRandomValues(new Uint8Array(16)),a=await crypto.subtle.encrypt({name:`AES-GCM`,iv:i},e,t);return r.uint8ArrayToString(n.concatUint8Array([i,new Uint8Array(a)]))}async function m(t,n){let r=e.base64StringToUint8Array(n);return crypto.subtle.decrypt({name:`AES-GCM`,iv:r.slice(0,16)},t,r.slice(16))}exports.createKey=s,exports.decodeKey=l,exports.decrypt=f,exports.decryptBuffer=m,exports.encodeKey=c,exports.encrypt=d,exports.encryptBuffer=p;
1
+ const require_string_to_uint8_array = require('./convert/src/string-to-uint8-array.cjs');
2
+ const require_string_to_utf8_array = require('./convert/src/string-to-utf8-array.cjs');
3
+ const require_uint8_array_to_stream = require('./convert/src/uint8-array-to-stream.cjs');
4
+ const require_uint8_array_to_string = require('./convert/src/uint8-array-to-string.cjs');
5
+ const require_utf8_array_to_string = require('./convert/src/utf8-array-to-string.cjs');
6
+ require('./convert/src/neutral.cjs');
7
+ const require_base_64 = require('./base-64.cjs');
8
+ const require_hex = require('./hex.cjs');
9
+
10
+ //#region src/encryption.ts
11
+ /**
12
+ * Creates a CryptoKey object that can be used to encrypt any string.
13
+ *
14
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
15
+ *
16
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.
17
+ */
18
+ async function createKey() {
19
+ return crypto.subtle.generateKey({
20
+ name: "AES-GCM",
21
+ length: 256
22
+ }, true, ["encrypt", "decrypt"]);
23
+ }
24
+ /**
25
+ * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
26
+ *
27
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
28
+ *
29
+ * @param key - The CryptoKey to encode
30
+ * @returns A promise that resolves to a base64 string representing the key
31
+ */
32
+ async function encodeKey(key) {
33
+ const exported = await crypto.subtle.exportKey("raw", key);
34
+ return require_base_64.encodeBase64(new Uint8Array(exported)).toString();
35
+ }
36
+ /**
37
+ * Decodes a base64 string into bytes and then imports the key.
38
+ *
39
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
40
+ *
41
+ * @param encoded - The base64 encoded key
42
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings
43
+ */
44
+ async function decodeKey(encoded) {
45
+ const bytes = require_base_64.decodeBase64(encoded);
46
+ return crypto.subtle.importKey("raw", bytes.buffer, "AES-GCM", true, ["encrypt", "decrypt"]);
47
+ }
48
+ const IV_LENGTH = 24;
49
+ /**
50
+ * Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.
51
+ *
52
+ * @remarks
53
+ * The initialization vector is randomly generated and prepended to the encrypted string. The IV is required for decryption, so it must be stored alongside the encrypted data.
54
+ *
55
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
56
+ *
57
+ * @param key - The CryptoKey to use for encryption
58
+ * @param plaintext - The plaintext string to encrypt
59
+ * @returns A promise that resolves to a base64 string representing the encrypted data
60
+ */
61
+ async function encrypt(key, plaintext) {
62
+ const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));
63
+ const encrypted = await crypto.subtle.encrypt({
64
+ name: "AES-GCM",
65
+ iv
66
+ }, key, require_string_to_utf8_array.stringToUtf8Array(plaintext));
67
+ return require_hex.encodeHex(iv) + require_base_64.encodeBase64(new Uint8Array(encrypted));
68
+ }
69
+ /**
70
+ * Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.
71
+ *
72
+ * @remarks
73
+ * The initialization vector is expected to be prepended to the encrypted string. The IV is required for decryption, so it must be extracted from the start of the string.
74
+ *
75
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
76
+ *
77
+ * @param key - The CryptoKey to use for decryption
78
+ * @param encrypted - The encrypted base64 encoded string to decrypt
79
+ * @returns A promise that resolves to the decrypted string
80
+ */
81
+ async function decrypt(key, encrypted) {
82
+ return require_utf8_array_to_string.utf8ArrayToString(await crypto.subtle.decrypt({
83
+ name: "AES-GCM",
84
+ iv: require_hex.decodeHex(encrypted.slice(0, IV_LENGTH))
85
+ }, key, require_base_64.decodeBase64(encrypted.slice(IV_LENGTH))));
86
+ }
87
+ /**
88
+ * Encrypts a buffer using AES-GCM with a given CryptoKey.
89
+ *
90
+ * @remarks
91
+ * The initialization vector (IV) is randomly generated and prepended to the encrypted data. The resulting data is then encoded as a base64 string for easy storage/transmission.
92
+ *
93
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
94
+ *
95
+ * @param key - The CryptoKey to use for encryption
96
+ * @param buffer - The buffer to encrypt
97
+ * @returns A promise that resolves to a base64 string representing the encrypted data
98
+ */
99
+ async function encryptBuffer(key, buffer) {
100
+ const iv = crypto.getRandomValues(new Uint8Array(16));
101
+ const encrypted = await crypto.subtle.encrypt({
102
+ name: "AES-GCM",
103
+ iv
104
+ }, key, buffer);
105
+ return require_uint8_array_to_string.uint8ArrayToString(require_uint8_array_to_stream.concatUint8Array([iv, new Uint8Array(encrypted)]));
106
+ }
107
+ /**
108
+ * Decrypts a buffer using AES-GCM with a given CryptoKey.
109
+ *
110
+ * @remarks
111
+ * The initialization vector (IV) is expected to be prepended to the encrypted data. The IV is required for decryption, so it must be extracted from the start of the buffer.
112
+ *
113
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
114
+ *
115
+ * @param key - The CryptoKey to use for decryption
116
+ * @param encrypted - The encrypted base64 encoded string to decrypt
117
+ * @returns A promise that resolves to the decrypted string
118
+ */
119
+ async function decryptBuffer(key, encrypted) {
120
+ const concatenated = require_string_to_uint8_array.base64StringToUint8Array(encrypted);
121
+ return crypto.subtle.decrypt({
122
+ name: "AES-GCM",
123
+ iv: concatenated.slice(0, 16)
124
+ }, key, concatenated.slice(16));
125
+ }
126
+
127
+ //#endregion
128
+ exports.createKey = createKey;
129
+ exports.decodeKey = decodeKey;
130
+ exports.decrypt = decrypt;
131
+ exports.decryptBuffer = decryptBuffer;
132
+ exports.encodeKey = encodeKey;
133
+ exports.encrypt = encrypt;
134
+ exports.encryptBuffer = encryptBuffer;
@@ -1,2 +1,129 @@
1
- import{base64StringToUint8Array as e}from"./convert/src/string-to-uint8-array.mjs";import{stringToUtf8Array as t}from"./convert/src/string-to-utf8-array.mjs";import{concatUint8Array as n}from"./convert/src/uint8-array-to-stream.mjs";import{uint8ArrayToString as r}from"./convert/src/uint8-array-to-string.mjs";import{utf8ArrayToString as i}from"./convert/src/utf8-array-to-string.mjs";import"./convert/src/neutral.mjs";import{decodeBase64 as a,encodeBase64 as o}from"./base-64.mjs";import{decodeHex as s,encodeHex as c}from"./hex.mjs";async function l(){return crypto.subtle.generateKey({name:`AES-GCM`,length:256},!0,[`encrypt`,`decrypt`])}async function u(e){let t=await crypto.subtle.exportKey(`raw`,e);return o(new Uint8Array(t)).toString()}async function d(e){let t=a(e);return crypto.subtle.importKey(`raw`,t.buffer,`AES-GCM`,!0,[`encrypt`,`decrypt`])}async function f(e,n){let r=crypto.getRandomValues(new Uint8Array(24/2)),i=await crypto.subtle.encrypt({name:`AES-GCM`,iv:r},e,t(n));return c(r)+o(new Uint8Array(i))}async function p(e,t){return i(await crypto.subtle.decrypt({name:`AES-GCM`,iv:s(t.slice(0,24))},e,a(t.slice(24))))}async function m(e,t){let i=crypto.getRandomValues(new Uint8Array(16)),a=await crypto.subtle.encrypt({name:`AES-GCM`,iv:i},e,t);return r(n([i,new Uint8Array(a)]))}async function h(t,n){let r=e(n);return crypto.subtle.decrypt({name:`AES-GCM`,iv:r.slice(0,16)},t,r.slice(16))}export{l as createKey,d as decodeKey,p as decrypt,h as decryptBuffer,u as encodeKey,f as encrypt,m as encryptBuffer};
1
+ import { base64StringToUint8Array } from "./convert/src/string-to-uint8-array.mjs";
2
+ import { stringToUtf8Array } from "./convert/src/string-to-utf8-array.mjs";
3
+ import { concatUint8Array } from "./convert/src/uint8-array-to-stream.mjs";
4
+ import { uint8ArrayToString } from "./convert/src/uint8-array-to-string.mjs";
5
+ import { utf8ArrayToString } from "./convert/src/utf8-array-to-string.mjs";
6
+ import "./convert/src/neutral.mjs";
7
+ import { decodeBase64, encodeBase64 } from "./base-64.mjs";
8
+ import { decodeHex, encodeHex } from "./hex.mjs";
9
+
10
+ //#region src/encryption.ts
11
+ /**
12
+ * Creates a CryptoKey object that can be used to encrypt any string.
13
+ *
14
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
15
+ *
16
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.
17
+ */
18
+ async function createKey() {
19
+ return crypto.subtle.generateKey({
20
+ name: "AES-GCM",
21
+ length: 256
22
+ }, true, ["encrypt", "decrypt"]);
23
+ }
24
+ /**
25
+ * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
26
+ *
27
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
28
+ *
29
+ * @param key - The CryptoKey to encode
30
+ * @returns A promise that resolves to a base64 string representing the key
31
+ */
32
+ async function encodeKey(key) {
33
+ const exported = await crypto.subtle.exportKey("raw", key);
34
+ return encodeBase64(new Uint8Array(exported)).toString();
35
+ }
36
+ /**
37
+ * Decodes a base64 string into bytes and then imports the key.
38
+ *
39
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
40
+ *
41
+ * @param encoded - The base64 encoded key
42
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings
43
+ */
44
+ async function decodeKey(encoded) {
45
+ const bytes = decodeBase64(encoded);
46
+ return crypto.subtle.importKey("raw", bytes.buffer, "AES-GCM", true, ["encrypt", "decrypt"]);
47
+ }
48
+ const IV_LENGTH = 24;
49
+ /**
50
+ * Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.
51
+ *
52
+ * @remarks
53
+ * The initialization vector is randomly generated and prepended to the encrypted string. The IV is required for decryption, so it must be stored alongside the encrypted data.
54
+ *
55
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
56
+ *
57
+ * @param key - The CryptoKey to use for encryption
58
+ * @param plaintext - The plaintext string to encrypt
59
+ * @returns A promise that resolves to a base64 string representing the encrypted data
60
+ */
61
+ async function encrypt(key, plaintext) {
62
+ const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));
63
+ const encrypted = await crypto.subtle.encrypt({
64
+ name: "AES-GCM",
65
+ iv
66
+ }, key, stringToUtf8Array(plaintext));
67
+ return encodeHex(iv) + encodeBase64(new Uint8Array(encrypted));
68
+ }
69
+ /**
70
+ * Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.
71
+ *
72
+ * @remarks
73
+ * The initialization vector is expected to be prepended to the encrypted string. The IV is required for decryption, so it must be extracted from the start of the string.
74
+ *
75
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
76
+ *
77
+ * @param key - The CryptoKey to use for decryption
78
+ * @param encrypted - The encrypted base64 encoded string to decrypt
79
+ * @returns A promise that resolves to the decrypted string
80
+ */
81
+ async function decrypt(key, encrypted) {
82
+ return utf8ArrayToString(await crypto.subtle.decrypt({
83
+ name: "AES-GCM",
84
+ iv: decodeHex(encrypted.slice(0, IV_LENGTH))
85
+ }, key, decodeBase64(encrypted.slice(IV_LENGTH))));
86
+ }
87
+ /**
88
+ * Encrypts a buffer using AES-GCM with a given CryptoKey.
89
+ *
90
+ * @remarks
91
+ * The initialization vector (IV) is randomly generated and prepended to the encrypted data. The resulting data is then encoded as a base64 string for easy storage/transmission.
92
+ *
93
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
94
+ *
95
+ * @param key - The CryptoKey to use for encryption
96
+ * @param buffer - The buffer to encrypt
97
+ * @returns A promise that resolves to a base64 string representing the encrypted data
98
+ */
99
+ async function encryptBuffer(key, buffer) {
100
+ const iv = crypto.getRandomValues(new Uint8Array(16));
101
+ const encrypted = await crypto.subtle.encrypt({
102
+ name: "AES-GCM",
103
+ iv
104
+ }, key, buffer);
105
+ return uint8ArrayToString(concatUint8Array([iv, new Uint8Array(encrypted)]));
106
+ }
107
+ /**
108
+ * Decrypts a buffer using AES-GCM with a given CryptoKey.
109
+ *
110
+ * @remarks
111
+ * The initialization vector (IV) is expected to be prepended to the encrypted data. The IV is required for decryption, so it must be extracted from the start of the buffer.
112
+ *
113
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
114
+ *
115
+ * @param key - The CryptoKey to use for decryption
116
+ * @param encrypted - The encrypted base64 encoded string to decrypt
117
+ * @returns A promise that resolves to the decrypted string
118
+ */
119
+ async function decryptBuffer(key, encrypted) {
120
+ const concatenated = base64StringToUint8Array(encrypted);
121
+ return crypto.subtle.decrypt({
122
+ name: "AES-GCM",
123
+ iv: concatenated.slice(0, 16)
124
+ }, key, concatenated.slice(16));
125
+ }
126
+
127
+ //#endregion
128
+ export { createKey, decodeKey, decrypt, decryptBuffer, encodeKey, encrypt, encryptBuffer };
2
129
  //# sourceMappingURL=encryption.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"encryption.mjs","names":[],"sources":["../src/encryption.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n base64StringToUint8Array,\n concatUint8Array,\n stringToUtf8Array,\n uint8ArrayToString,\n utf8ArrayToString\n} from \"@stryke/convert/neutral\";\nimport { decodeBase64, encodeBase64 } from \"./base-64\";\nimport { decodeHex, encodeHex } from \"./hex\";\n\n/**\n * Creates a CryptoKey object that can be used to encrypt any string.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey\n *\n * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.\n */\nexport async function createKey(): Promise<CryptoKey> {\n return crypto.subtle.generateKey(\n {\n name: \"AES-GCM\",\n length: 256\n },\n true,\n [\"encrypt\", \"decrypt\"]\n );\n}\n\n/**\n * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey\n *\n * @param key - The CryptoKey to encode\n * @returns A promise that resolves to a base64 string representing the key\n */\nexport async function encodeKey(key: CryptoKey): Promise<string> {\n const exported = await crypto.subtle.exportKey(\"raw\", key);\n\n return encodeBase64(new Uint8Array(exported)).toString();\n}\n\n/**\n * Decodes a base64 string into bytes and then imports the key.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey\n *\n * @param encoded - The base64 encoded key\n * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings\n */\nexport async function decodeKey(encoded: string): Promise<CryptoKey> {\n const bytes = decodeBase64(encoded);\n\n return crypto.subtle.importKey(\n \"raw\",\n bytes.buffer as ArrayBuffer,\n \"AES-GCM\",\n true,\n [\"encrypt\", \"decrypt\"]\n );\n}\n\n// The length of the initialization vector\n// See https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams\nconst IV_LENGTH = 24;\n\n/**\n * Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.\n *\n * @remarks\n * The initialization vector is randomly generated and prepended to the encrypted string. The IV is required for decryption, so it must be stored alongside the encrypted data.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt\n *\n * @param key - The CryptoKey to use for encryption\n * @param plaintext - The plaintext string to encrypt\n * @returns A promise that resolves to a base64 string representing the encrypted data\n */\nexport async function encrypt(\n key: CryptoKey,\n plaintext: string\n): Promise<string> {\n const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));\n const encrypted = await crypto.subtle.encrypt(\n {\n name: \"AES-GCM\",\n iv: iv as Uint8Array<ArrayBuffer>\n },\n key,\n stringToUtf8Array(plaintext) as BufferSource\n );\n\n // iv is 12, hex brings it to 24\n return encodeHex(iv) + encodeBase64(new Uint8Array(encrypted));\n}\n\n/**\n * Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.\n *\n * @remarks\n * The initialization vector is expected to be prepended to the encrypted string. The IV is required for decryption, so it must be extracted from the start of the string.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt\n *\n * @param key - The CryptoKey to use for decryption\n * @param encrypted - The encrypted base64 encoded string to decrypt\n * @returns A promise that resolves to the decrypted string\n */\nexport async function decrypt(\n key: CryptoKey,\n encrypted: string\n): Promise<string> {\n const decrypted = await crypto.subtle.decrypt(\n {\n name: \"AES-GCM\",\n iv: decodeHex(encrypted.slice(0, IV_LENGTH)) as Uint8Array<ArrayBuffer>\n },\n key,\n decodeBase64(encrypted.slice(IV_LENGTH)) as BufferSource\n );\n\n return utf8ArrayToString(decrypted);\n}\n\n/**\n * Encrypts a buffer using AES-GCM with a given CryptoKey.\n *\n * @remarks\n * The initialization vector (IV) is randomly generated and prepended to the encrypted data. The resulting data is then encoded as a base64 string for easy storage/transmission.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt\n *\n * @param key - The CryptoKey to use for encryption\n * @param buffer - The buffer to encrypt\n * @returns A promise that resolves to a base64 string representing the encrypted data\n */\nexport async function encryptBuffer(\n key: CryptoKey,\n buffer: BufferSource\n): Promise<string> {\n const iv = crypto.getRandomValues(new Uint8Array(16));\n const encrypted = await crypto.subtle.encrypt(\n {\n name: \"AES-GCM\",\n iv\n },\n key,\n buffer\n );\n\n return uint8ArrayToString(concatUint8Array([iv, new Uint8Array(encrypted)]));\n}\n\n/**\n * Decrypts a buffer using AES-GCM with a given CryptoKey.\n *\n * @remarks\n * The initialization vector (IV) is expected to be prepended to the encrypted data. The IV is required for decryption, so it must be extracted from the start of the buffer.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt\n *\n * @param key - The CryptoKey to use for decryption\n * @param encrypted - The encrypted base64 encoded string to decrypt\n * @returns A promise that resolves to the decrypted string\n */\nexport async function decryptBuffer(\n key: CryptoKey,\n encrypted: string\n): Promise<ArrayBuffer> {\n const concatenated = base64StringToUint8Array(encrypted);\n\n return crypto.subtle.decrypt(\n {\n name: \"AES-GCM\",\n iv: concatenated.slice(0, 16)\n },\n key,\n concatenated.slice(16)\n );\n}\n"],"mappings":"uhBAmCA,eAAsB,GAAgC,CACpD,OAAO,OAAO,OAAO,YACnB,CACE,KAAM,UACN,OAAQ,IACT,CACD,GACA,CAAC,UAAW,UAAU,CACvB,CAWH,eAAsB,EAAU,EAAiC,CAC/D,IAAM,EAAW,MAAM,OAAO,OAAO,UAAU,MAAO,EAAI,CAE1D,OAAO,EAAa,IAAI,WAAW,EAAS,CAAC,CAAC,UAAU,CAW1D,eAAsB,EAAU,EAAqC,CACnE,IAAM,EAAQ,EAAa,EAAQ,CAEnC,OAAO,OAAO,OAAO,UACnB,MACA,EAAM,OACN,UACA,GACA,CAAC,UAAW,UAAU,CACvB,CAmBH,eAAsB,EACpB,EACA,EACiB,CACjB,IAAM,EAAK,OAAO,gBAAgB,IAAI,WAAW,GAAY,EAAE,CAAC,CAC1D,EAAY,MAAM,OAAO,OAAO,QACpC,CACE,KAAM,UACF,KACL,CACD,EACA,EAAkB,EAAU,CAC7B,CAGD,OAAO,EAAU,EAAG,CAAG,EAAa,IAAI,WAAW,EAAU,CAAC,CAehE,eAAsB,EACpB,EACA,EACiB,CAUjB,OAAO,EATW,MAAM,OAAO,OAAO,QACpC,CACE,KAAM,UACN,GAAI,EAAU,EAAU,MAAM,EAAG,GAAU,CAAC,CAC7C,CACD,EACA,EAAa,EAAU,MAAM,GAAU,CAAC,CACzC,CAEkC,CAerC,eAAsB,EACpB,EACA,EACiB,CACjB,IAAM,EAAK,OAAO,gBAAgB,IAAI,WAAW,GAAG,CAAC,CAC/C,EAAY,MAAM,OAAO,OAAO,QACpC,CACE,KAAM,UACN,KACD,CACD,EACA,EACD,CAED,OAAO,EAAmB,EAAiB,CAAC,EAAI,IAAI,WAAW,EAAU,CAAC,CAAC,CAAC,CAe9E,eAAsB,EACpB,EACA,EACsB,CACtB,IAAM,EAAe,EAAyB,EAAU,CAExD,OAAO,OAAO,OAAO,QACnB,CACE,KAAM,UACN,GAAI,EAAa,MAAM,EAAG,GAAG,CAC9B,CACD,EACA,EAAa,MAAM,GAAG,CACvB"}
1
+ {"version":3,"file":"encryption.mjs","names":[],"sources":["../src/encryption.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n base64StringToUint8Array,\n concatUint8Array,\n stringToUtf8Array,\n uint8ArrayToString,\n utf8ArrayToString\n} from \"@stryke/convert/neutral\";\nimport { decodeBase64, encodeBase64 } from \"./base-64\";\nimport { decodeHex, encodeHex } from \"./hex\";\n\n/**\n * Creates a CryptoKey object that can be used to encrypt any string.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey\n *\n * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.\n */\nexport async function createKey(): Promise<CryptoKey> {\n return crypto.subtle.generateKey(\n {\n name: \"AES-GCM\",\n length: 256\n },\n true,\n [\"encrypt\", \"decrypt\"]\n );\n}\n\n/**\n * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey\n *\n * @param key - The CryptoKey to encode\n * @returns A promise that resolves to a base64 string representing the key\n */\nexport async function encodeKey(key: CryptoKey): Promise<string> {\n const exported = await crypto.subtle.exportKey(\"raw\", key);\n\n return encodeBase64(new Uint8Array(exported)).toString();\n}\n\n/**\n * Decodes a base64 string into bytes and then imports the key.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey\n *\n * @param encoded - The base64 encoded key\n * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings\n */\nexport async function decodeKey(encoded: string): Promise<CryptoKey> {\n const bytes = decodeBase64(encoded);\n\n return crypto.subtle.importKey(\n \"raw\",\n bytes.buffer as ArrayBuffer,\n \"AES-GCM\",\n true,\n [\"encrypt\", \"decrypt\"]\n );\n}\n\n// The length of the initialization vector\n// See https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams\nconst IV_LENGTH = 24;\n\n/**\n * Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.\n *\n * @remarks\n * The initialization vector is randomly generated and prepended to the encrypted string. The IV is required for decryption, so it must be stored alongside the encrypted data.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt\n *\n * @param key - The CryptoKey to use for encryption\n * @param plaintext - The plaintext string to encrypt\n * @returns A promise that resolves to a base64 string representing the encrypted data\n */\nexport async function encrypt(\n key: CryptoKey,\n plaintext: string\n): Promise<string> {\n const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH / 2));\n const encrypted = await crypto.subtle.encrypt(\n {\n name: \"AES-GCM\",\n iv: iv as Uint8Array<ArrayBuffer>\n },\n key,\n stringToUtf8Array(plaintext) as BufferSource\n );\n\n // iv is 12, hex brings it to 24\n return encodeHex(iv) + encodeBase64(new Uint8Array(encrypted));\n}\n\n/**\n * Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.\n *\n * @remarks\n * The initialization vector is expected to be prepended to the encrypted string. The IV is required for decryption, so it must be extracted from the start of the string.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt\n *\n * @param key - The CryptoKey to use for decryption\n * @param encrypted - The encrypted base64 encoded string to decrypt\n * @returns A promise that resolves to the decrypted string\n */\nexport async function decrypt(\n key: CryptoKey,\n encrypted: string\n): Promise<string> {\n const decrypted = await crypto.subtle.decrypt(\n {\n name: \"AES-GCM\",\n iv: decodeHex(encrypted.slice(0, IV_LENGTH)) as Uint8Array<ArrayBuffer>\n },\n key,\n decodeBase64(encrypted.slice(IV_LENGTH)) as BufferSource\n );\n\n return utf8ArrayToString(decrypted);\n}\n\n/**\n * Encrypts a buffer using AES-GCM with a given CryptoKey.\n *\n * @remarks\n * The initialization vector (IV) is randomly generated and prepended to the encrypted data. The resulting data is then encoded as a base64 string for easy storage/transmission.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt\n *\n * @param key - The CryptoKey to use for encryption\n * @param buffer - The buffer to encrypt\n * @returns A promise that resolves to a base64 string representing the encrypted data\n */\nexport async function encryptBuffer(\n key: CryptoKey,\n buffer: BufferSource\n): Promise<string> {\n const iv = crypto.getRandomValues(new Uint8Array(16));\n const encrypted = await crypto.subtle.encrypt(\n {\n name: \"AES-GCM\",\n iv\n },\n key,\n buffer\n );\n\n return uint8ArrayToString(concatUint8Array([iv, new Uint8Array(encrypted)]));\n}\n\n/**\n * Decrypts a buffer using AES-GCM with a given CryptoKey.\n *\n * @remarks\n * The initialization vector (IV) is expected to be prepended to the encrypted data. The IV is required for decryption, so it must be extracted from the start of the buffer.\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt\n *\n * @param key - The CryptoKey to use for decryption\n * @param encrypted - The encrypted base64 encoded string to decrypt\n * @returns A promise that resolves to the decrypted string\n */\nexport async function decryptBuffer(\n key: CryptoKey,\n encrypted: string\n): Promise<ArrayBuffer> {\n const concatenated = base64StringToUint8Array(encrypted);\n\n return crypto.subtle.decrypt(\n {\n name: \"AES-GCM\",\n iv: concatenated.slice(0, 16)\n },\n key,\n concatenated.slice(16)\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAmCA,eAAsB,YAAgC;AACpD,QAAO,OAAO,OAAO,YACnB;EACE,MAAM;EACN,QAAQ;EACT,EACD,MACA,CAAC,WAAW,UAAU,CACvB;;;;;;;;;;AAWH,eAAsB,UAAU,KAAiC;CAC/D,MAAM,WAAW,MAAM,OAAO,OAAO,UAAU,OAAO,IAAI;AAE1D,QAAO,aAAa,IAAI,WAAW,SAAS,CAAC,CAAC,UAAU;;;;;;;;;;AAW1D,eAAsB,UAAU,SAAqC;CACnE,MAAM,QAAQ,aAAa,QAAQ;AAEnC,QAAO,OAAO,OAAO,UACnB,OACA,MAAM,QACN,WACA,MACA,CAAC,WAAW,UAAU,CACvB;;AAKH,MAAM,YAAY;;;;;;;;;;;;;AAclB,eAAsB,QACpB,KACA,WACiB;CACjB,MAAM,KAAK,OAAO,gBAAgB,IAAI,WAAW,YAAY,EAAE,CAAC;CAChE,MAAM,YAAY,MAAM,OAAO,OAAO,QACpC;EACE,MAAM;EACF;EACL,EACD,KACA,kBAAkB,UAAU,CAC7B;AAGD,QAAO,UAAU,GAAG,GAAG,aAAa,IAAI,WAAW,UAAU,CAAC;;;;;;;;;;;;;;AAehE,eAAsB,QACpB,KACA,WACiB;AAUjB,QAAO,kBATW,MAAM,OAAO,OAAO,QACpC;EACE,MAAM;EACN,IAAI,UAAU,UAAU,MAAM,GAAG,UAAU,CAAC;EAC7C,EACD,KACA,aAAa,UAAU,MAAM,UAAU,CAAC,CACzC,CAEkC;;;;;;;;;;;;;;AAerC,eAAsB,cACpB,KACA,QACiB;CACjB,MAAM,KAAK,OAAO,gBAAgB,IAAI,WAAW,GAAG,CAAC;CACrD,MAAM,YAAY,MAAM,OAAO,OAAO,QACpC;EACE,MAAM;EACN;EACD,EACD,KACA,OACD;AAED,QAAO,mBAAmB,iBAAiB,CAAC,IAAI,IAAI,WAAW,UAAU,CAAC,CAAC,CAAC;;;;;;;;;;;;;;AAe9E,eAAsB,cACpB,KACA,WACsB;CACtB,MAAM,eAAe,yBAAyB,UAAU;AAExD,QAAO,OAAO,OAAO,QACnB;EACE,MAAM;EACN,IAAI,aAAa,MAAM,GAAG,GAAG;EAC9B,EACD,KACA,aAAa,MAAM,GAAG,CACvB"}
@@ -1 +1,63 @@
1
- let e=require(`node:buffer`),t=require(`node:crypto`);const n=`chacha20-poly1305`,r=32,i=16,a=16,o=64,s=1e5;function c(e,n){return typeof e==`string`?(0,t.createSecretKey)(e,n):(0,t.createSecretKey)(e)}function l(r,i){let a=(0,t.pbkdf2Sync)(r,(0,t.randomBytes)(64),s,32,`sha512`),o=(0,t.randomBytes)(16),c=(0,t.randomBytes)(64),l=(0,t.createCipheriv)(n,a,(0,t.randomBytes)(16)),u=e.Buffer.concat([l.update(i,`utf8`),l.final()]),d=l.getAuthTag();return e.Buffer.concat([c,o,d,u]).toString(`hex`)}function u(r,i){let a=e.Buffer.from(i,`hex`),o=(0,t.createDecipheriv)(n,(0,t.pbkdf2Sync)(r,a.slice(0,64),s,32,`sha512`),a.slice(64,80));return o.setAuthTag(a.slice(80,96)),o.update(a.slice(96))+o.final(`utf8`)}exports.createKey=c,exports.decrypt=u,exports.encrypt=l;
1
+ let node_buffer = require("node:buffer");
2
+ let node_crypto = require("node:crypto");
3
+
4
+ //#region src/encryption.node.ts
5
+ const CIPHER_ALGORITHM = "chacha20-poly1305";
6
+ const CIPHER_KEY_LENGTH = 32;
7
+ const CIPHER_IV_LENGTH = 16;
8
+ const CIPHER_TAG_LENGTH = 16;
9
+ const CIPHER_SALT_LENGTH = 64;
10
+ const PBKDF2_ITERATIONS = 1e5;
11
+ /**
12
+ * Creates and returns a new key object containing a secret key for symmetric encryption or \`Hmac\`.
13
+ *
14
+ * @param key - The key to use. If \`key\` is a \`Buffer\`, \`TypedArray\`, or \`DataView\`, the \`encoding\` argument is ignored.
15
+ * @param encoding - The \`encoding\` of the \`key\` string. Must be one of \`'utf8'\`, \`'utf16le'\`, \`'latin1'\`, or \`'base64'\`. Default is \`'utf8'\`.
16
+ * @returns The new \`KeyObject\`.
17
+ */
18
+ function createKey(key, encoding) {
19
+ return typeof key === "string" ? (0, node_crypto.createSecretKey)(key, encoding) : (0, node_crypto.createSecretKey)(key);
20
+ }
21
+ /**
22
+ * Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
23
+ *
24
+ * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
25
+ *
26
+ * @param secret - The secret key used for encryption.
27
+ * @param plaintext - The data to encrypt.
28
+ * @returns The encrypted data.
29
+ */
30
+ function encrypt(secret, plaintext) {
31
+ const key = (0, node_crypto.pbkdf2Sync)(secret, (0, node_crypto.randomBytes)(CIPHER_SALT_LENGTH), PBKDF2_ITERATIONS, CIPHER_KEY_LENGTH, "sha512");
32
+ const iv = (0, node_crypto.randomBytes)(CIPHER_IV_LENGTH);
33
+ const salt = (0, node_crypto.randomBytes)(CIPHER_SALT_LENGTH);
34
+ const cipher = (0, node_crypto.createCipheriv)(CIPHER_ALGORITHM, key, (0, node_crypto.randomBytes)(CIPHER_IV_LENGTH));
35
+ const encrypted = node_buffer.Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
36
+ const tag = cipher.getAuthTag();
37
+ return node_buffer.Buffer.concat([
38
+ salt,
39
+ iv,
40
+ tag,
41
+ encrypted
42
+ ]).toString("hex");
43
+ }
44
+ /**
45
+ * Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
46
+ *
47
+ * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
48
+ *
49
+ * @param secret - The secret key used for decryption.
50
+ * @param encrypted - The encrypted data to decrypt.
51
+ * @returns The decrypted data.
52
+ */
53
+ function decrypt(secret, encrypted) {
54
+ const buffer = node_buffer.Buffer.from(encrypted, "hex");
55
+ const decipher = (0, node_crypto.createDecipheriv)(CIPHER_ALGORITHM, (0, node_crypto.pbkdf2Sync)(secret, buffer.slice(0, CIPHER_SALT_LENGTH), PBKDF2_ITERATIONS, CIPHER_KEY_LENGTH, "sha512"), buffer.slice(CIPHER_SALT_LENGTH, CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH));
56
+ decipher.setAuthTag(buffer.slice(CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH, CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH + CIPHER_TAG_LENGTH));
57
+ return decipher.update(buffer.slice(CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH + CIPHER_TAG_LENGTH)) + decipher.final("utf8");
58
+ }
59
+
60
+ //#endregion
61
+ exports.createKey = createKey;
62
+ exports.decrypt = decrypt;
63
+ exports.encrypt = encrypt;
@@ -1,2 +1,62 @@
1
- import{Buffer as e}from"node:buffer";import{createCipheriv as t,createDecipheriv as n,createSecretKey as r,pbkdf2Sync as i,randomBytes as a}from"node:crypto";const o=`chacha20-poly1305`,s=1e5;function c(e,t){return typeof e==`string`?r(e,t):r(e)}function l(n,r){let c=i(n,a(64),s,32,`sha512`),l=a(16),u=a(64),d=t(o,c,a(16)),f=e.concat([d.update(r,`utf8`),d.final()]),p=d.getAuthTag();return e.concat([u,l,p,f]).toString(`hex`)}function u(t,r){let a=e.from(r,`hex`),c=n(o,i(t,a.slice(0,64),s,32,`sha512`),a.slice(64,80));return c.setAuthTag(a.slice(80,96)),c.update(a.slice(96))+c.final(`utf8`)}export{c as createKey,u as decrypt,l as encrypt};
1
+ import { Buffer } from "node:buffer";
2
+ import { createCipheriv, createDecipheriv, createSecretKey, pbkdf2Sync, randomBytes } from "node:crypto";
3
+
4
+ //#region src/encryption.node.ts
5
+ const CIPHER_ALGORITHM = "chacha20-poly1305";
6
+ const CIPHER_KEY_LENGTH = 32;
7
+ const CIPHER_IV_LENGTH = 16;
8
+ const CIPHER_TAG_LENGTH = 16;
9
+ const CIPHER_SALT_LENGTH = 64;
10
+ const PBKDF2_ITERATIONS = 1e5;
11
+ /**
12
+ * Creates and returns a new key object containing a secret key for symmetric encryption or \`Hmac\`.
13
+ *
14
+ * @param key - The key to use. If \`key\` is a \`Buffer\`, \`TypedArray\`, or \`DataView\`, the \`encoding\` argument is ignored.
15
+ * @param encoding - The \`encoding\` of the \`key\` string. Must be one of \`'utf8'\`, \`'utf16le'\`, \`'latin1'\`, or \`'base64'\`. Default is \`'utf8'\`.
16
+ * @returns The new \`KeyObject\`.
17
+ */
18
+ function createKey(key, encoding) {
19
+ return typeof key === "string" ? createSecretKey(key, encoding) : createSecretKey(key);
20
+ }
21
+ /**
22
+ * Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
23
+ *
24
+ * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
25
+ *
26
+ * @param secret - The secret key used for encryption.
27
+ * @param plaintext - The data to encrypt.
28
+ * @returns The encrypted data.
29
+ */
30
+ function encrypt(secret, plaintext) {
31
+ const key = pbkdf2Sync(secret, randomBytes(CIPHER_SALT_LENGTH), PBKDF2_ITERATIONS, CIPHER_KEY_LENGTH, "sha512");
32
+ const iv = randomBytes(CIPHER_IV_LENGTH);
33
+ const salt = randomBytes(CIPHER_SALT_LENGTH);
34
+ const cipher = createCipheriv(CIPHER_ALGORITHM, key, randomBytes(CIPHER_IV_LENGTH));
35
+ const encrypted = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);
36
+ const tag = cipher.getAuthTag();
37
+ return Buffer.concat([
38
+ salt,
39
+ iv,
40
+ tag,
41
+ encrypted
42
+ ]).toString("hex");
43
+ }
44
+ /**
45
+ * Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
46
+ *
47
+ * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
48
+ *
49
+ * @param secret - The secret key used for decryption.
50
+ * @param encrypted - The encrypted data to decrypt.
51
+ * @returns The decrypted data.
52
+ */
53
+ function decrypt(secret, encrypted) {
54
+ const buffer = Buffer.from(encrypted, "hex");
55
+ const decipher = createDecipheriv(CIPHER_ALGORITHM, pbkdf2Sync(secret, buffer.slice(0, CIPHER_SALT_LENGTH), PBKDF2_ITERATIONS, CIPHER_KEY_LENGTH, "sha512"), buffer.slice(CIPHER_SALT_LENGTH, CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH));
56
+ decipher.setAuthTag(buffer.slice(CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH, CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH + CIPHER_TAG_LENGTH));
57
+ return decipher.update(buffer.slice(CIPHER_SALT_LENGTH + CIPHER_IV_LENGTH + CIPHER_TAG_LENGTH)) + decipher.final("utf8");
58
+ }
59
+
60
+ //#endregion
61
+ export { createKey, decrypt, encrypt };
2
62
  //# sourceMappingURL=encryption.node.mjs.map