@stryke/crypto 0.1.0 → 0.3.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.
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.base64FromBase64url = base64FromBase64url;
7
+ exports.base64ToBase64url = base64ToBase64url;
8
+ exports.fromBase64 = fromBase64;
9
+ exports.toBase64 = toBase64;
10
+ var _isUndefined = require("@stryke/type-checks/is-undefined");
11
+ 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
+ h = 61,
13
+ 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,
16
+ n = new Uint8Array(Math.ceil(f / 3) * 4),
17
+ 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];
21
+ }
22
+ const o = l;
23
+ switch (f % 3) {
24
+ case 1:
25
+ {
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;
28
+ break;
29
+ }
30
+ case 2:
31
+ {
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;
34
+ break;
35
+ }
36
+ }
37
+ return n;
38
+ }
39
+ function fromBase64(e) {
40
+ const f = e.length;
41
+ if (f % 4 !== 0) throw new Error("invalid length");
42
+ 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");
45
+ 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
+ }
52
+ const c = l;
53
+ switch (o % 3) {
54
+ case 1:
55
+ {
56
+ const t = s[e[4 * c + 0]] << 18 | s[e[4 * c + 1]] << 12;
57
+ r[3 * c] = t >> 16 & 255;
58
+ break;
59
+ }
60
+ case 2:
61
+ {
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;
64
+ break;
65
+ }
66
+ }
67
+ return r;
68
+ }
69
+ function base64ToBase64url(e) {
70
+ return e.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
71
+ }
72
+ function base64FromBase64url(e) {
73
+ return e.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - e.length % 4) % 4);
74
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Encodes a Uint8Array into a Base64 encoded Uint8Array.
3
+ *
4
+ * @param input - The input Uint8Array to encode.
5
+ * @returns The Base64 encoded Uint8Array.
6
+ */
7
+ export declare function toBase64(input: Uint8Array): Uint8Array;
8
+ /**
9
+ * Decodes a Base64 encoded Uint8Array into a Uint8Array.
10
+ *
11
+ * @param input - The Base64 encoded Uint8Array to decode.
12
+ * @returns The decoded Uint8Array.
13
+ */
14
+ export declare function fromBase64(input: Uint8Array): Uint8Array;
15
+ /**
16
+ * Converts a Base64 encoded string to a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string.
17
+ *
18
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C
19
+ *
20
+ * @param base64 - The Base64 encoded string to convert.
21
+ * @returns The Base64url encoded string.
22
+ */
23
+ export declare function base64ToBase64url(base64: string): string;
24
+ /**
25
+ * Converts a [Base64url](https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) encoded string to a Base64 encoded string.
26
+ *
27
+ * @see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C *
28
+ *
29
+ * @param base64url - The Base64url encoded string to convert.
30
+ * @returns The Base64 encoded string.
31
+ */
32
+ export declare function base64FromBase64url(base64url: string): string;
@@ -0,0 +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)}
package/dist/index.cjs CHANGED
@@ -3,14 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _encryption = require("./encryption.cjs");
7
- Object.keys(_encryption).forEach(function (key) {
6
+ var _sym = require("./sym.cjs");
7
+ Object.keys(_sym).forEach(function (key) {
8
8
  if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _encryption[key]) return;
9
+ if (key in exports && exports[key] === _sym[key]) return;
10
10
  Object.defineProperty(exports, key, {
11
11
  enumerable: true,
12
12
  get: function () {
13
- return _encryption[key];
13
+ return _sym[key];
14
14
  }
15
15
  });
16
16
  });
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from "./encryption";
1
+ export * from "./sym";
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export*from"./encryption";
1
+ export*from"./sym";
package/dist/sym.cjs ADDED
@@ -0,0 +1,39 @@
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 ADDED
@@ -0,0 +1,36 @@
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 ADDED
@@ -0,0 +1 @@
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")}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/crypto",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "A package containing cryptographic utilities used by Storm Software.",
6
6
  "repository": {
@@ -9,6 +9,7 @@
9
9
  "directory": "packages/crypto"
10
10
  },
11
11
  "private": false,
12
+ "dependencies": { "@stryke/type-checks": "^0.3.9" },
12
13
  "devDependencies": { "@types/node": "^22.14.0" },
13
14
  "publishConfig": { "access": "public" },
14
15
  "sideEffects": false,
@@ -54,6 +55,11 @@
54
55
  }
55
56
  ],
56
57
  "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
+ },
57
63
  "./index": {
58
64
  "import": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" },
59
65
  "require": {
@@ -62,18 +68,18 @@
62
68
  },
63
69
  "default": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" }
64
70
  },
