Frontend2024년 12월 21일1분 읽기

Web Crypto API — 브라우저에서 안전한 암호화

YS
YoungSam
조회 1861

Web Crypto API란

브라우저에서 네이티브로 제공하는 암호화 API입니다. 순수 JavaScript 라이브러리보다 안전하고 빠릅니다.

해싱

async function sha256(message: string) {
  const msgBuffer = new TextEncoder().encode(message);
  const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  return hashArray.map(b => b.toString(16).padStart(2, "0")).join("");
}

AES 암호화

const key = await crypto.subtle.generateKey(
  { name: "AES-GCM", length: 256 },
  true, ["encrypt", "decrypt"]
);
Web CryptoSecurityBrowser API

댓글 0

아직 댓글이 없습니다.