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@-IVST"); 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 { const base64url = CryptoJS.enc.Base64url.stringify(input); return this.convertToSpecialChars(base64url); } public static decodeBase64Url(input: string): WordArray { try { const base64url = this.convertFromSpecialChars(input); return CryptoJS.enc.Base64url.parse(base64url); } catch { 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 { const base64 = CryptoJS.enc.Base64.stringify(input); return this.convertToSpecialChars(base64); } public static decodeBase64(input: string): WordArray { try { const base64 = this.convertFromSpecialChars(input); return CryptoJS.enc.Base64.parse(base64); } catch { return CryptoJS.enc.Base64.parse(input); } } 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); } }