65
- "./encryption": {
71
+ "./base-64": {
66
72
  "import": {
67
- "types": "./dist/encryption.d.ts",
68
- "default": "./dist/encryption.mjs"
73
+ "types": "./dist/base-64.d.ts",
74
+ "default": "./dist/base-64.mjs"
69
75
  },
70
76
  "require": {
71
- "types": "./dist/encryption.d.ts",
72
- "default": "./dist/encryption.cjs"
77
+ "types": "./dist/base-64.d.ts",
78
+ "default": "./dist/base-64.cjs"
73
79
  },
74
80
  "default": {
75
- "types": "./dist/encryption.d.ts",
76
- "default": "./dist/encryption.mjs"
81
+ "types": "./dist/base-64.d.ts",
82
+ "default": "./dist/base-64.mjs"
77
83
  }
78
84
  },
79
85
  ".": {
@@ -89,5 +95,5 @@
89
95
  "main": "./dist/index.cjs",
90
96
  "module": "./dist/index.mjs",
91
97
  "types": "./dist/index.d.ts",
92
- "gitHead": "3326ca10ce43fd782ffb2fa29d49bf152e1dac05"
98
+ "gitHead": "a27f10e735a05cca18b29026136249ed3bb76c1c"
93
99
  }
@@ -1,35 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.decryptWithSecret = decryptWithSecret;
7
- exports.encryptWithSecret = encryptWithSecret;
8
- var _nodeBuffer = require("node:buffer");
9
- var _nodeCrypto = _interopRequireDefault(require("node:crypto"));
10
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
- const g = "aes-256-gcm",
12
- y = 32,
13
- r = 16,
14
- E = 16,
15
- c = 64,
16
- T = 1e5;
17
- function encryptWithSecret(o, i) {
18
- const t = _nodeCrypto.default.randomBytes(r),
19
- s = _nodeCrypto.default.randomBytes(c),
20
- a = _nodeCrypto.default.pbkdf2Sync(o, s, T, y, "sha512"),
21
- n = _nodeCrypto.default.createCipheriv(g, a, t),
22
- f = _nodeBuffer.Buffer.concat([n.update(i, "utf8"), n.final()]),
23
- p = n.getAuthTag();
24
- return _nodeBuffer.Buffer.concat([s, t, p, f]).toString("hex");
25
- }
26
- function decryptWithSecret(o, i) {
27
- const t = _nodeBuffer.Buffer.from(i, "hex"),
28
- s = t.slice(0, c),
29
- a = t.slice(c, c + r),
30
- n = t.slice(c + r, c + r + E),
31
- f = t.slice(c + r + E),
32
- p = _nodeCrypto.default.pbkdf2Sync(o, s, T, y, "sha512"),
33
- u = _nodeCrypto.default.createDecipheriv(g, p, a);
34
- return u.setAuthTag(n), u.update(f) + u.final("utf8");
35
- }
@@ -1,17 +0,0 @@
1
- import { Buffer } from "node:buffer";
2
- /**
3
- * Encrypts data using a secret.
4
- *
5
- * @param secret - The secret key used for encryption.
6
- * @param data - The data to encrypt.
7
- * @returns The encrypted data.
8
- */
9
- export declare function encryptWithSecret(secret: Buffer, data: string): string;
10
- /**
11
- * Decrypts data using a secret.
12
- *
13
- * @param secret - The secret key used for decryption.
14
- * @param encryptedData - The encrypted data to decrypt.
15
- * @returns The decrypted data.
16
- */
17
- export declare function decryptWithSecret(secret: Buffer, encryptedData: string): string;
@@ -1 +0,0 @@
1
- import{Buffer as h}from"node:buffer";import e from"node:crypto";const g="aes-256-gcm",y=32,r=16,E=16,c=64,T=1e5;export function encryptWithSecret(o,i){const t=e.randomBytes(r),s=e.randomBytes(c),a=e.pbkdf2Sync(o,s,T,y,"sha512"),n=e.createCipheriv(g,a,t),f=h.concat([n.update(i,"utf8"),n.final()]),p=n.getAuthTag();return h.concat([s,t,p,f]).toString("hex")}export function decryptWithSecret(o,i){const t=h.from(i,"hex"),s=t.slice(0,c),a=t.slice(c,c+r),n=t.slice(c+r,c+r+E),f=t.slice(c+r+E),p=e.pbkdf2Sync(o,s,T,y,"sha512"),u=e.createDecipheriv(g,p,a);return u.setAuthTag(n),u.update(f)+u.final("utf8")}