Compare commits
14 Commits
6c2399b283
...
4a30758d7f
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a30758d7f | |||
| e93f6c8f41 | |||
| b73d5d55fb | |||
| 4a06fc7e83 | |||
| b9642961ac | |||
| e528f6a67e | |||
| e224455dea | |||
| c7b0655ec2 | |||
| 8c0b19d542 | |||
| b3c9a8f6ee | |||
| 5fe38e1c90 | |||
| 295b914f0f | |||
| 4c4ef662fd | |||
| b9fa6255b7 |
@@ -1,7 +1,7 @@
|
|||||||
import CryptoJS from "crypto-js";
|
import CryptoJS from "crypto-js";
|
||||||
|
|
||||||
const KEY = CryptoJS.enc.Utf8.parse("kr!-Kfp@-fr0nT-CrypT0-Key-STR");
|
const KEY = CryptoJS.enc.Utf8.parse("kr!-Kfp@-fr0nT-CrypT0-Key-STR!!!");
|
||||||
const IV = CryptoJS.enc.Utf8.parse("kr!-Kfp@-IVST");
|
const IV = CryptoJS.enc.Utf8.parse("kr!-Kfp@Dash-IV!");
|
||||||
|
|
||||||
export type WordArray = CryptoJS.lib.WordArray;
|
export type WordArray = CryptoJS.lib.WordArray;
|
||||||
|
|
||||||
@@ -143,17 +143,16 @@ export class Security {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static encodeBase64Url(input: WordArray): string {
|
public static encodeBase64Url(input: WordArray): string {
|
||||||
const base64url = CryptoJS.enc.Base64url.stringify(input);
|
return CryptoJS.enc.Base64url.stringify(input);
|
||||||
return this.convertToSpecialChars(base64url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static decodeBase64Url(input: string): WordArray {
|
public static decodeBase64Url(input: string): WordArray {
|
||||||
try {
|
if (this.hasSpecialChars(input)) {
|
||||||
const base64url = this.convertFromSpecialChars(input);
|
const base64url = this.convertFromSpecialChars(input);
|
||||||
return CryptoJS.enc.Base64url.parse(base64url);
|
return CryptoJS.enc.Base64url.parse(base64url);
|
||||||
} catch {
|
|
||||||
return CryptoJS.enc.Base64url.parse(input);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return CryptoJS.enc.Base64url.parse(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static encodeBase64Utf8(input: string): string {
|
public static encodeBase64Utf8(input: string): string {
|
||||||
@@ -165,17 +164,20 @@ export class Security {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static encodeBase64(input: WordArray): string {
|
public static encodeBase64(input: WordArray): string {
|
||||||
const base64 = CryptoJS.enc.Base64.stringify(input);
|
return CryptoJS.enc.Base64.stringify(input);
|
||||||
return this.convertToSpecialChars(base64);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static decodeBase64(input: string): WordArray {
|
public static decodeBase64(input: string): WordArray {
|
||||||
try {
|
if (this.hasSpecialChars(input)) {
|
||||||
const base64 = this.convertFromSpecialChars(input);
|
const base64 = this.convertFromSpecialChars(input);
|
||||||
return CryptoJS.enc.Base64.parse(base64);
|
return CryptoJS.enc.Base64.parse(base64);
|
||||||
} catch {
|
}
|
||||||
|
|
||||||
return CryptoJS.enc.Base64.parse(input);
|
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 {
|
public static encodeHex(input: WordArray): string {
|
||||||
|
|||||||
@@ -1,71 +1,50 @@
|
|||||||
import { Security } from "@/lib/security";
|
import { Security } from "@/lib/security";
|
||||||
import {clientEnv} from "@/lib/env.client";
|
import { clientEnv } from "@/lib/env.client";
|
||||||
|
|
||||||
type StorageValue = any; // 요청대로 any 유지
|
type StorageValue = any;
|
||||||
|
|
||||||
// --- 내부 헬퍼 함수들 ---
|
const parseStorageValue = (value: string): StorageValue => {
|
||||||
|
|
||||||
const getDecryptedValue = (value: string): StorageValue => {
|
|
||||||
try {
|
|
||||||
if (!value || value.trim() === "") {
|
if (!value || value.trim() === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const decryptValue = clientEnv.ENV ==="prod" ? Security.decryptBase64(value) : value;
|
const rawValue = clientEnv.ENV === "prod" ? Security.decryptBase64(value) : value;
|
||||||
|
if (!rawValue || rawValue.trim() === "") {
|
||||||
if (!decryptValue || decryptValue.trim() === "") {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON.parse(decryptValue);
|
return JSON.parse(rawValue);
|
||||||
} catch (error) {
|
|
||||||
// 손상된 데이터인 경우 null 반환
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getEncryptedValue = (value: StorageValue): string => {
|
const stringifyStorageValue = (value: StorageValue): string => {
|
||||||
const stringifyValue = JSON.stringify(value);
|
const rawValue = JSON.stringify(value);
|
||||||
return clientEnv.ENV ==="prod" ? Security.encryptBase64(stringifyValue) : stringifyValue;
|
return clientEnv.ENV === "prod" ? Security.encryptBase64(rawValue) : rawValue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// --- LocalStorage 함수들 ---
|
|
||||||
|
|
||||||
export const setLocalStorage = (key: string, value: StorageValue): void => {
|
export const setLocalStorage = (key: string, value: StorageValue): void => {
|
||||||
try {
|
try {
|
||||||
const encryptValue = getEncryptedValue(value);
|
localStorage.setItem(key, stringifyStorageValue(value));
|
||||||
localStorage.setItem(key, encryptValue);
|
} catch {}
|
||||||
} catch (error) {
|
|
||||||
// 필요하면 로깅 추가
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getLocalStorage = (key: string): StorageValue => {
|
export const getLocalStorage = (key: string): StorageValue => {
|
||||||
try {
|
try {
|
||||||
const encryptValue: string | null = localStorage.getItem(key);
|
const value = localStorage.getItem(key);
|
||||||
if (encryptValue === null || encryptValue === "") {
|
if (value === null || value === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: StorageValue = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 암호화된 값 복호화 시도
|
return parseStorageValue(value);
|
||||||
value = getDecryptedValue(encryptValue);
|
} catch {
|
||||||
} catch (decryptError) {
|
|
||||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
|
||||||
try {
|
try {
|
||||||
value = JSON.parse(encryptValue);
|
return JSON.parse(value);
|
||||||
} catch (fallbackError) {
|
} catch {
|
||||||
// 완전히 실패한 경우 저장된 데이터 제거
|
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
return value;
|
|
||||||
} catch (error) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -78,43 +57,30 @@ export const clearLocalStorage = (): void => {
|
|||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- SessionStorage 함수들 ---
|
|
||||||
|
|
||||||
// 세션스토리지는 테스트/실제 서버 모두 암호화 없음
|
|
||||||
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
||||||
try {
|
try {
|
||||||
const encryptValue = getEncryptedValue(value);
|
sessionStorage.setItem(key, stringifyStorageValue(value));
|
||||||
sessionStorage.setItem(key, encryptValue);
|
} catch {}
|
||||||
} catch (error) {
|
|
||||||
// 필요하면 로깅 추가
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSessionStorage = (key: string): StorageValue => {
|
export const getSessionStorage = (key: string): StorageValue => {
|
||||||
try {
|
try {
|
||||||
const encryptValue: string | null = sessionStorage.getItem(key);
|
const value = sessionStorage.getItem(key);
|
||||||
if (encryptValue === null || encryptValue === "") {
|
if (value === null || value === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: StorageValue = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 암호화된 값 복호화 시도
|
return parseStorageValue(value);
|
||||||
value = getDecryptedValue(encryptValue);
|
} catch {
|
||||||
} catch (decryptError) {
|
|
||||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
|
||||||
try {
|
try {
|
||||||
value = JSON.parse(encryptValue);
|
return JSON.parse(value);
|
||||||
} catch (fallbackError) {
|
} catch {
|
||||||
// 완전히 실패한 경우 저장된 데이터 제거
|
sessionStorage.removeItem(key);
|
||||||
localStorage.removeItem(key);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
return value;
|
|
||||||
} catch (error) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user