Skip to content

Commit

Permalink
feat: add ezwel seed encoder/decoder method
Browse files Browse the repository at this point in the history
  • Loading branch information
soomtong committed Jan 22, 2025
1 parent c1ffaab commit 474244f
Show file tree
Hide file tree
Showing 4 changed files with 2,951 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/crypto-util/crypto-util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TextEncoder } from 'node:util';
import { CryptoUtil } from './crypto-util';

describe('CryptoUtil', () => {
Expand Down Expand Up @@ -49,4 +50,19 @@ describe('CryptoUtil', () => {
expect(result).toEqual(new TextEncoder().encode('hello world'));
});
});

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

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

const crypto = (globalThis as any).crypto as typeof webcrypto;
Expand Down Expand Up @@ -36,4 +38,24 @@ export namespace CryptoUtil {
export const decodeBase64 = (data: string): Uint8Array => {
return Uint8Array.from(atob(data), (c) => c.charCodeAt(0));
};

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();
};

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, '');
};
}
2 changes: 2 additions & 0 deletions src/crypto-util/ezwel-seed.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CryptoJS: any;
export function buildHexString(text: string): string;
Loading

0 comments on commit 474244f

Please sign in to comment.