@webframe/encrypt 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 tw050x
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,22 @@
1
- # Temporary Holding Version
1
+ # @webframe/encrypt
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Encrypts and decrypts small pieces of state, such as the `state` of an OAuth sign-in or a value kept in a cookie, with AES-256-GCM.
4
+
5
+ ```sh
6
+ npm install @webframe/encrypt
7
+ ```
8
+
9
+ ```ts
10
+ import { encrypt, decrypt, generateKey } from '@webframe/encrypt'
11
+
12
+ const key = generateKey() // keep it in a secret: 64 hex characters
13
+
14
+ const sealed = encrypt(JSON.stringify({ returnTo: '/account', nonce }), key)
15
+ // '{"iv":"…","tag":"…","content":"…"}'
16
+
17
+ decrypt(sealed, key) // { returnTo: '/account', nonce: '…' }
18
+ ```
19
+
20
+ The full documentation is at [webframe.tw050x.net/packages/encrypt](https://webframe.tw050x.net/packages/encrypt).
21
+
22
+ Part of [webframe](https://webframe.tw050x.net), MIT licensed.
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Makes a key for `encrypt` and `decrypt`: 32 random bytes, as hex.
3
+ */
4
+ export declare const generateKey: () => string;
5
+ /**
6
+ * Encrypts a string with a key (64 hex characters, from `generateKey`). The result is JSON
7
+ * holding the IV, the authentication tag and the ciphertext, so it can't be altered unnoticed.
8
+ *
9
+ * @example
10
+ * const sealed = encrypt(JSON.stringify({ returnTo: '/account' }), key);
11
+ * decrypt(sealed, key); // { returnTo: '/account' }
12
+ */
13
+ export declare const encrypt: (state: string, key: string) => string;
14
+ /**
15
+ * Decrypts what `encrypt` made and parses it as JSON. Throws if it isn't valid, was made with
16
+ * another key, or has been altered.
17
+ */
18
+ export declare const decrypt: (encryptedState: string, key: string) => unknown;
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,eAAO,MAAM,WAAW,QAAO,MAAyC,CAAC;AAUzE;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,KAAG,MASpD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,OAAO,GAAI,gBAAgB,MAAM,EAAE,KAAK,MAAM,KAAG,OA6B7D,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,64 @@
1
+ import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
2
+ const ALGORITHM = 'aes-256-gcm';
3
+ /**
4
+ * Makes a key for `encrypt` and `decrypt`: 32 random bytes, as hex.
5
+ */
6
+ export const generateKey = () => randomBytes(32).toString('hex');
7
+ const keyOf = (key) => {
8
+ const bytes = Buffer.from(key, 'hex');
9
+ if (bytes.length !== 32) {
10
+ throw new Error('The key must be 32 bytes, as 64 hex characters');
11
+ }
12
+ return bytes;
13
+ };
14
+ /**
15
+ * Encrypts a string with a key (64 hex characters, from `generateKey`). The result is JSON
16
+ * holding the IV, the authentication tag and the ciphertext, so it can't be altered unnoticed.
17
+ *
18
+ * @example
19
+ * const sealed = encrypt(JSON.stringify({ returnTo: '/account' }), key);
20
+ * decrypt(sealed, key); // { returnTo: '/account' }
21
+ */
22
+ export const encrypt = (state, key) => {
23
+ const iv = randomBytes(12);
24
+ const cipher = createCipheriv(ALGORITHM, keyOf(key), iv);
25
+ const content = Buffer.concat([cipher.update(state, 'utf8'), cipher.final()]);
26
+ return JSON.stringify({
27
+ iv: iv.toString('hex'),
28
+ tag: cipher.getAuthTag().toString('hex'),
29
+ content: content.toString('hex'),
30
+ });
31
+ };
32
+ /**
33
+ * Decrypts what `encrypt` made and parses it as JSON. Throws if it isn't valid, was made with
34
+ * another key, or has been altered.
35
+ */
36
+ export const decrypt = (encryptedState, key) => {
37
+ let sealed;
38
+ try {
39
+ sealed = JSON.parse(encryptedState);
40
+ }
41
+ catch (error) {
42
+ throw new Error('Invalid encrypted state format', { cause: error });
43
+ }
44
+ const { iv, tag, content } = sealed ?? {};
45
+ if (typeof iv !== 'string' || typeof tag !== 'string' || typeof content !== 'string') {
46
+ throw new Error('Invalid encrypted state format');
47
+ }
48
+ let decrypted;
49
+ try {
50
+ const decipher = createDecipheriv(ALGORITHM, keyOf(key), Buffer.from(iv, 'hex'));
51
+ decipher.setAuthTag(Buffer.from(tag, 'hex'));
52
+ decrypted = Buffer.concat([decipher.update(Buffer.from(content, 'hex')), decipher.final()]).toString('utf8');
53
+ }
54
+ catch (error) {
55
+ throw new Error('Could not decrypt the state', { cause: error });
56
+ }
57
+ try {
58
+ return JSON.parse(decrypted);
59
+ }
60
+ catch (error) {
61
+ throw new Error('Invalid decrypted state format', { cause: error });
62
+ }
63
+ };
64
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE5E,MAAM,SAAS,GAAG,aAAa,CAAC;AAEhC;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAW,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAEzE,MAAM,KAAK,GAAG,CAAC,GAAW,EAAE,EAAE;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACtC,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAa,EAAE,GAAW,EAAU,EAAE;IAC5D,MAAM,EAAE,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;IAC3B,MAAM,MAAM,GAAG,cAAc,CAAC,SAAS,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;IACzD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IAC9E,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC;QACtB,GAAG,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC;QACxC,OAAO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E,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACnE,CAAC;IAED,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,gCAAgC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACtE,CAAC;AACH,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@webframe/encrypt",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Encrypt and decrypt small pieces of state (such as OAuth state) with AES-256-GCM.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "src"
16
+ ],
17
+ "dependencies": {},
18
+ "engines": {
19
+ "node": ">=22"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/tw050x/webframe.git",
24
+ "directory": "packages/node/encrypt"
25
+ },
26
+ "homepage": "https://webframe.tw050x.net/packages/encrypt",
27
+ "publishConfig": {
28
+ "access": "public"
29
+ }
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,70 @@
1
+ import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
2
+
3
+ const ALGORITHM = 'aes-256-gcm';
4
+
5
+ /**
6
+ * Makes a key for `encrypt` and `decrypt`: 32 random bytes, as hex.
7
+ */
8
+ export const generateKey = (): string => randomBytes(32).toString('hex');
9
+
10
+ const keyOf = (key: string) => {
11
+ const bytes = Buffer.from(key, 'hex');
12
+ if (bytes.length !== 32) {
13
+ throw new Error('The key must be 32 bytes, as 64 hex characters');
14
+ }
15
+ return bytes;
16
+ }
17
+
18
+ /**
19
+ * Encrypts a string with a key (64 hex characters, from `generateKey`). The result is JSON
20
+ * holding the IV, the authentication tag and the ciphertext, so it can't be altered unnoticed.
21
+ *
22
+ * @example
23
+ * const sealed = encrypt(JSON.stringify({ returnTo: '/account' }), key);
24
+ * decrypt(sealed, key); // { returnTo: '/account' }
25
+ */
26
+ export const encrypt = (state: string, key: string): string => {
27
+ const iv = randomBytes(12);
28
+ const cipher = createCipheriv(ALGORITHM, keyOf(key), iv);
29
+ const content = Buffer.concat([cipher.update(state, 'utf8'), cipher.final()]);
30
+ return JSON.stringify({
31
+ iv: iv.toString('hex'),
32
+ tag: cipher.getAuthTag().toString('hex'),
33
+ content: content.toString('hex'),
34
+ });
35
+ }
36
+
37
+ /**
38
+ * Decrypts what `encrypt` made and parses it as JSON. Throws if it isn't valid, was made with
39
+ * another key, or has been altered.
40
+ */
41
+ export const decrypt = (encryptedState: string, key: string): unknown => {
42
+ let sealed: { iv?: unknown; tag?: unknown; content?: unknown };
43
+ try {
44
+ sealed = JSON.parse(encryptedState);
45
+ }
46
+ catch (error) {
47
+ throw new Error('Invalid encrypted state format', { cause: error });
48
+ }
49
+ const { iv, tag, content } = sealed ?? {};
50
+ if (typeof iv !== 'string' || typeof tag !== 'string' || typeof content !== 'string') {
51
+ throw new Error('Invalid encrypted state format');
52
+ }
53
+
54
+ let decrypted: string;
55
+ try {
56
+ const decipher = createDecipheriv(ALGORITHM, keyOf(key), Buffer.from(iv, 'hex'));
57
+ decipher.setAuthTag(Buffer.from(tag, 'hex'));
58
+ decrypted = Buffer.concat([decipher.update(Buffer.from(content, 'hex')), decipher.final()]).toString('utf8');
59
+ }
60
+ catch (error) {
61
+ throw new Error('Could not decrypt the state', { cause: error });
62
+ }
63
+
64
+ try {
65
+ return JSON.parse(decrypted);
66
+ }
67
+ catch (error) {
68
+ throw new Error('Invalid decrypted state format', { cause: error });
69
+ }
70
+ }