224 lines
6.6 KiB
TypeScript
224 lines
6.6 KiB
TypeScript
import CryptoJS from "crypto-js";
|
||
|
||
const KEY = CryptoJS.enc.Utf8.parse("kr!-Kfp@-fr0nT-CrypT0-Key-STR!!!");
|
||
const IV = CryptoJS.enc.Utf8.parse("kr!-Kfp@Dash-IV!");
|
||
const VALID_AES_KEY_BYTES = [16, 24, 32];
|
||
const AES_IV_BYTES = 16;
|
||
|
||
const validateAesConfig = () => {
|
||
const validKey = VALID_AES_KEY_BYTES.includes(KEY.sigBytes);
|
||
const validIv = IV.sigBytes === AES_IV_BYTES;
|
||
|
||
if (validKey && validIv) return;
|
||
|
||
const message = [
|
||
"AES 설정이 올바르지 않습니다.",
|
||
`KEY: ${KEY.sigBytes} bytes (허용: ${VALID_AES_KEY_BYTES.join(", ")})`,
|
||
`IV: ${IV.sigBytes} bytes (필수: ${AES_IV_BYTES})`,
|
||
].join("\n");
|
||
|
||
if (typeof window !== "undefined" && process.env.NEXT_PUBLIC_ENV === "local") {
|
||
const w = window as Window & { __kfpaCryptoConfigAlerted?: boolean };
|
||
if (!w.__kfpaCryptoConfigAlerted) {
|
||
w.__kfpaCryptoConfigAlerted = true;
|
||
alert(message);
|
||
}
|
||
}
|
||
};
|
||
|
||
validateAesConfig();
|
||
|
||
export type WordArray = CryptoJS.lib.WordArray;
|
||
|
||
export class Security {
|
||
public static encryptBase64UrlObject(input: unknown): string {
|
||
const data = JSON.stringify(input);
|
||
return this.encryptBase64Url(data);
|
||
}
|
||
|
||
public static decryptBase64UrlObject(input: string): unknown {
|
||
const data = this.decryptBase64Url(input);
|
||
return JSON.parse(data);
|
||
}
|
||
|
||
public static encryptBase64Object(input: unknown): string {
|
||
const data = JSON.stringify(input);
|
||
return this.encryptBase64(data);
|
||
}
|
||
|
||
public static decryptBase64Object(input: string): unknown {
|
||
const data = this.decryptBase64(input);
|
||
return JSON.parse(data);
|
||
}
|
||
|
||
public static encryptBase64Url(input: string): string {
|
||
const data = this.decodeUtf8(input);
|
||
const params = this.encrypt(data);
|
||
return this.encodeBase64Url(params);
|
||
}
|
||
|
||
public static decryptBase64Url(input: string): string {
|
||
const data = this.decodeBase64Url(input);
|
||
const decryptedData = this.decrypt(data);
|
||
return this.encodeUtf8(decryptedData);
|
||
}
|
||
|
||
public static encryptBase64(input: string): string {
|
||
const data = this.decodeUtf8(input);
|
||
const params = this.encrypt(data);
|
||
return this.encodeBase64(params);
|
||
}
|
||
public static decryptBase64(input: string): string {
|
||
try {
|
||
const data = this.decodeBase64(input);
|
||
const decryptedData = this.decrypt(data);
|
||
return this.encodeUtf8(decryptedData);
|
||
} catch (error) {
|
||
console.error("SecurityFunction.decryptBase64 오류:", {
|
||
input: input?.substring(0, 50) + "...", // 처음 50자만 로그
|
||
error: error instanceof Error ? error.message : String(error),
|
||
});
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
public static encrypt(input: WordArray): WordArray {
|
||
const params = CryptoJS.AES.encrypt(input, KEY, {
|
||
iv: IV,
|
||
padding: CryptoJS.pad.Pkcs7,
|
||
mode: CryptoJS.mode.CBC,
|
||
});
|
||
return params.ciphertext;
|
||
}
|
||
|
||
public static decrypt(input: WordArray): WordArray {
|
||
const params = CryptoJS.lib.CipherParams.create({
|
||
ciphertext: input,
|
||
});
|
||
return CryptoJS.AES.decrypt(params, KEY, {
|
||
iv: IV,
|
||
padding: CryptoJS.pad.Pkcs7,
|
||
mode: CryptoJS.mode.CBC,
|
||
});
|
||
}
|
||
|
||
public static convertUint8ArrayToWordArray(input: Uint8Array): WordArray {
|
||
const words: number[] = [];
|
||
let i = 0;
|
||
while (i < input.length) {
|
||
const word =
|
||
(input[i++] << 24) |
|
||
(input[i++] << 16) |
|
||
(input[i++] << 8) |
|
||
input[i++];
|
||
words.push(word);
|
||
}
|
||
return CryptoJS.lib.WordArray.create(words, words.length * 4);
|
||
}
|
||
|
||
public static convertWordArrayToUint8Array(input: WordArray): Uint8Array {
|
||
const array = new Uint8Array(input.words.length << 2);
|
||
for (let i = 0, j = 0; i < input.words.length; i++) {
|
||
const word = input.words[i];
|
||
array[j++] = word >> 24;
|
||
array[j++] = (word >> 16) & 0xff;
|
||
array[j++] = (word >> 8) & 0xff;
|
||
array[j++] = word & 0xff;
|
||
}
|
||
return array;
|
||
}
|
||
|
||
public static computeHmacSha256Hex(input: WordArray, key: WordArray): string {
|
||
return this.encodeHex(this.computeHmacSha256(input, key));
|
||
}
|
||
|
||
public static computeHmacSha256(input: WordArray, key: WordArray): WordArray {
|
||
return CryptoJS.HmacSHA256(input, key);
|
||
}
|
||
|
||
public static computeSha256Hex(input: WordArray): string {
|
||
return this.encodeHex(this.computeSha256(input));
|
||
}
|
||
|
||
public static computeSha256(input: WordArray): WordArray {
|
||
return CryptoJS.SHA256(input);
|
||
}
|
||
|
||
private static convertToSpecialChars(base64: string): string {
|
||
return Array.from(base64)
|
||
.map((char) => char.charCodeAt(0).toString(2).padStart(16, "0"))
|
||
.map((binary) => binary.replace(/0/g, "\u200b").replace(/1/g, "\u200c"))
|
||
.join("ㅤ");
|
||
}
|
||
|
||
private static convertFromSpecialChars(input: string): string {
|
||
return input
|
||
.split("ㅤ")
|
||
.map((bin) => bin.replace(/\u200b/g, "0").replace(/\u200c/g, "1"))
|
||
.map((binary) => String.fromCharCode(parseInt(binary, 2)))
|
||
.join("");
|
||
}
|
||
|
||
public static encodeBase64UrlUtf8(input: string): string {
|
||
return this.encodeBase64Url(this.decodeUtf8(input));
|
||
}
|
||
|
||
public static decodeBase64UrlUtf8(input: string): string {
|
||
return this.encodeUtf8(this.decodeBase64Url(input));
|
||
}
|
||
|
||
public static encodeBase64Url(input: WordArray): string {
|
||
return CryptoJS.enc.Base64url.stringify(input);
|
||
}
|
||
|
||
public static decodeBase64Url(input: string): WordArray {
|
||
if (this.hasSpecialChars(input)) {
|
||
const base64url = this.convertFromSpecialChars(input);
|
||
return CryptoJS.enc.Base64url.parse(base64url);
|
||
}
|
||
|
||
return CryptoJS.enc.Base64url.parse(input);
|
||
}
|
||
|
||
public static encodeBase64Utf8(input: string): string {
|
||
return this.encodeBase64(this.decodeUtf8(input));
|
||
}
|
||
|
||
public static decodeBase64Utf8(input: string): string {
|
||
return this.encodeUtf8(this.decodeBase64(input));
|
||
}
|
||
|
||
public static encodeBase64(input: WordArray): string {
|
||
return CryptoJS.enc.Base64.stringify(input);
|
||
}
|
||
|
||
public static decodeBase64(input: string): WordArray {
|
||
if (this.hasSpecialChars(input)) {
|
||
const base64 = this.convertFromSpecialChars(input);
|
||
return CryptoJS.enc.Base64.parse(base64);
|
||
}
|
||
|
||
return CryptoJS.enc.Base64.parse(input);
|
||
}
|
||
|
||
private static hasSpecialChars(input: string): boolean {
|
||
return input.includes("\u200b") || input.includes("\u200c");
|
||
}
|
||
|
||
public static encodeHex(input: WordArray): string {
|
||
return CryptoJS.enc.Hex.stringify(input);
|
||
}
|
||
|
||
public static decodeHex(input: string): WordArray {
|
||
return CryptoJS.enc.Hex.parse(input);
|
||
}
|
||
|
||
public static encodeUtf8(input: WordArray): string {
|
||
return CryptoJS.enc.Utf8.stringify(input);
|
||
}
|
||
|
||
public static decodeUtf8(input: string): WordArray {
|
||
return CryptoJS.enc.Utf8.parse(input);
|
||
}
|
||
}
|