@ysk-kit/crypto 1.0.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 +21 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 YSK Limited
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ICryptoPort {
|
|
2
|
+
encrypt(plain: Uint8Array): Promise<Uint8Array>;
|
|
3
|
+
decrypt(cipher: Uint8Array): Promise<Uint8Array>;
|
|
4
|
+
}
|
|
5
|
+
export declare const DEV_CRYPTO_KEY_HEX: string;
|
|
6
|
+
export declare const createAes256Gcm: (masterKeyHex: string) => ICryptoPort;
|
|
7
|
+
export declare const encryptUtf8: (port: ICryptoPort, text: string) => Promise<Uint8Array>;
|
|
8
|
+
export declare const decryptUtf8: (port: ICryptoPort, blob: Uint8Array) => Promise<string>;
|
|
9
|
+
export declare const createCryptoFromEnv: (env: {
|
|
10
|
+
CRYPTO_MASTER_KEY?: string | undefined;
|
|
11
|
+
NODE_ENV?: string | undefined;
|
|
12
|
+
}) => ICryptoPort;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAChD,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAClD;AAUD,eAAO,MAAM,kBAAkB,QAAkE,CAAC;AAElG,eAAO,MAAM,eAAe,GAAI,cAAc,MAAM,KAAG,WAsBtD,CAAC;AAEF,eAAO,MAAM,WAAW,GAAU,MAAM,WAAW,EAAE,MAAM,MAAM,KAAG,OAAO,CAAC,UAAU,CACxC,CAAC;AAE/C,eAAO,MAAM,WAAW,GAAU,MAAM,WAAW,EAAE,MAAM,UAAU,KAAG,OAAO,CAAC,MAAM,CAClC,CAAC;AAErD,eAAO,MAAM,mBAAmB,GAAI,KAAK;IACvC,iBAAiB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B,KAAG,WAMH,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
const VERSION = 1;
|
|
3
|
+
const IV_LENGTH = 12;
|
|
4
|
+
const TAG_LENGTH = 16;
|
|
5
|
+
const KEY_LENGTH = 32;
|
|
6
|
+
const parseKey = (hex) => {
|
|
7
|
+
const key = Buffer.from(hex, 'hex');
|
|
8
|
+
if (key.length !== KEY_LENGTH) {
|
|
9
|
+
throw new Error('CRYPTO_MASTER_KEY must be 64 hex chars (32 bytes)');
|
|
10
|
+
}
|
|
11
|
+
return key;
|
|
12
|
+
};
|
|
13
|
+
export const DEV_CRYPTO_KEY_HEX = createHash('sha256').update('ysk-kit-dev-crypto').digest('hex');
|
|
14
|
+
export const createAes256Gcm = (masterKeyHex) => {
|
|
15
|
+
const key = parseKey(masterKeyHex);
|
|
16
|
+
return {
|
|
17
|
+
async encrypt(plain) {
|
|
18
|
+
const iv = randomBytes(IV_LENGTH);
|
|
19
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
20
|
+
const ciphertext = Buffer.concat([cipher.update(plain), cipher.final()]);
|
|
21
|
+
const tag = cipher.getAuthTag();
|
|
22
|
+
return Buffer.concat([Buffer.from([VERSION]), iv, ciphertext, tag]);
|
|
23
|
+
},
|
|
24
|
+
async decrypt(blob) {
|
|
25
|
+
const buf = Buffer.from(blob);
|
|
26
|
+
if (buf.length < 1 + IV_LENGTH + TAG_LENGTH)
|
|
27
|
+
throw new Error('ciphertext too short');
|
|
28
|
+
if (buf[0] !== VERSION)
|
|
29
|
+
throw new Error('unsupported crypto version');
|
|
30
|
+
const iv = buf.subarray(1, 1 + IV_LENGTH);
|
|
31
|
+
const tag = buf.subarray(buf.length - TAG_LENGTH);
|
|
32
|
+
const ciphertext = buf.subarray(1 + IV_LENGTH, buf.length - TAG_LENGTH);
|
|
33
|
+
const decipher = createDecipheriv('aes-256-gcm', key, iv);
|
|
34
|
+
decipher.setAuthTag(tag);
|
|
35
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export const encryptUtf8 = async (port, text) => port.encrypt(new TextEncoder().encode(text));
|
|
40
|
+
export const decryptUtf8 = async (port, blob) => new TextDecoder().decode(await port.decrypt(blob));
|
|
41
|
+
export const createCryptoFromEnv = (env) => {
|
|
42
|
+
if (env.CRYPTO_MASTER_KEY)
|
|
43
|
+
return createAes256Gcm(env.CRYPTO_MASTER_KEY);
|
|
44
|
+
if (env.NODE_ENV === 'production') {
|
|
45
|
+
throw new Error('CRYPTO_MASTER_KEY is required in production');
|
|
46
|
+
}
|
|
47
|
+
return createAes256Gcm(DEV_CRYPTO_KEY_HEX);
|
|
48
|
+
};
|
|
49
|
+
//# 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,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAExF,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,UAAU,GAAG,EAAE,CAAC;AAOtB,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAU,EAAE;IACvC,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACpC,IAAI,GAAG,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC;IACvE,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,MAAM,CA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
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "^24.5.2",
|
|
15
|
+
"typescript": "^6.0.3",
|
|
16
|
+
"vitest": "^5.0.2"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"registry": "https://registry.npmjs.org",
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
27
|
+
"test": "vitest run --passWithNoTests",
|
|
28
|
+
"build": "tsc -p tsconfig.build.json"
|
|
29
|
+
}
|
|
30
|
+
}
|