logo

@jihyunlab/web-crypto

@jihyunlab/web-crypto는 웹 어플리케이션에서 암호화 기능 구현의 편의성을 높이기 위해 개발되었습니다.
암호화 기능은 Node.js의 Web Crypto API로 구현되며 AES 256 CBC와 AES 256 GCM에 대한 암호화를 제공합니다.

설치

프로젝트 폴더에서 @jihyunlab/web-crypto를 설치합니다.

npm i @jihyunlab/web-crypto

사용 예

간단한 방법으로 데이터를 암호화하고 복호화할 수 있습니다.

import { CIPHER, createCipher } from '@jihyunlab/web-crypto';

const cipher = await createCipher(CIPHER.AES_256_GCM, 'your secret key');

const encrypted = await cipher.encrypt('jihyunlab');
console.log(encrypted); // Uint8Array(37)[51, 174, 20, 84, 12, 141, 173, 206, 249, 11, 59, 112, 88, 223, 163, 211, 128, 234, 102, 116, 16, 224, 175, 45, 46, 52, 186, 141, 15, 243, 9, 120, 64, 27, 135, 169, 65]

const decrypted = await cipher.decrypt(encrypted);
console.log(decrypted); // Uint8Array(9)[106, 105, 104, 121, 117, 110, 108, 97, 98]

Uint8Array 데이터에 대한 암호화 기능을 제공합니다.

const encrypted = await cipher.encrypt(
  new Uint8Array([106, 105, 104, 121, 117, 110, 108, 97, 98])
);
console.log(encrypted); // Uint8Array(37)[185, 95, 254, 103, 109, 250, 109, 50, 8, 218, 251, 74, 215, 108, 74, 86, 177, 82, 144, 154, 156, 120, 128, 169, 112, 236, 153, 23, 253, 164, 238, 159, 236, 17, 85, 26, 75]

const decrypted = await cipher.decrypt(encrypted);
console.log(decrypted); // Uint8Array(9)[106, 105, 104, 121, 117, 110, 108, 97, 98]

암호화 옵션을 설정할 수 있습니다.

const cipher = await createCipher(CIPHER.AES_256_GCM, 'your secret key', {
  salt: 'salt',
  iterations: 256,
});

@jihyunlab/web-buffer

추가적인 데이터 변환을 위해 @jihyunlab/web-buffer를 설치하고 사용할 수 있습니다.

npm i @jihyunlab/web-buffer
import { WebBuffer } from '@jihyunlab/web-buffer';

const buffer = WebBuffer.from(
  new Uint8Array([106, 105, 104, 121, 117, 110, 108, 97, 98]),
  'uint8array' /* hex, base64, base64url, utf8, uint8array */
);

const utf8 = buffer.toString('utf8');
console.log(utf8); // jihyunlab

@jihyunlab/crypto

@jihyunlab/crypto는 @jihyunlab/web-crypto와 동일한 인터페이스를 사용하여 Node.js 어플리케이션에 대한 암호화 기능을 구현합니다. @jihyunlab/web-crypto에서 암호화된 데이터를 Node.js 어플리케이션에서 복호화 하기 위해 @jihyunlab/crypto를 설치하고 사용할 수 있습니다.

import { CIPHER, createCipher } from '@jihyunlab/crypto';

const cipher = await createCipher(CIPHER.AES_256_GCM, 'your secret key');
const encrypted = await cipher.encrypt('jihyunlab');

라이센스

Open source licensed as MIT.

logo

ⓒ 2023-2024 JihyunLab. All rights reserved.

info@jihyunlab.com