Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/crypto-util/crypto-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ describe('decodeBase64', () => {
});

describe('ezwelSeed', () => {
const decrypted = '{"code": "hello world", "message": "안녕하세요"}';
const encrypted = 'LUPvDxmJToRCZcl56a7j+b1X1NV+6PMiBLm7SkLALDqyIfqCsHla0jkDuzoIn60GV5BA1t22DaHs1L32r4uw+A==';
const seedKey = 'string_x_sixteen';

it('should return encrypted string', () => {
const data = '{"code": "hello world", "message": "안녕하세요"}';
const r1 = encodeSeedString(data, { seedKey: 'string_x_sixteen' });
expect(r1).toBe('LUPvDxmJToRCZcl56a7j+b1X1NV+6PMiBLm7SkLALDqyIfqCsHla0jkDuzoIn60G52VI68uIO51li9JAFsksAA==');
const r1 = encodeSeedString(decrypted, { seedKey });
expect(r1).toEqual(encrypted);
});

it('should return decrypted string', () => {
const data = 'LUPvDxmJToRCZcl56a7j+b1X1NV+6PMiBLm7SkLALDqyIfqCsHla0jkDuzoIn60G52VI68uIO51li9JAFsksAA==';
const r1 = decodeSeedString(data, { seedKey: 'string_x_sixteen' });
expect(r1.trim()).toBe('{"code": "hello world", "message": "안녕하세요"}');
expect(JSON.parse(r1)).toEqual({ code: 'hello world', message: '안녕하세요' });
const r1 = decodeSeedString(encrypted, { seedKey });
expect(r1).toEqual(decrypted);
expect(JSON.parse(r1)).toEqual(JSON.parse(decrypted));
});
});
});
20 changes: 5 additions & 15 deletions src/crypto-util/crypto-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CryptoJS, buildHexString } from './ezwel-seed';
import { EzwelCrypto } from './ezwel-seed';
import type { webcrypto } from 'crypto';

const crypto = (globalThis as any).crypto as typeof webcrypto;
const ezwelCrypto = new EzwelCrypto();

export const sha256 = async (data: string | Uint8Array): Promise<Uint8Array> => {
if (typeof data === 'string') {
Expand Down Expand Up @@ -38,21 +39,10 @@ export const decodeBase64 = (data: string): Uint8Array => {
};

export const encodeSeedString = (data: string, { seedKey }: { seedKey: string }): string => {
const encoded = CryptoJS.enc.Utf8.parse(data);
const key = CryptoJS.enc.Hex.parse(buildHexString(seedKey));
const encrypted = CryptoJS.SEED.encrypt(encoded, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.ZeroPadding,
});
return encrypted.toString();
return encodeBase64(ezwelCrypto.encrypt(data, seedKey));
};

export const decodeSeedString = (hash: string, { seedKey }: { seedKey: string }): string => {
const key = CryptoJS.enc.Hex.parse(buildHexString(seedKey));
const decrypted = CryptoJS.SEED.decrypt(hash, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.ZeroPadding,
});
const decoded = CryptoJS.enc.Utf8.stringify(decrypted);
return decoded.replaceAll(/\x00/g, '');
const decrypted = ezwelCrypto.decrypt(decodeBase64(hash), seedKey);
return new TextDecoder().decode(decrypted);
};
2 changes: 0 additions & 2 deletions src/crypto-util/ezwel-seed.d.ts

This file was deleted.

Loading