@stryke/crypto 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/base-64.cjs CHANGED
@@ -5,70 +5,75 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.base64FromBase64url = base64FromBase64url;
7
7
  exports.base64ToBase64url = base64ToBase64url;
8
- exports.fromBase64 = fromBase64;
9
- exports.toBase64 = toBase64;
8
+ exports.decodeBase64 = decodeBase64;
9
+ exports.encodeBase64 = encodeBase64;
10
+ var _stringToUtf8Array = require("@stryke/convert/string-to-utf8-array");
11
+ var _utf8ArrayToString = require("@stryke/convert/utf8-array-to-string");
12
+ var _isString = require("@stryke/type-checks/is-string");
10
13
  var _isUndefined = require("@stryke/type-checks/is-undefined");
11
14
  const a = new Uint8Array([65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47]),
12
15
  h = 61,
13
16
  s = new Uint8Array([100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 62, 100, 100, 100, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 100, 100, 100, 100, 100, 100, 100, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 100, 100, 100, 100, 100, 100, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]);
14
- function toBase64(e) {
15
- const f = e.length,
17
+ function encodeBase64(r) {
18
+ (0, _isString.isString)(r) && (r = (0, _stringToUtf8Array.stringToUtf8Array)(r));
19
+ const f = r.length,
16
20
  n = new Uint8Array(Math.ceil(f / 3) * 4),
17
21
  l = Math.floor(f / 3);
18
- for (let r = 0; r < l; r++) {
19
- const c = e[3 * r + 0] << 16 | e[3 * r + 1] << 8 | e[3 * r + 2];
20
- n[4 * r + 0] = a[c >> 18 & 63], n[4 * r + 1] = a[c >> 12 & 63], n[4 * r + 2] = a[c >> 6 & 63], n[4 * r + 3] = a[c >> 0 & 63];
22
+ for (let e = 0; e < l; e++) {
23
+ const c = r[3 * e + 0] << 16 | r[3 * e + 1] << 8 | r[3 * e + 2];
24
+ n[4 * e + 0] = a[c >> 18 & 63], n[4 * e + 1] = a[c >> 12 & 63], n[4 * e + 2] = a[c >> 6 & 63], n[4 * e + 3] = a[c >> 0 & 63];
21
25
  }
22
- const o = l;
26
+ const t = l;
23
27
  switch (f % 3) {
24
28
  case 1:
25
29
  {
26
- const r = e[3 * o + 0] << 16;
27
- n[4 * o + 0] = a[r >> 18 & 63], n[4 * o + 1] = a[r >> 12 & 63], n[4 * o + 2] = h, n[4 * o + 3] = h;
30
+ const e = r[3 * t + 0] << 16;
31
+ n[4 * t + 0] = a[e >> 18 & 63], n[4 * t + 1] = a[e >> 12 & 63], n[4 * t + 2] = h, n[4 * t + 3] = h;
28
32
  break;
29
33
  }
30
34
  case 2:
31
35
  {
32
- const r = e[3 * o + 0] << 16 | e[3 * o + 1] << 8;
33
- n[4 * o + 0] = a[r >> 18 & 63], n[4 * o + 1] = a[r >> 12 & 63], n[4 * o + 2] = a[r >> 6 & 63], n[4 * o + 3] = h;
36
+ const e = r[3 * t + 0] << 16 | r[3 * t + 1] << 8;
37
+ n[4 * t + 0] = a[e >> 18 & 63], n[4 * t + 1] = a[e >> 12 & 63], n[4 * t + 2] = a[e >> 6 & 63], n[4 * t + 3] = h;
34
38
  break;
35
39
  }
36
40
  }
37
- return n;
41
+ return (0, _utf8ArrayToString.utf8ArrayToString)(n);
38
42
  }
39
- function fromBase64(e) {
40
- const f = e.length;
43
+ function decodeBase64(r) {
44
+ (0, _isString.isString)(r) && (r = (0, _stringToUtf8Array.stringToUtf8Array)(r));
45
+ const f = r.length;
41
46
  if (f % 4 !== 0) throw new Error("invalid length");
42
47
  let n = 0;
43
- for (; n < 2 && n < e.length && e[e.length - 1 - n] === h;) n++;
44
- for (let t = 0; t < e.length - n; t++) if ((0, _isUndefined.isUndefined)(e[t]) || (0, _isUndefined.isUndefined)(s[e[t]]) || s[e[t]] >= 64) throw new Error("invalid data");
48
+ for (; n < 2 && n < r.length && r[r.length - 1 - n] === h;) n++;
49
+ for (let o = 0; o < r.length - n; o++) if ((0, _isUndefined.isUndefined)(r[o]) || (0, _isUndefined.isUndefined)(s[r[o]]) || s[r[o]] >= 64) throw new Error("invalid data");
45
50
  const l = Math.floor((f - n) / 4),
46
- o = 3 * l + (3 - n) % 3,
47
- r = new Uint8Array(o);
48
- for (let t = 0; t < l; t++) {
49
- const g = s[e[4 * t + 0]] << 18 | s[e[4 * t + 1]] << 12 | s[e[4 * t + 2]] << 6 | s[e[4 * t + 3]] << 0;
50
- r[3 * t] = g >> 16 & 255, r[3 * t + 1] = g >> 8 & 255, r[3 * t + 2] = g >> 0 & 255;
51
+ t = 3 * l + (3 - n) % 3,
52
+ e = new Uint8Array(t);
53
+ for (let o = 0; o < l; o++) {
54
+ const g = s[r[4 * o + 0]] << 18 | s[r[4 * o + 1]] << 12 | s[r[4 * o + 2]] << 6 | s[r[4 * o + 3]] << 0;
55
+ e[3 * o] = g >> 16 & 255, e[3 * o + 1] = g >> 8 & 255, e[3 * o + 2] = g >> 0 & 255;
51
56
  }
52
57
  const c = l;
53
- switch (o % 3) {
58
+ switch (t % 3) {
54
59
  case 1:
55
60
  {
56
- const t = s[e[4 * c + 0]] << 18 | s[e[4 * c + 1]] << 12;
57
- r[3 * c] = t >> 16 & 255;
61
+ const o = s[r[4 * c + 0]] << 18 | s[r[4 * c + 1]] << 12;
62
+ e[3 * c] = o >> 16 & 255;
58
63
  break;
59
64
  }
60
65
  case 2:
61
66
  {
62
- const t = s[e[4 * c + 0]] << 18 | s[e[4 * c + 1]] << 12 | s[e[4 * c + 2]] << 6;
63
- r[3 * c] = t >> 16 & 255, r[3 * c + 1] = t >> 8 & 255;
67
+ const o = s[r[4 * c + 0]] << 18 | s[r[4 * c + 1]] << 12 | s[r[4 * c + 2]] << 6;
68
+ e[3 * c] = o >> 16 & 255, e[3 * c + 1] = o >> 8 & 255;
64
69
  break;
65
70
  }
66
71
  }
67
- return r;
72
+ return e;
68
73
  }
69
- function base64ToBase64url(e) {
70
- return e.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
74
+ function base64ToBase64url(r) {
75
+ return r.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
71
76
  }
72
- function base64FromBase64url(e) {
73
- return e.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - e.length % 4) % 4);
77
+ function base64FromBase64url(r) {
78
+ return r.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - r.length % 4) % 4);
74
79
  }
package/dist/base-64.d.ts CHANGED
@@ -1,17 +1,21 @@
1
1
  /**
2
2
  * Encodes a Uint8Array into a Base64 encoded Uint8Array.
3
3
  *
4
- * @param input - The input Uint8Array to encode.
4
+ * @credits https://github.com/hi-ogawa/js-utils
5
+ *
6
+ * @param input - The input Uint8Array or string to encode.
5
7
  * @returns The Base64 encoded Uint8Array.
6
8
  */
7
- export declare function toBase64(input: Uint8Array): Uint8Array;
9
+ export declare function encodeBase64(input: Uint8Array | string): string;
8
10
  /**
9
11
  * Decodes a Base64 encoded Uint8Array into a Uint8Array.
10
12
  *
11
- * @param input - The Base64 encoded Uint8Array to decode.
13
+ * @credits https://github.com/hi-ogawa/js-utils
14
+ *
15
+ * @param input - The Base64 encoded Uint8Array or string to decode.
12
16
  * @returns The decoded Uint8Array.
13
17
  */
14
- export declare function fromBase64(input: Uint8Array): Uint8Array;
18
+ export declare function decodeBase64(input: Uint8Array | string): Uint8Array;
15
19
  /**
16
20
  * Converts a Base64 encoded string to a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string.
17
21
  *
@@ -24,7 +28,7 @@ export declare function base64ToBase64url(base64: string): string;
24
28
  /**
25
29
  * Converts a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string to a Base64 encoded string.
26
30
  *
27
- * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C *
31
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
28
32
  *
29
33
  * @param base64url - The Base64url encoded string to convert.
30
34
  * @returns The Base64 encoded string.
package/dist/base-64.mjs CHANGED
@@ -1 +1 @@
1
- import{isUndefined as b}from"@stryke/type-checks/is-undefined";const a=new Uint8Array([65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47]),h=61,s=new Uint8Array([100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,62,100,100,100,63,52,53,54,55,56,57,58,59,60,61,100,100,100,100,100,100,100,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,100,100,100,100,100,100,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100]);export function toBase64(e){const f=e.length,n=new Uint8Array(Math.ceil(f/3)*4),l=Math.floor(f/3);for(let r=0;r<l;r++){const c=e[3*r+0]<<16|e[3*r+1]<<8|e[3*r+2];n[4*r+0]=a[c>>18&63],n[4*r+1]=a[c>>12&63],n[4*r+2]=a[c>>6&63],n[4*r+3]=a[c>>0&63]}const o=l;switch(f%3){case 1:{const r=e[3*o+0]<<16;n[4*o+0]=a[r>>18&63],n[4*o+1]=a[r>>12&63],n[4*o+2]=h,n[4*o+3]=h;break}case 2:{const r=e[3*o+0]<<16|e[3*o+1]<<8;n[4*o+0]=a[r>>18&63],n[4*o+1]=a[r>>12&63],n[4*o+2]=a[r>>6&63],n[4*o+3]=h;break}}return n}export function fromBase64(e){const f=e.length;if(f%4!==0)throw new Error("invalid length");let n=0;for(;n<2&&n<e.length&&e[e.length-1-n]===h;)n++;for(let t=0;t<e.length-n;t++)if(b(e[t])||b(s[e[t]])||s[e[t]]>=64)throw new Error("invalid data");const l=Math.floor((f-n)/4),o=3*l+(3-n)%3,r=new Uint8Array(o);for(let t=0;t<l;t++){const g=s[e[4*t+0]]<<18|s[e[4*t+1]]<<12|s[e[4*t+2]]<<6|s[e[4*t+3]]<<0;r[3*t]=g>>16&255,r[3*t+1]=g>>8&255,r[3*t+2]=g>>0&255}const c=l;switch(o%3){case 1:{const t=s[e[4*c+0]]<<18|s[e[4*c+1]]<<12;r[3*c]=t>>16&255;break}case 2:{const t=s[e[4*c+0]]<<18|s[e[4*c+1]]<<12|s[e[4*c+2]]<<6;r[3*c]=t>>16&255,r[3*c+1]=t>>8&255;break}}return r}export function base64ToBase64url(e){return e.replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}export function base64FromBase64url(e){return e.replace(/-/g,"+").replace(/_/g,"/")+"=".repeat((4-e.length%4)%4)}
1
+ import{stringToUtf8Array as i}from"@stryke/convert/string-to-utf8-array";import{utf8ArrayToString as x}from"@stryke/convert/utf8-array-to-string";import{isString as b}from"@stryke/type-checks/is-string";import{isUndefined as k}from"@stryke/type-checks/is-undefined";const a=new Uint8Array([65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47]),h=61,s=new Uint8Array([100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,62,100,100,100,63,52,53,54,55,56,57,58,59,60,61,100,100,100,100,100,100,100,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,100,100,100,100,100,100,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100]);export function encodeBase64(r){b(r)&&(r=i(r));const f=r.length,n=new Uint8Array(Math.ceil(f/3)*4),l=Math.floor(f/3);for(let e=0;e<l;e++){const c=r[3*e+0]<<16|r[3*e+1]<<8|r[3*e+2];n[4*e+0]=a[c>>18&63],n[4*e+1]=a[c>>12&63],n[4*e+2]=a[c>>6&63],n[4*e+3]=a[c>>0&63]}const t=l;switch(f%3){case 1:{const e=r[3*t+0]<<16;n[4*t+0]=a[e>>18&63],n[4*t+1]=a[e>>12&63],n[4*t+2]=h,n[4*t+3]=h;break}case 2:{const e=r[3*t+0]<<16|r[3*t+1]<<8;n[4*t+0]=a[e>>18&63],n[4*t+1]=a[e>>12&63],n[4*t+2]=a[e>>6&63],n[4*t+3]=h;break}}return x(n)}export function decodeBase64(r){b(r)&&(r=i(r));const f=r.length;if(f%4!==0)throw new Error("invalid length");let n=0;for(;n<2&&n<r.length&&r[r.length-1-n]===h;)n++;for(let o=0;o<r.length-n;o++)if(k(r[o])||k(s[r[o]])||s[r[o]]>=64)throw new Error("invalid data");const l=Math.floor((f-n)/4),t=3*l+(3-n)%3,e=new Uint8Array(t);for(let o=0;o<l;o++){const g=s[r[4*o+0]]<<18|s[r[4*o+1]]<<12|s[r[4*o+2]]<<6|s[r[4*o+3]]<<0;e[3*o]=g>>16&255,e[3*o+1]=g>>8&255,e[3*o+2]=g>>0&255}const c=l;switch(t%3){case 1:{const o=s[r[4*c+0]]<<18|s[r[4*c+1]]<<12;e[3*c]=o>>16&255;break}case 2:{const o=s[r[4*c+0]]<<18|s[r[4*c+1]]<<12|s[r[4*c+2]]<<6;e[3*c]=o>>16&255,e[3*c+1]=o>>8&255;break}}return e}export function base64ToBase64url(r){return r.replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}export function base64FromBase64url(r){return r.replace(/-/g,"+").replace(/_/g,"/")+"=".repeat((4-r.length%4)%4)}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createKey = createKey;
7
+ exports.decodeKey = decodeKey;
8
+ exports.decrypt = decrypt;
9
+ exports.decryptBuffer = decryptBuffer;
10
+ exports.encodeKey = encodeKey;
11
+ exports.encrypt = encrypt;
12
+ exports.encryptBuffer = encryptBuffer;
13
+ var _stringToUint8Array = require("@stryke/convert/string-to-uint8-array");
14
+ var _stringToUtf8Array = require("@stryke/convert/string-to-utf8-array");
15
+ var _uint8ArrayToStream = require("@stryke/convert/uint8-array-to-stream");
16
+ var _uint8ArrayToString = require("@stryke/convert/uint8-array-to-string");
17
+ var _utf8ArrayToString = require("@stryke/convert/utf8-array-to-string");
18
+ var _base = require("./base-64.cjs");
19
+ var _hex = require("./hex.cjs");
20
+ async function createKey() {
21
+ return crypto.subtle.generateKey({
22
+ name: "AES-GCM",
23
+ length: 256
24
+ }, !0, ["encrypt", "decrypt"]);
25
+ }
26
+ async function encodeKey(t) {
27
+ const r = await crypto.subtle.exportKey("raw", t);
28
+ return (0, _base.encodeBase64)(new Uint8Array(r)).toString();
29
+ }
30
+ async function decodeKey(t) {
31
+ const r = (0, _base.decodeBase64)(t);
32
+ return crypto.subtle.importKey("raw", r.buffer, "AES-GCM", !0, ["encrypt", "decrypt"]);
33
+ }
34
+ const o = 24;
35
+ async function encrypt(t, r) {
36
+ const e = crypto.getRandomValues(new Uint8Array(o / 2)),
37
+ n = await crypto.subtle.encrypt({
38
+ name: "AES-GCM",
39
+ iv: e
40
+ }, t, (0, _stringToUtf8Array.stringToUtf8Array)(r));
41
+ return (0, _hex.encodeHex)(e) + (0, _base.encodeBase64)(new Uint8Array(n));
42
+ }
43
+ async function decrypt(t, r) {
44
+ const e = await crypto.subtle.decrypt({
45
+ name: "AES-GCM",
46
+ iv: (0, _hex.decodeHex)(r.slice(0, o))
47
+ }, t, (0, _base.decodeBase64)(r.slice(o)));
48
+ return (0, _utf8ArrayToString.utf8ArrayToString)(e);
49
+ }
50
+ async function encryptBuffer(t, r) {
51
+ const e = crypto.getRandomValues(new Uint8Array(16)),
52
+ n = await crypto.subtle.encrypt({
53
+ name: "AES-GCM",
54
+ iv: e
55
+ }, t, r);
56
+ return (0, _uint8ArrayToString.base64Uint8ArrayToString)((0, _uint8ArrayToStream.concatUint8Array)([e, new Uint8Array(n)]));
57
+ }
58
+ async function decryptBuffer(t, r) {
59
+ const e = (0, _stringToUint8Array.base64StringToUint8Array)(r);
60
+ return crypto.subtle.decrypt({
61
+ name: "AES-GCM",
62
+ iv: e.slice(0, 16)
63
+ }, t, e.slice(16));
64
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Creates a CryptoKey object that can be used to encrypt any string.
3
+ *
4
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
5
+ *
6
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings.
7
+ */
8
+ export declare function createKey(): Promise<CryptoKey>;
9
+ /**
10
+ * Encodes a CryptoKey to base64 string, so that it can be embedded in JSON / JavaScript
11
+ *
12
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey
13
+ *
14
+ * @param key - The CryptoKey to encode
15
+ * @returns A promise that resolves to a base64 string representing the key
16
+ */
17
+ export declare function encodeKey(key: CryptoKey): Promise<string>;
18
+ /**
19
+ * Decodes a base64 string into bytes and then imports the key.
20
+ *
21
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
22
+ *
23
+ * @param encoded - The base64 encoded key
24
+ * @returns A promise that resolves to a CryptoKey object that can be used to encrypt and decrypt strings
25
+ */
26
+ export declare function decodeKey(encoded: string): Promise<CryptoKey>;
27
+ /**
28
+ * Using a CryptoKey, use AES-GCM to encrypt a string into a base64 string.
29
+ *
30
+ * @remarks
31
+ * 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.
32
+ *
33
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
34
+ *
35
+ * @param key - The CryptoKey to use for encryption
36
+ * @param plaintext - The plaintext string to encrypt
37
+ * @returns A promise that resolves to a base64 string representing the encrypted data
38
+ */
39
+ export declare function encrypt(key: CryptoKey, plaintext: string): Promise<string>;
40
+ /**
41
+ * Takes a base64 encoded string, decodes it and returns the AES-GCM decrypted text.
42
+ *
43
+ * @remarks
44
+ * 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.
45
+ *
46
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
47
+ *
48
+ * @param key - The CryptoKey to use for decryption
49
+ * @param encrypted - The encrypted base64 encoded string to decrypt
50
+ * @returns A promise that resolves to the decrypted string
51
+ */
52
+ export declare function decrypt(key: CryptoKey, encrypted: string): Promise<string>;
53
+ /**
54
+ * Encrypts a buffer using AES-GCM with a given CryptoKey.
55
+ *
56
+ * @remarks
57
+ * 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.
58
+ *
59
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt
60
+ *
61
+ * @param key - The CryptoKey to use for encryption
62
+ * @param buffer - The buffer to encrypt
63
+ * @returns A promise that resolves to a base64 string representing the encrypted data
64
+ */
65
+ export declare function encryptBuffer(key: CryptoKey, buffer: BufferSource): Promise<string>;
66
+ /**
67
+ * Decrypts a buffer using AES-GCM with a given CryptoKey.
68
+ *
69
+ * @remarks
70
+ * 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.
71
+ *
72
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt
73
+ *
74
+ * @param key - The CryptoKey to use for decryption
75
+ * @param encrypted - The encrypted base64 encoded string to decrypt
76
+ * @returns A promise that resolves to the decrypted string
77
+ */
78
+ export declare function decryptBuffer(key: CryptoKey, encrypted: string): Promise<ArrayBuffer>;
@@ -0,0 +1 @@
1
+ import{base64StringToUint8Array as i}from"@stryke/convert/string-to-uint8-array";import{stringToUtf8Array as s}from"@stryke/convert/string-to-utf8-array";import{concatUint8Array as a}from"@stryke/convert/uint8-array-to-stream";import{base64Uint8ArrayToString as p}from"@stryke/convert/uint8-array-to-string";import{utf8ArrayToString as u}from"@stryke/convert/utf8-array-to-string";import{decodeBase64 as y,encodeBase64 as c}from"./base-64";import{decodeHex as m,encodeHex as f}from"./hex";export async function createKey(){return crypto.subtle.generateKey({name:"AES-GCM",length:256},!0,["encrypt","decrypt"])}export async function encodeKey(t){const r=await crypto.subtle.exportKey("raw",t);return c(new Uint8Array(r)).toString()}export async function decodeKey(t){const r=y(t);return crypto.subtle.importKey("raw",r.buffer,"AES-GCM",!0,["encrypt","decrypt"])}const o=24;export async function encrypt(t,r){const e=crypto.getRandomValues(new Uint8Array(o/2)),n=await crypto.subtle.encrypt({name:"AES-GCM",iv:e},t,s(r));return f(e)+c(new Uint8Array(n))}export async function decrypt(t,r){const e=await crypto.subtle.decrypt({name:"AES-GCM",iv:m(r.slice(0,o))},t,y(r.slice(o)));return u(e)}export async function encryptBuffer(t,r){const e=crypto.getRandomValues(new Uint8Array(16)),n=await crypto.subtle.encrypt({name:"AES-GCM",iv:e},t,r);return p(a([e,new Uint8Array(n)]))}export async function decryptBuffer(t,r){const e=i(r);return crypto.subtle.decrypt({name:"AES-GCM",iv:e.slice(0,16)},t,e.slice(16))}
package/dist/hex.cjs ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.decodeHex = decodeHex;
7
+ exports.encodeHex = encodeHex;
8
+ const o = "0123456789ABCDEF",
9
+ t = {
10
+ 0: 0,
11
+ 1: 1,
12
+ 2: 2,
13
+ 3: 3,
14
+ 4: 4,
15
+ 5: 5,
16
+ 6: 6,
17
+ 7: 7,
18
+ 8: 8,
19
+ 9: 9,
20
+ a: 10,
21
+ A: 10,
22
+ b: 11,
23
+ B: 11,
24
+ c: 12,
25
+ C: 12,
26
+ d: 13,
27
+ D: 13,
28
+ e: 14,
29
+ E: 14,
30
+ f: 15,
31
+ F: 15
32
+ };
33
+ function encodeHex(e) {
34
+ let n = "";
35
+ for (let r = 0; r < e.length; r++) n += o[e[r] >> 4], n += o[e[r] & 15];
36
+ return n;
37
+ }
38
+ function decodeHex(e) {
39
+ if (e.length % 2 !== 0) throw new Error("Invalid hex string");
40
+ const n = new Uint8Array(e.length / 2);
41
+ for (let r = 0; r < e.length; r += 2) {
42
+ if (!(e[r] in t)) throw new Error("Invalid character");
43
+ if (!(e[r + 1] in t)) throw new Error("Invalid character");
44
+ n[r / 2] |= t[e[r]] << 4, n[r / 2] |= t[e[r + 1]];
45
+ }
46
+ return n;
47
+ }
package/dist/hex.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Encodes a Uint8Array into a hexadecimal string.
3
+ *
4
+ * @param input - The input Uint8Array.
5
+ * @returns The hexadecimal string.
6
+ */
7
+ export declare function encodeHex(input: Uint8Array): string;
8
+ /**
9
+ * Encodes a Uint8Array into an uppercase hexadecimal string.
10
+ *
11
+ * @param input - The input Uint8Array.
12
+ * @returns The uppercase hexadecimal string.
13
+ */
14
+ export declare function decodeHex(input: string): Uint8Array;
package/dist/hex.mjs ADDED
@@ -0,0 +1 @@
1
+ const o="0123456789ABCDEF",t={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,A:10,b:11,B:11,c:12,C:12,d:13,D:13,e:14,E:14,f:15,F:15};export function encodeHex(e){let n="";for(let r=0;r<e.length;r++)n+=o[e[r]>>4],n+=o[e[r]&15];return n}export function decodeHex(e){if(e.length%2!==0)throw new Error("Invalid hex string");const n=new Uint8Array(e.length/2);for(let r=0;r<e.length;r+=2){if(!(e[r]in t))throw new Error("Invalid character");if(!(e[r+1]in t))throw new Error("Invalid character");n[r/2]|=t[e[r]]<<4,n[r/2]|=t[e[r+1]]}return n}
package/dist/index.cjs CHANGED
@@ -3,14 +3,36 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _sym = require("./sym.cjs");
7
- Object.keys(_sym).forEach(function (key) {
6
+ var _base = require("./base-64.cjs");
7
+ Object.keys(_base).forEach(function (key) {
8
8
  if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _sym[key]) return;
9
+ if (key in exports && exports[key] === _base[key]) return;
10
10
  Object.defineProperty(exports, key, {
11
11
  enumerable: true,
12
12
  get: function () {
13
- return _sym[key];
13
+ return _base[key];
14
+ }
15
+ });
16
+ });
17
+ var _encryption = require("./encryption.cjs");
18
+ Object.keys(_encryption).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (key in exports && exports[key] === _encryption[key]) return;
21
+ Object.defineProperty(exports, key, {
22
+ enumerable: true,
23
+ get: function () {
24
+ return _encryption[key];
25
+ }
26
+ });
27
+ });
28
+ var _hex = require("./hex.cjs");
29
+ Object.keys(_hex).forEach(function (key) {
30
+ if (key === "default" || key === "__esModule") return;
31
+ if (key in exports && exports[key] === _hex[key]) return;
32
+ Object.defineProperty(exports, key, {
33
+ enumerable: true,
34
+ get: function () {
35
+ return _hex[key];
14
36
  }
15
37
  });
16
38
  });
package/dist/index.d.ts CHANGED
@@ -1 +1,3 @@
1
- export * from "./sym";
1
+ export * from "./base-64";
2
+ export * from "./encryption";
3
+ export * from "./hex";
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export*from"./sym";
1
+ export*from"./base-64";export*from"./encryption";export*from"./hex";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/crypto",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "A package containing cryptographic utilities used by Storm Software.",
6
6
  "repository": {
@@ -9,9 +9,12 @@
9
9
  "directory": "packages/crypto"
10
10
  },
11
11
  "private": false,
12
- "dependencies": { "@stryke/type-checks": "^0.3.9" },
13
- "devDependencies": { "@types/node": "^22.14.0" },
12
+ "dependencies": {
13
+ "@stryke/convert": "^0.5.0",
14
+ "@stryke/type-checks": "^0.3.9"
15
+ },
14
16
  "publishConfig": { "access": "public" },
17
+ "devDependencies": {},
15
18
  "sideEffects": false,
16
19
  "files": ["dist/**/*"],
17
20
  "homepage": "https://stormsoftware.com",
@@ -55,11 +58,6 @@
55
58
  }
56
59
  ],
