yb
yb7mo ago

지급대행 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 환경에서 구현한 간단한 예시입니다 🙂
5 Replies
토스페이먼츠 BOT
⏳ 잠시만 기다려주세요! 곧 답변드리겠습니다
오류 문의일 경우 아래 정보를 미리 전달해주시면, 빠른 답변에 도움이 됩니다.
- 주문번호(orderId) : - 문의 내용 :
(img를 함께 첨부해주시면 도움이됩니다)
* 계약관련 내용은 1544-7772로 문의주세요. * 주말/공휴일에는 답변이 늦을 수 있어요.
yb
ybOP7mo ago
지급대행하기 | 토스페이먼츠 개발자센터
지급대행은 오픈마켓을 운영하는 사업자 대신 토스페이먼츠가 셀러(입점 판매자)에게 대금 지급을 대행 해주는 서비스예요.
yb
ybOP7mo ago
이곳에 JS가 없어서 따로 구현했습니다!
Minsu Kim
Minsu Kim7mo ago
공유해 주셔서 감사합니다. ☺️
토스페이먼츠 BOT
❤️ 기술문의 경험이 어떠셨나요?!
간단히 코멘트 남겨주세요! 제품 발전에 큰 힘이 됩니다.

Did you find this page helpful?