@cryptforge/cryptography 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/README.md +331 -0
- package/dist/index.d.mts +125 -0
- package/dist/index.d.ts +125 -0
- package/dist/index.js +119 -0
- package/dist/index.mjs +91 -0
- package/dist/server.d.mts +172 -0
- package/dist/server.d.ts +172 -0
- package/dist/server.js +213 -0
- package/dist/server.mjs +174 -0
- package/package.json +50 -0
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// src/client/CryptoBrowser.ts
|
|
2
|
+
var CryptoBrowser = class {
|
|
3
|
+
encryptionKey;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new CryptoBrowser instance.
|
|
6
|
+
* @param getEncryptionKey - Function that returns the encryption key for this instance
|
|
7
|
+
*/
|
|
8
|
+
constructor() {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Sets the encryption key for the encrypt and decrypt operations.
|
|
12
|
+
* Validates that the key is AES-GCM with 256-bit length for cross-platform compatibility.
|
|
13
|
+
*
|
|
14
|
+
* @param key - The encryption key to set (must be AES-GCM 256-bit)
|
|
15
|
+
* @throws {Error} If key is not AES-GCM or not 256-bit
|
|
16
|
+
*/
|
|
17
|
+
setEncryptionKey = (key) => {
|
|
18
|
+
if (key.algorithm.name !== "AES-GCM") {
|
|
19
|
+
throw new Error(
|
|
20
|
+
`Invalid key algorithm: ${key.algorithm.name}. Key must be AES-GCM for cross-platform compatibility with CryptoServer.`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const keyLength = key.algorithm.length;
|
|
24
|
+
if (keyLength !== 256) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Invalid key length: ${keyLength} bits. Key must be 256-bit for cross-platform compatibility with CryptoServer (AES-256-GCM).`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
this.encryptionKey = key;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Creates an encryption function that encrypts string data using AES-GCM encryption.
|
|
33
|
+
* Uses a random initialization vector (IV) for each encryption operation.
|
|
34
|
+
* The IV is prepended to the ciphertext and the result is base64-encoded.
|
|
35
|
+
* Uses the encryption key provided via the `setEncryptionKey` method.
|
|
36
|
+
*
|
|
37
|
+
* @returns A curried function that encrypts data
|
|
38
|
+
*/
|
|
39
|
+
encrypt = () => async (data) => {
|
|
40
|
+
const key = this.encryptionKey;
|
|
41
|
+
if (!key) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
"Encryption key not available. You must call `setEncryptionKey` before using this instance."
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
47
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
48
|
+
{ name: "AES-GCM", iv },
|
|
49
|
+
key,
|
|
50
|
+
new TextEncoder().encode(data)
|
|
51
|
+
);
|
|
52
|
+
const combined = new Uint8Array(iv.length + encrypted.byteLength);
|
|
53
|
+
combined.set(iv);
|
|
54
|
+
combined.set(new Uint8Array(encrypted), iv.length);
|
|
55
|
+
return btoa(String.fromCharCode(...combined));
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Creates a decryption function that decrypts encrypted string data using AES-GCM decryption.
|
|
59
|
+
* Extracts the IV from the beginning of the encrypted data and uses it to decrypt the ciphertext.
|
|
60
|
+
* Uses the encryption key provided via the constructor.
|
|
61
|
+
*
|
|
62
|
+
* @returns A curried function that decrypts data
|
|
63
|
+
*/
|
|
64
|
+
decrypt = () => async (encryptedData) => {
|
|
65
|
+
const key = this.encryptionKey;
|
|
66
|
+
if (!key) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
"Encryption key not available. You must call `setEncryptionKey` before using this instance."
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
const combined = Uint8Array.from(
|
|
72
|
+
atob(encryptedData),
|
|
73
|
+
(c) => c.charCodeAt(0)
|
|
74
|
+
);
|
|
75
|
+
const iv = combined.slice(0, 12);
|
|
76
|
+
const ciphertext = combined.slice(12);
|
|
77
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
78
|
+
{ name: "AES-GCM", iv },
|
|
79
|
+
key,
|
|
80
|
+
ciphertext
|
|
81
|
+
);
|
|
82
|
+
return new TextDecoder().decode(decrypted);
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/server/CryptoServer.ts
|
|
87
|
+
import crypto2 from "crypto";
|
|
88
|
+
var CryptoServer = class {
|
|
89
|
+
/**
|
|
90
|
+
* The encryption key for the encrypt and decrypt operations.
|
|
91
|
+
*/
|
|
92
|
+
encryptionKey;
|
|
93
|
+
/**
|
|
94
|
+
* Creates a new CryptoServer instance.
|
|
95
|
+
*/
|
|
96
|
+
constructor() {
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Sets the encryption key for the encrypt and decrypt operations.
|
|
100
|
+
* Validates that the key is 32 bytes (256 bits) for AES-256-GCM.
|
|
101
|
+
*
|
|
102
|
+
* @param key - The encryption key to set (must be 32 bytes / 256 bits)
|
|
103
|
+
* @throws {Error} If key is not 32 bytes
|
|
104
|
+
*/
|
|
105
|
+
setEncryptionKey = (key) => {
|
|
106
|
+
if (key.length !== 32) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Invalid key length: ${key.length} bytes (${key.length * 8} bits). Key must be 32 bytes (256 bits) for AES-256-GCM cross-platform compatibility with CryptoBrowser.`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
this.encryptionKey = key;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Creates an encryption function that encrypts string data using AES-GCM encryption.
|
|
115
|
+
* Uses a random initialization vector (IV) for each encryption operation.
|
|
116
|
+
* The IV is prepended to the ciphertext and the result is base64-encoded.
|
|
117
|
+
* Uses the encryption key provided via the `setEncryptionKey` method.
|
|
118
|
+
*
|
|
119
|
+
* Format matches CryptoBrowser: [IV (12 bytes)][Ciphertext + AuthTag (16 bytes)] -> base64
|
|
120
|
+
*
|
|
121
|
+
* @returns A curried function that encrypts data
|
|
122
|
+
*/
|
|
123
|
+
encrypt = () => async (data) => {
|
|
124
|
+
const key = this.encryptionKey;
|
|
125
|
+
if (!key) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
"Encryption key not available. You must call `setEncryptionKey` before using this instance."
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const iv = crypto2.randomBytes(12);
|
|
131
|
+
const cipher = crypto2.createCipheriv("aes-256-gcm", key, iv);
|
|
132
|
+
let encrypted = cipher.update(data, "utf8");
|
|
133
|
+
encrypted = Buffer.concat([encrypted, cipher.final()]);
|
|
134
|
+
const authTag = cipher.getAuthTag();
|
|
135
|
+
const ciphertextWithAuthTag = Buffer.concat([encrypted, authTag]);
|
|
136
|
+
const combined = Buffer.concat([iv, ciphertextWithAuthTag]);
|
|
137
|
+
return combined.toString("base64");
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Creates a decryption function that decrypts encrypted string data using AES-GCM decryption.
|
|
141
|
+
* Extracts the IV from the beginning of the encrypted data and uses it to decrypt the ciphertext.
|
|
142
|
+
* Uses the encryption key provided via the constructor.
|
|
143
|
+
*
|
|
144
|
+
* Format matches CryptoBrowser: base64 -> [IV (12 bytes)][Ciphertext + AuthTag (16 bytes)]
|
|
145
|
+
*
|
|
146
|
+
* @returns A curried function that decrypts data
|
|
147
|
+
*/
|
|
148
|
+
decrypt = () => async (encryptedData) => {
|
|
149
|
+
const key = this.encryptionKey;
|
|
150
|
+
if (!key) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
"Encryption key not available. You must call `setEncryptionKey` before using this instance."
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
const combined = Buffer.from(encryptedData, "base64");
|
|
156
|
+
const iv = combined.slice(0, 12);
|
|
157
|
+
const ciphertextWithAuthTag = combined.slice(12);
|
|
158
|
+
const authTag = ciphertextWithAuthTag.slice(-16);
|
|
159
|
+
const ciphertext = ciphertextWithAuthTag.slice(0, -16);
|
|
160
|
+
const decipher = crypto2.createDecipheriv("aes-256-gcm", key, iv);
|
|
161
|
+
decipher.setAuthTag(authTag);
|
|
162
|
+
let decrypted = decipher.update(ciphertext);
|
|
163
|
+
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
164
|
+
return decrypted.toString("utf8");
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// src/server.ts
|
|
169
|
+
var version = "0.1.0-server";
|
|
170
|
+
export {
|
|
171
|
+
CryptoBrowser,
|
|
172
|
+
CryptoServer,
|
|
173
|
+
version
|
|
174
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cryptforge/cryptography",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Cryptographic operations for CryptForge applications - supports both client and server environments",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/server.d.ts",
|
|
16
|
+
"import": "./dist/server.mjs",
|
|
17
|
+
"require": "./dist/server.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"cryptforge",
|
|
22
|
+
"cryptography",
|
|
23
|
+
"encryption",
|
|
24
|
+
"decryption",
|
|
25
|
+
"security",
|
|
26
|
+
"crypto"
|
|
27
|
+
],
|
|
28
|
+
"author": "Viktor Albus",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/viktor-albus/cryptforge-sdk.git",
|
|
33
|
+
"directory": "packages/cryptography"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/viktor-albus/cryptforge-sdk#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/viktor-albus/cryptforge-sdk/issues"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsup",
|
|
45
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@cryptforge/core": "workspace:*"
|
|
49
|
+
}
|
|
50
|
+
}
|