@rscc/common-core 0.1.2 → 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/crypto.cjs +116 -0
- package/dist/crypto.d.cts +26 -0
- package/dist/crypto.d.ts +26 -0
- package/dist/crypto.js +89 -0
- package/dist/index.cjs +947 -4
- package/dist/index.d.cts +552 -1
- package/dist/index.d.ts +552 -1
- package/dist/index.js +912 -3
- package/package.json +12 -2
package/dist/crypto.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/crypto.ts
|
|
21
|
+
var crypto_exports = {};
|
|
22
|
+
__export(crypto_exports, {
|
|
23
|
+
CryptoError: () => CryptoError,
|
|
24
|
+
decryptAesGcm: () => decryptAesGcm,
|
|
25
|
+
encryptAesGcm: () => encryptAesGcm
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(crypto_exports);
|
|
28
|
+
var import_node_crypto = require("crypto");
|
|
29
|
+
var ALGORITHM = "aes-256-gcm";
|
|
30
|
+
var IV_LENGTH_BYTES = 12;
|
|
31
|
+
var TAG_LENGTH_BYTES = 16;
|
|
32
|
+
var KEY_LENGTH_BYTES = 32;
|
|
33
|
+
var JAVA_BLANK_PATTERN = /^[\t-\r\u001C-\u001F \u1680\u2000-\u2006\u2008-\u200A\u2028\u2029\u205F\u3000]*$/;
|
|
34
|
+
var BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
35
|
+
var utf8Encoder = new TextEncoder();
|
|
36
|
+
var utf8Decoder = new TextDecoder();
|
|
37
|
+
var CryptoError = class extends Error {
|
|
38
|
+
constructor(message) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.name = "CryptoError";
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
function concatBytes(...parts) {
|
|
44
|
+
let total = 0;
|
|
45
|
+
for (const part of parts) total += part.length;
|
|
46
|
+
const out = new Uint8Array(total);
|
|
47
|
+
let offset = 0;
|
|
48
|
+
for (const part of parts) {
|
|
49
|
+
out.set(part, offset);
|
|
50
|
+
offset += part.length;
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
function toBase64(bytes) {
|
|
55
|
+
let binary = "";
|
|
56
|
+
const CHUNK = 32768;
|
|
57
|
+
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
58
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
59
|
+
}
|
|
60
|
+
return btoa(binary);
|
|
61
|
+
}
|
|
62
|
+
function fromBase64(base64) {
|
|
63
|
+
if (base64.length % 4 !== 0 || !BASE64_PATTERN.test(base64)) {
|
|
64
|
+
throw new TypeError("\uC554\uD638\uBB38\uC774 \uC62C\uBC14\uB978 Base64 \uD615\uC2DD\uC774 \uC544\uB2D9\uB2C8\uB2E4.");
|
|
65
|
+
}
|
|
66
|
+
const binary = atob(base64);
|
|
67
|
+
const bytes = new Uint8Array(binary.length);
|
|
68
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
69
|
+
return bytes;
|
|
70
|
+
}
|
|
71
|
+
function deriveKey(key) {
|
|
72
|
+
if (key == null || JAVA_BLANK_PATTERN.test(key)) {
|
|
73
|
+
throw new TypeError("\uC554\uD638\uD654 \uD0A4\uB294 null \uB610\uB294 \uACF5\uBC31\uC77C \uC218 \uC5C6\uC2B5\uB2C8\uB2E4.");
|
|
74
|
+
}
|
|
75
|
+
const keyBytes = utf8Encoder.encode(key);
|
|
76
|
+
if (keyBytes.length !== KEY_LENGTH_BYTES) {
|
|
77
|
+
return (0, import_node_crypto.createHash)("sha256").update(keyBytes).digest();
|
|
78
|
+
}
|
|
79
|
+
return keyBytes;
|
|
80
|
+
}
|
|
81
|
+
function encryptAesGcm(plainText, key) {
|
|
82
|
+
if (plainText == null || JAVA_BLANK_PATTERN.test(plainText)) return plainText;
|
|
83
|
+
const keyBytes = deriveKey(key);
|
|
84
|
+
const iv = (0, import_node_crypto.randomBytes)(IV_LENGTH_BYTES);
|
|
85
|
+
const cipher = (0, import_node_crypto.createCipheriv)(ALGORITHM, keyBytes, iv);
|
|
86
|
+
const encrypted = concatBytes(cipher.update(utf8Encoder.encode(plainText)), cipher.final());
|
|
87
|
+
return toBase64(concatBytes(iv, encrypted, cipher.getAuthTag()));
|
|
88
|
+
}
|
|
89
|
+
function decryptAesGcm(cipherText, key) {
|
|
90
|
+
if (cipherText == null || JAVA_BLANK_PATTERN.test(cipherText)) return cipherText;
|
|
91
|
+
const keyBytes = deriveKey(key);
|
|
92
|
+
const combined = fromBase64(cipherText);
|
|
93
|
+
if (combined.length <= IV_LENGTH_BYTES) {
|
|
94
|
+
throw new TypeError("\uC554\uD638\uBB38 \uAE38\uC774\uAC00 \uC62C\uBC14\uB974\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4. (IV \uD3EC\uD568 \uCD5C\uC18C \uAE38\uC774 \uBBF8\uB2EC)");
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const iv = combined.subarray(0, IV_LENGTH_BYTES);
|
|
98
|
+
const body = combined.subarray(IV_LENGTH_BYTES);
|
|
99
|
+
const data = body.subarray(0, Math.max(0, body.length - TAG_LENGTH_BYTES));
|
|
100
|
+
const tag = body.subarray(Math.max(0, body.length - TAG_LENGTH_BYTES));
|
|
101
|
+
const decipher = (0, import_node_crypto.createDecipheriv)(ALGORITHM, keyBytes, iv, {
|
|
102
|
+
authTagLength: TAG_LENGTH_BYTES
|
|
103
|
+
});
|
|
104
|
+
decipher.setAuthTag(tag);
|
|
105
|
+
const decrypted = concatBytes(decipher.update(data), decipher.final());
|
|
106
|
+
return utf8Decoder.decode(decrypted);
|
|
107
|
+
} catch {
|
|
108
|
+
throw new CryptoError("\uB370\uC774\uD130 \uBCF5\uD638\uD654\uC5D0 \uC2E4\uD328\uD588\uC2B5\uB2C8\uB2E4. (\uD0A4\uAC00 \uB2E4\uB974\uAC70\uB098 \uB370\uC774\uD130\uAC00 \uBCC0\uC870\uB418\uC5C8\uC2B5\uB2C8\uB2E4)");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
CryptoError,
|
|
114
|
+
decryptAesGcm,
|
|
115
|
+
encryptAesGcm
|
|
116
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 복호화 실패(키 상이·데이터 변조 — GCM 태그 불일치) 오류.
|
|
3
|
+
* Java `IllegalStateException` / Python `CryptoError(ValueError)` 에 대응.
|
|
4
|
+
*/
|
|
5
|
+
declare class CryptoError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 평문을 AES-256/GCM 으로 암호화한다 — contracts/crypto.md.
|
|
10
|
+
*
|
|
11
|
+
* @param plainText 암호화할 평문 (blank 면 그대로 반환 — Java 파리티).
|
|
12
|
+
* @param key 암호화 키 (blank 면 TypeError. 32바이트 정확 일치 시 raw, 그 외 SHA-256 유도).
|
|
13
|
+
* @returns Base64( IV(12B) ∥ 암호문 ∥ 태그(16B) ) — IV 가 랜덤이라 매번 다르다.
|
|
14
|
+
*/
|
|
15
|
+
declare function encryptAesGcm(plainText: string, key: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* `encryptAesGcm`(또는 Java `CryptoUtils.encrypt`) 산출물을 복호화한다.
|
|
18
|
+
*
|
|
19
|
+
* @param cipherText Base64 암호문 (blank 면 그대로 반환).
|
|
20
|
+
* @param key 암호화에 사용했던 키.
|
|
21
|
+
* @throws TypeError 키 blank / Base64 형식 오류 / 길이 미달(디코드 후 ≤ 12바이트).
|
|
22
|
+
* @throws CryptoError 키가 다르거나 데이터가 변조된 경우 (GCM 태그 불일치).
|
|
23
|
+
*/
|
|
24
|
+
declare function decryptAesGcm(cipherText: string, key: string): string;
|
|
25
|
+
|
|
26
|
+
export { CryptoError, decryptAesGcm, encryptAesGcm };
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 복호화 실패(키 상이·데이터 변조 — GCM 태그 불일치) 오류.
|
|
3
|
+
* Java `IllegalStateException` / Python `CryptoError(ValueError)` 에 대응.
|
|
4
|
+
*/
|
|
5
|
+
declare class CryptoError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 평문을 AES-256/GCM 으로 암호화한다 — contracts/crypto.md.
|
|
10
|
+
*
|
|
11
|
+
* @param plainText 암호화할 평문 (blank 면 그대로 반환 — Java 파리티).
|
|
12
|
+
* @param key 암호화 키 (blank 면 TypeError. 32바이트 정확 일치 시 raw, 그 외 SHA-256 유도).
|
|
13
|
+
* @returns Base64( IV(12B) ∥ 암호문 ∥ 태그(16B) ) — IV 가 랜덤이라 매번 다르다.
|
|
14
|
+
*/
|
|
15
|
+
declare function encryptAesGcm(plainText: string, key: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* `encryptAesGcm`(또는 Java `CryptoUtils.encrypt`) 산출물을 복호화한다.
|
|
18
|
+
*
|
|
19
|
+
* @param cipherText Base64 암호문 (blank 면 그대로 반환).
|
|
20
|
+
* @param key 암호화에 사용했던 키.
|
|
21
|
+
* @throws TypeError 키 blank / Base64 형식 오류 / 길이 미달(디코드 후 ≤ 12바이트).
|
|
22
|
+
* @throws CryptoError 키가 다르거나 데이터가 변조된 경우 (GCM 태그 불일치).
|
|
23
|
+
*/
|
|
24
|
+
declare function decryptAesGcm(cipherText: string, key: string): string;
|
|
25
|
+
|
|
26
|
+
export { CryptoError, decryptAesGcm, encryptAesGcm };
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/crypto.ts
|
|
2
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "crypto";
|
|
3
|
+
var ALGORITHM = "aes-256-gcm";
|
|
4
|
+
var IV_LENGTH_BYTES = 12;
|
|
5
|
+
var TAG_LENGTH_BYTES = 16;
|
|
6
|
+
var KEY_LENGTH_BYTES = 32;
|
|
7
|
+
var JAVA_BLANK_PATTERN = /^[\t-\r\u001C-\u001F \u1680\u2000-\u2006\u2008-\u200A\u2028\u2029\u205F\u3000]*$/;
|
|
8
|
+
var BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
9
|
+
var utf8Encoder = new TextEncoder();
|
|
10
|
+
var utf8Decoder = new TextDecoder();
|
|
11
|
+
var CryptoError = class extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "CryptoError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function concatBytes(...parts) {
|
|
18
|
+
let total = 0;
|
|
19
|
+
for (const part of parts) total += part.length;
|
|
20
|
+
const out = new Uint8Array(total);
|
|
21
|
+
let offset = 0;
|
|
22
|
+
for (const part of parts) {
|
|
23
|
+
out.set(part, offset);
|
|
24
|
+
offset += part.length;
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function toBase64(bytes) {
|
|
29
|
+
let binary = "";
|
|
30
|
+
const CHUNK = 32768;
|
|
31
|
+
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
32
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
33
|
+
}
|
|
34
|
+
return btoa(binary);
|
|
35
|
+
}
|
|
36
|
+
function fromBase64(base64) {
|
|
37
|
+
if (base64.length % 4 !== 0 || !BASE64_PATTERN.test(base64)) {
|
|
38
|
+
throw new TypeError("\uC554\uD638\uBB38\uC774 \uC62C\uBC14\uB978 Base64 \uD615\uC2DD\uC774 \uC544\uB2D9\uB2C8\uB2E4.");
|
|
39
|
+
}
|
|
40
|
+
const binary = atob(base64);
|
|
41
|
+
const bytes = new Uint8Array(binary.length);
|
|
42
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
43
|
+
return bytes;
|
|
44
|
+
}
|
|
45
|
+
function deriveKey(key) {
|
|
46
|
+
if (key == null || JAVA_BLANK_PATTERN.test(key)) {
|
|
47
|
+
throw new TypeError("\uC554\uD638\uD654 \uD0A4\uB294 null \uB610\uB294 \uACF5\uBC31\uC77C \uC218 \uC5C6\uC2B5\uB2C8\uB2E4.");
|
|
48
|
+
}
|
|
49
|
+
const keyBytes = utf8Encoder.encode(key);
|
|
50
|
+
if (keyBytes.length !== KEY_LENGTH_BYTES) {
|
|
51
|
+
return createHash("sha256").update(keyBytes).digest();
|
|
52
|
+
}
|
|
53
|
+
return keyBytes;
|
|
54
|
+
}
|
|
55
|
+
function encryptAesGcm(plainText, key) {
|
|
56
|
+
if (plainText == null || JAVA_BLANK_PATTERN.test(plainText)) return plainText;
|
|
57
|
+
const keyBytes = deriveKey(key);
|
|
58
|
+
const iv = randomBytes(IV_LENGTH_BYTES);
|
|
59
|
+
const cipher = createCipheriv(ALGORITHM, keyBytes, iv);
|
|
60
|
+
const encrypted = concatBytes(cipher.update(utf8Encoder.encode(plainText)), cipher.final());
|
|
61
|
+
return toBase64(concatBytes(iv, encrypted, cipher.getAuthTag()));
|
|
62
|
+
}
|
|
63
|
+
function decryptAesGcm(cipherText, key) {
|
|
64
|
+
if (cipherText == null || JAVA_BLANK_PATTERN.test(cipherText)) return cipherText;
|
|
65
|
+
const keyBytes = deriveKey(key);
|
|
66
|
+
const combined = fromBase64(cipherText);
|
|
67
|
+
if (combined.length <= IV_LENGTH_BYTES) {
|
|
68
|
+
throw new TypeError("\uC554\uD638\uBB38 \uAE38\uC774\uAC00 \uC62C\uBC14\uB974\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4. (IV \uD3EC\uD568 \uCD5C\uC18C \uAE38\uC774 \uBBF8\uB2EC)");
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const iv = combined.subarray(0, IV_LENGTH_BYTES);
|
|
72
|
+
const body = combined.subarray(IV_LENGTH_BYTES);
|
|
73
|
+
const data = body.subarray(0, Math.max(0, body.length - TAG_LENGTH_BYTES));
|
|
74
|
+
const tag = body.subarray(Math.max(0, body.length - TAG_LENGTH_BYTES));
|
|
75
|
+
const decipher = createDecipheriv(ALGORITHM, keyBytes, iv, {
|
|
76
|
+
authTagLength: TAG_LENGTH_BYTES
|
|
77
|
+
});
|
|
78
|
+
decipher.setAuthTag(tag);
|
|
79
|
+
const decrypted = concatBytes(decipher.update(data), decipher.final());
|
|
80
|
+
return utf8Decoder.decode(decrypted);
|
|
81
|
+
} catch {
|
|
82
|
+
throw new CryptoError("\uB370\uC774\uD130 \uBCF5\uD638\uD654\uC5D0 \uC2E4\uD328\uD588\uC2B5\uB2C8\uB2E4. (\uD0A4\uAC00 \uB2E4\uB974\uAC70\uB098 \uB370\uC774\uD130\uAC00 \uBCC0\uC870\uB418\uC5C8\uC2B5\uB2C8\uB2E4)");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
CryptoError,
|
|
87
|
+
decryptAesGcm,
|
|
88
|
+
encryptAesGcm
|
|
89
|
+
};
|