热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jquerybase64.js插件使用

官方文档地址:https:github.comyckartjquery.base64.jsvara123;varb$.base64.btoa(a);conso

官方文档地址:https://github.com/yckart/jquery.base64.js

var a="123";
var b=$.base64.btoa(a);
console.log(a===$.base64.atob(b));

true;

var a="123";
var b=$.base64.atob(a);
console.log(a
===$.base64.btoa(b));

false;

/*!
* jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js
* Makes Base64 en & -decoding simpler as it is.
*
* Based upon: https://gist.github.com/Yaffle/1284012
*
* Copyright (c) 2012 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/02/10
**/
;(function($) {

var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
a256 = '',
r64 = [256],
r256 = [256],
i = 0;

var UTF8 = {

/**
* Encode multi-byte Unicode string into utf-8 multiple single-byte characters
* (BMP / basic multilingual plane only)
*
* Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
*
* @param {String} strUni Unicode string to be encoded as UTF-8
* @returns {String} encoded string
*/
encode: function(strUni) {
// use regular expressions & String.replace callback function for better efficiency
// than procedural approaches
var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
function(c) {
var cc = c.charCodeAt(0);
return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
})
.replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
function(c) {
var cc = c.charCodeAt(0);
return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
});
return strUtf;
},

/**
* Decode utf-8 encoded string back into multi-byte Unicode characters
*
* @param {String} strUtf UTF-8 string to be decoded back to Unicode
* @returns {String} decoded string
*/
decode: function(strUtf) {
// note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
function(c) { // (note parentheses for precence)
var cc &#61; ((c.charCodeAt(0) & 0x0f) <<12) | ((c.charCodeAt(1) & 0x3f) <<6) | (c.charCodeAt(2) & 0x3f);
return String.fromCharCode(cc);
})
.replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
function(c) { // (note parentheses for precence)
var cc &#61; (c.charCodeAt(0) & 0x1f) <<6 | c.charCodeAt(1) & 0x3f;
return String.fromCharCode(cc);
});
return strUni;
}
};

while(i <256) {
var c &#61; String.fromCharCode(i);
a256 &#43;&#61; c;
r256[i] &#61; i;
r64[i] &#61; b64.indexOf(c);
&#43;&#43;i;
}

function code(s, discard, alpha, beta, w1, w2) {
s &#61; String(s);
var buffer &#61; 0,
i &#61; 0,
length &#61; s.length,
result &#61; &#39;&#39;,
bitsInBuffer &#61; 0;

while(i var c &#61; s.charCodeAt(i);
c &#61; c <256 ? alpha[c] : -1;

buffer &#61; (buffer < bitsInBuffer &#43;&#61; w1;

while(bitsInBuffer >&#61; w2) {
bitsInBuffer -&#61; w2;
var tmp &#61; buffer >> bitsInBuffer;
result &#43;&#61; beta.charAt(tmp);
buffer ^&#61; tmp < }
&#43;&#43;i;
}
if(!discard && bitsInBuffer > 0) result &#43;&#61; beta.charAt(buffer <<(w2 - bitsInBuffer));
return result;
}

var Plugin &#61; $.base64 &#61; function(dir, input, encode) {
return input ? Plugin[dir](input, encode) : dir ? null : this;
};

Plugin.btoa &#61; Plugin.encode &#61; function(plain, utf8encode) {
plain &#61; Plugin.raw &#61;&#61;&#61; false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain;
plain &#61; code(plain, false, r256, b64, 8, 6);
return plain &#43; &#39;&#61;&#61;&#61;&#61;&#39;.slice((plain.length % 4) || 4);
};

Plugin.atob &#61; Plugin.decode &#61; function(coded, utf8decode) {
coded &#61; String(coded).split(&#39;&#61;&#39;);
var i &#61; coded.length;
do {--i;
coded[i] &#61; code(coded[i], true, r64, a256, 6, 8);
} while (i > 0);
coded &#61; coded.join(&#39;&#39;);
return Plugin.raw &#61;&#61;&#61; false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded;
};
}(jQuery));

 


转:https://www.cnblogs.com/windseek/p/6433606.html



推荐阅读
author-avatar
手机用户新常态
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有