@stryke/crypto 0.1.0 → 0.2.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/index.cjs +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sym.cjs +39 -0
- package/dist/sym.d.ts +36 -0
- package/dist/sym.mjs +1 -0
- package/package.json +8 -16
- package/dist/encryption.cjs +0 -35
- package/dist/encryption.d.ts +0 -17
- package/dist/encryption.mjs +0 -1
package/dist/index.cjs
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
var
|
|
7
|
-
Object.keys(
|
|
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] ===
|
|
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
|
|
13
|
+
return _sym[key];
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./sym";
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./
|
|
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.
|
|
3
|
+
"version": "0.2.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,20 +68,6 @@
|
|
|
62
68
|
},
|
|
63
69
|
"default": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" }
|
|
64
70
|
},
|
|
65
|
-
"./encryption": {
|
|
66
|
-
"import": {
|
|
67
|
-
"types": "./dist/encryption.d.ts",
|
|
68
|
-
"default": "./dist/encryption.mjs"
|
|
69
|
-
},
|
|
70
|
-
"require": {
|
|
71
|
-
"types": "./dist/encryption.d.ts",
|
|
72
|
-
"default": "./dist/encryption.cjs"
|
|
73
|
-
},
|
|
74
|
-
"default": {
|
|
75
|
-
"types": "./dist/encryption.d.ts",
|
|
76
|
-
"default": "./dist/encryption.mjs"
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
71
|
".": {
|
|
80
72
|
"import": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" },
|
|
81
73
|
"require": {
|
|
@@ -89,5 +81,5 @@
|
|
|
89
81
|
"main": "./dist/index.cjs",
|
|
90
82
|
"module": "./dist/index.mjs",
|
|
91
83
|
"types": "./dist/index.d.ts",
|
|
92
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "afd8f7b3e9698dbd9373ce05c950c61d489ed001"
|
|
93
85
|
}
|
package/dist/encryption.cjs
DELETED
|
@@ -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
|
-
}
|
package/dist/encryption.d.ts
DELETED
|
@@ -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;
|
package/dist/encryption.mjs
DELETED
|
@@ -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")}
|