57
60
  "exports": {
58
- "./sym": {
59
- "import": { "types": "./dist/sym.d.ts", "default": "./dist/sym.mjs" },
60
- "require": { "types": "./dist/sym.d.ts", "default": "./dist/sym.cjs" },
61
- "default": { "types": "./dist/sym.d.ts", "default": "./dist/sym.mjs" }
62
- },
63
61
  "./index": {
64
62
  "import": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" },
65
63
  "require": {
@@ -68,6 +66,25 @@
68
66
  },
69
67
  "default": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" }
70
68
  },
69
+ "./hex": {
70
+ "import": { "types": "./dist/hex.d.ts", "default": "./dist/hex.mjs" },
71
+ "require": { "types": "./dist/hex.d.ts", "default": "./dist/hex.cjs" },
72
+ "default": { "types": "./dist/hex.d.ts", "default": "./dist/hex.mjs" }
73
+ },
74
+ "./encryption": {
75
+ "import": {
76
+ "types": "./dist/encryption.d.ts",
77
+ "default": "./dist/encryption.mjs"
78
+ },
79
+ "require": {
80
+ "types": "./dist/encryption.d.ts",
81
+ "default": "./dist/encryption.cjs"
82
+ },
83
+ "default": {
84
+ "types": "./dist/encryption.d.ts",
85
+ "default": "./dist/encryption.mjs"
86
+ }
87
+ },
71
88
  "./base-64": {
72
89
  "import": {
73
90
  "types": "./dist/base-64.d.ts",
@@ -95,5 +112,5 @@
95
112
  "main": "./dist/index.cjs",
96
113
  "module": "./dist/index.mjs",
97
114
  "types": "./dist/index.d.ts",
98
- "gitHead": "a27f10e735a05cca18b29026136249ed3bb76c1c"
115
+ "gitHead": "7331a32032c42269626a48f5011938b82dc80c45"
99
116
  }
package/dist/sym.cjs DELETED
@@ -1,39 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createSecret = createSecret;
7
- exports.decrypt = decrypt;
8
- exports.encrypt = encrypt;
9
- var _isSetString = require("@stryke/type-checks/is-set-string");
10
- var _nodeBuffer = require("node:buffer");
11
- var _nodeCrypto = require("node:crypto");
12
- const E = "chacha20-poly1305",
13
- h = 32,
14
- o = 16,
15
- S = 16,
16
- r = 64,
17
- T = 1e5;
18
- function createSecret(t, c) {
19
- return (0, _isSetString.isSetString)(t) ? (0, _nodeCrypto.createSecretKey)(t, c) : (0, _nodeCrypto.createSecretKey)(t);
20
- }
21
- function encrypt(t, c) {
22
- const e = (0, _nodeCrypto.randomBytes)(o),
23
- i = (0, _nodeCrypto.randomBytes)(r),
24
- s = (0, _nodeCrypto.pbkdf2Sync)(t, i, T, h, "sha512"),
25
- n = (0, _nodeCrypto.createCipheriv)(E, s, e),
26
- a = _nodeBuffer.Buffer.concat([n.update(c, "utf8"), n.final()]),
27
- f = n.getAuthTag();
28
- return _nodeBuffer.Buffer.concat([i, e, f, a]).toString("hex");
29
- }
30
- function decrypt(t, c) {
31
- const e = _nodeBuffer.Buffer.from(c, "hex"),
32
- i = e.slice(0, r),
33
- s = e.slice(r, r + o),
34
- n = e.slice(r + o, r + o + S),
35
- a = e.slice(r + o + S),
36
- f = (0, _nodeCrypto.pbkdf2Sync)(t, i, T, h, "sha512"),
37
- p = (0, _nodeCrypto.createDecipheriv)(E, f, s);
38
- return p.setAuthTag(n), p.update(a) + p.final("utf8");
39
- }
package/dist/sym.d.ts DELETED
@@ -1,36 +0,0 @@
1
- import type { BinaryLike, KeyObject } from "node:crypto";
2
- /**
3
- * Creates and returns a new key object containing a secret key for symmetric encryption or `Hmac`.
4
- *
5
- * @param key - The key to use when creating the `KeyObject`.
6
- * @returns The new `KeyObject`.
7
- */
8
- export declare function createSecret(key: NodeJS.ArrayBufferView): KeyObject;
9
- /**
10
- * Creates and returns a new key object containing a secret key for symmetric encryption or `Hmac`.
11
- *
12
- * @param key - The key to use. If `key` is a `Buffer`, `TypedArray`, or `DataView`, the `encoding` argument is ignored.
13
- * @param encoding - The `encoding` of the `key` string. Must be one of `'utf8'`, `'utf16le'`, `'latin1'`, or `'base64'`. Default is `'utf8'`.
14
- * @returns The new `KeyObject`.
15
- */
16
- export declare function createSecret(key: string, encoding: BufferEncoding): KeyObject;
17
- /**
18
- * Symmetrically encrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
19
- *
20
- * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
21
- *
22
- * @param secret - The secret key used for encryption.
23
- * @param data - The data to encrypt.
24
- * @returns The encrypted data.
25
- */
26
- export declare function encrypt(secret: BinaryLike, data: string): string;
27
- /**
28
- * Symmetrically decrypts data using the [ChaCha20-Poly1305](https://en.wikipedia.org/wiki/ChaCha20-Poly1305) cipher.
29
- *
30
- * @see https://en.wikipedia.org/wiki/ChaCha20-Poly1305
31
- *
32
- * @param secret - The secret key used for decryption.
33
- * @param encryptedData - The encrypted data to decrypt.
34
- * @returns The decrypted data.
35
- */
36
- export declare function decrypt(secret: BinaryLike, encryptedData: string): string;
package/dist/sym.mjs DELETED
@@ -1 +0,0 @@
1
- import{isSetString as _}from"@stryke/type-checks/is-set-string";import{Buffer as y}from"node:buffer";import{createCipheriv as m,createDecipheriv as B,createSecretKey as u,pbkdf2Sync as g,randomBytes as d}from"node:crypto";const E="chacha20-poly1305",h=32,o=16,S=16,r=64,T=1e5;export function createSecret(t,c){return _(t)?u(t,c):u(t)}export function encrypt(t,c){const e=d(o),i=d(r),s=g(t,i,T,h,"sha512"),n=m(E,s,e),a=y.concat([n.update(c,"utf8"),n.final()]),f=n.getAuthTag();return y.concat([i,e,f,a]).toString("hex")}export function decrypt(t,c){const e=y.from(c,"hex"),i=e.slice(0,r),s=e.slice(r,r+o),n=e.slice(r+o,r+o+S),a=e.slice(r+o+S),f=g(t,i,T,h,"sha512"),p=B(E,f,s);return p.setAuthTag(n),p.update(a)+p.final("utf8")}