📂 JAVASCRIPT/그외
[ 자바스크립트 ] window.atob() & window.btoa() 🕔 2016. 1. 29. 07:14
정확한 사용 용도는 모르지만, 일단 퍼놓기... ─.─;;
[ 네이버 지식백과 ] encode : 암호로 바꾸다. 부호화하다. 입력된 데이터를 컴퓨터 속에서 사용하는 코드로 변환하다. decode : (암호를) 해독하다. 코드(부호)화된 데이터를 원래 형태로 변환하다. 어떤 규칙에 따라 부호로 변환된 데이터를 원래 형태로 되돌리다.
<script type="text/javascript">
/*
* window.atob() 함수가 적용되지 않을 때 추가할 내용 ( ie 9 이하 )
* 출처 : http://www.webtoolkit.info/javascript-base64.html
*/
( function (){
if ( ! window.atob ){
window.atob = function ( b64text ){ return Base64.decode( b64text ); };
window.btoa = function ( utf8text ){ return Base64.encode( utf8text ); };
var Base64 = {
keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode : function ( input ){
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var x = 0;
input = Base64._utf8_encode( input );
while ( x < input.length ){
chr1 = input.charCodeAt( x++ );
chr2 = input.charCodeAt( x++ );
chr3 = input.charCodeAt( x++ );
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if ( isNaN(chr2) ) enc3 = enc4 = 64;
else if ( isNaN(chr3) ) enc4 = 64;
output = output + this.keyStr.charAt( enc1 ) + this.keyStr.charAt( enc2 ) + this.keyStr.charAt( enc3 ) +
this.keyStr.charAt( enc4 );
}
return output;
},
decode : function ( input ){
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var x = 0;
input = input.replace( /[^A-Za-z0-9\+\/\=]/g , "" );
while ( x < input.length ){
enc1 = this.keyStr.indexOf( input.charAt(x++) );
enc2 = this.keyStr.indexOf( input.charAt(x++) );
enc3 = this.keyStr.indexOf( input.charAt(x++) );
enc4 = this.keyStr.indexOf( input.charAt(x++) );
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode( chr1 );
if ( enc3 != 64 ) output = output + String.fromCharCode( chr2 );
if ( enc4 != 64 ) output = output + String.fromCharCode( chr3 );
}
output = Base64._utf8_decode( output );
return output;
},
_utf8_encode : function ( string ){
string = string.replace( /\r\n/g , "\n" );
var utftext = "";
for ( var n = 0; n < string.length; n++ ){
var c = string.charCodeAt( n );
if ( c < 128 ) utftext += String.fromCharCode( c );
else if ( (c > 127) && (c < 2048) ){
utftext += String.fromCharCode( (c >> 6) | 192 );
utftext += String.fromCharCode( (c & 63) | 128 );
}
else {
utftext += String.fromCharCode( (c >> 12) | 224 );
utftext += String.fromCharCode( ( (c >> 6) & 63 ) | 128 );
utftext += String.fromCharCode( (c & 63) | 128 );
}
}
return utftext;
},
_utf8_decode : function ( utftext ){
var string = "";
var x = 0;
var c = c1 = c2 = 0;
while ( x < utftext.length ){
c = utftext.charCodeAt( x );
if ( c < 128 ) {
string += String.fromCharCode( c );
x++;
}
else if ( (c > 191) && (c < 224) ){
c2 = utftext.charCodeAt( x + 1 );
string += String.fromCharCode( ( (c & 31) << 6 ) | (c2 & 63) );
x += 2;
}
else {
c2 = utftext.charCodeAt( x + 1 );
c3 = utftext.charCodeAt( x + 2 );
string += String.fromCharCode( ( (c & 15) << 12 ) | ( (c2 & 63) << 6 ) | (c3 & 63) );
x += 3;
}
}
return string;
}
}
}
}() );
</script>
enc :
dec :
<button onclick="testing()"> 클릭하세요 </button><br /> enc : <span id="demo1"> </span><br /> dec : <span id="demo2"> </span> <script type="text/javascript"> function testing (){ var string = "Hi, everyone!"; var enc = btoa( string ); var dec = atob( enc ); demo1.innerHTML = enc; demo2.innerHTML = dec; } </script>
'JAVASCRIPT > 그외' 카테고리의 다른 글
[ 자바스크립트 ] removeChild() & childNodes() (0) | 2016.03.20 |
---|---|
[ 자바스크립트 ] Date.prototype.toISOString() (0) | 2016.01.29 |
[ 자바스크립트 ] 오페라 브라우저의 버전 체크용 함수 (0) | 2015.08.31 |
userAgent의 값을 알 수 있는 영문사이트 5곳. (0) | 2015.08.31 |
[ 자바스크립트 ] 태그의 title 속성 (0) | 2015.08.09 |