Toss payments 개발자 커뮤니티Tp개커Toss payments 개발자 커뮤니티
Powered by
ybY
Toss payments 개발자 커뮤니티•16mo ago•
4 replies
yb

지급대행 Encryption Javascript 버전

import { compactDecrypt, CompactEncrypt } from 'jose';
import { uuidv4 } from 'lib0/random';

const encrypt = async (target: object, securityKey: string) => {
  // securityKey is a 64 character Hexadecimal string, change it to a byte array
  const key = Buffer.from(securityKey, 'hex');
  const keyInUint8Array = new Uint8Array(key);
  const payload = JSON.stringify(target);
  const payloadAsUint8Array = new TextEncoder().encode(payload);

  const encryption = new CompactEncrypt(payloadAsUint8Array);
  const encrypted = await encryption
    .setProtectedHeader({
      alg: 'dir',
      enc: 'A256GCM',
      iat: new Date().toISOString(),
      nonce: uuidv4(),
    })
    .encrypt(keyInUint8Array);
  return encrypted;
};

const decrypt = async (encrypted: string, securityKey: string) => {
  const key = Buffer.from(securityKey, 'hex');

  const keyInUint8Array = new Uint8Array(key);
  const decryption = await compactDecrypt(encrypted, keyInUint8Array);
  const plainTextInUint8Array = decryption.plaintext;
  // decode the plain text from Uint8Array to string in utf-8
  const plainText = new TextDecoder().decode(plainTextInUint8Array);
  return JSON.parse(plainText) as object;
};

export { encrypt, decrypt };
import { compactDecrypt, CompactEncrypt } from 'jose';
import { uuidv4 } from 'lib0/random';

const encrypt = async (target: object, securityKey: string) => {
  // securityKey is a 64 character Hexadecimal string, change it to a byte array
  const key = Buffer.from(securityKey, 'hex');
  const keyInUint8Array = new Uint8Array(key);
  const payload = JSON.stringify(target);
  const payloadAsUint8Array = new TextEncoder().encode(payload);

  const encryption = new CompactEncrypt(payloadAsUint8Array);
  const encrypted = await encryption
    .setProtectedHeader({
      alg: 'dir',
      enc: 'A256GCM',
      iat: new Date().toISOString(),
      nonce: uuidv4(),
    })
    .encrypt(keyInUint8Array);
  return encrypted;
};

const decrypt = async (encrypted: string, securityKey: string) => {
  const key = Buffer.from(securityKey, 'hex');

  const keyInUint8Array = new Uint8Array(key);
  const decryption = await compactDecrypt(encrypted, keyInUint8Array);
  const plainTextInUint8Array = decryption.plaintext;
  // decode the plain text from Uint8Array to string in utf-8
  const plainText = new TextDecoder().decode(plainTextInUint8Array);
  return JSON.parse(plainText) as object;
};

export { encrypt, decrypt };


Node 환경에서 구현한 간단한 예시입니다 🙂
Toss payments 개발자 커뮤니티 banner
Toss payments 개발자 커뮤니티Join
Toss payments 개발자 커뮤니티입니다. 결제 연동하며 겪는 기술 문의부터, 결제 시장에 대한 다양한 정보까지 얻어가세요~
16,103Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

플러터 3.22 이후 버전 오류
우르릉우우르릉 / ✍┃피드백-개선요청
2y ago
용어사전 - 지급대행 페이지 오타 제보
Hyeonmin ParkHHyeonmin Park / ✍┃피드백-개선요청
4mo ago
지급대행 웹훅에서 payout.created 가 있으면 좋겠습니다.
leo6104Lleo6104 / ✍┃피드백-개선요청
2y ago
API 버전에 관련한 명칭 개선 제안
Minsu KimMMinsu Kim / ✍┃피드백-개선요청
6mo ago