javascript base64 encode,decode

  • 링크 : https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
  • 요약
    • btoa() : base64_encode
    • atob() : base64_decode
    • 지원브라우저
      • 크롬 : ALL
      • FF : 1.0, 27
      • IE : 10
      • Opera : ALL
      • Safari : ALL
    • 한계
      • javascript가 유니코드 기반이기 때문에 utf-8이 아닌 케릭터 셋으로 된것은 디코드 하면 글자가 깨진다.
      • 벗어난 범위의 글자로 에러가 발생할 수 있다.
      • 브라우저에서 지원 안될 수 있다. 특히 IE
        이경우 관련 누군가 만든 함수가 이미 있으니.. 가져다가 쓰자.

// UTF-8 기반 안전한 Base64 인코딩
const base64Encode = str => {
  const bytes = new TextEncoder().encode(str); // UTF-8 인코딩
  let binary = '';
  bytes.forEach(b => binary += String.fromCharCode(b));
  return btoa(binary);
};

// UTF-8 기반 Base64 디코딩
const base64Decode = b64 => {
  const binary = atob(b64);
  const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
  return new TextDecoder().decode(bytes);
};
댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
[기본형] HTML (with 부트스트랩5.3 , jquery 3.7, vue.js)
유용한 리눅스(LINUX) 명령어
[공지] 기술 게시판
9.28
9.29
9.30
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
10.10
10.11
10.12
10.13
10.15
10.16
10.17
10.18
10.19
10.20
10.21
10.22
10.23
10.24
10.25
10.26
10.27
10.28
10.29
10.30
10.31
11.1