You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
524
static/js/common.js
Executable file
524
static/js/common.js
Executable file
@ -0,0 +1,524 @@
|
||||
var WST = WST || {};
|
||||
WST.v = 'a2.0.2';
|
||||
WST.checkBrowser = function(){
|
||||
return {
|
||||
mozilla : /firefox/.test(navigator.userAgent.toLowerCase()),
|
||||
webkit : /webkit/.test(navigator.userAgent.toLowerCase()),
|
||||
opera : /opera/.test(navigator.userAgent.toLowerCase()),
|
||||
msie : /msie/.test(navigator.userAgent.toLowerCase())
|
||||
}
|
||||
}
|
||||
WST.pageHeight = function(){
|
||||
if(WST.checkBrowser().msie){
|
||||
return document.compatMode == "CSS1Compat"? document.documentElement.clientHeight :
|
||||
document.body.clientHeight;
|
||||
}else{
|
||||
return self.innerHeight;
|
||||
}
|
||||
};
|
||||
//返回当前页面宽度
|
||||
WST.pageWidth = function(){
|
||||
if(WST.checkBrowser().msie){
|
||||
return document.compatMode == "CSS1Compat"? document.documentElement.clientWidth :
|
||||
document.body.clientWidth;
|
||||
}else{
|
||||
return self.innerWidth;
|
||||
}
|
||||
};
|
||||
WST.TreeSelector = function(item,data,rootId,defaultValue){
|
||||
this._data = data;
|
||||
this._item = item;
|
||||
this._rootId = rootId;
|
||||
if(defaultValue)this.defaultValue = defaultValue;
|
||||
}
|
||||
WST.TreeSelector.prototype.createTree = function(){
|
||||
var len =this._data.length;
|
||||
for( var i= 0;i<len;i++){
|
||||
if ( this._data[i].pid == this._rootId){
|
||||
|
||||
this._item.options.add(new Option(" "+this._data[i].text,this._data[i].id));
|
||||
for(var j=0;j<len;j++){
|
||||
this.createSubOption(len,this._data[i],this._data[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(this.defaultValue)this._item.value = this.defaultValue;
|
||||
}
|
||||
|
||||
WST.TreeSelector.prototype.createSubOption = function(len,current,next){
|
||||
var blank = "..";
|
||||
if ( next.pid == current.id){
|
||||
intLevel =0;
|
||||
var intlvl =this.getLevel(this._data,this._rootId,current);
|
||||
for(a=0;a<intlvl;a++)
|
||||
blank += "..";
|
||||
blank += "├-";
|
||||
this._item.options.add(new Option(blank + next.text,next.id));
|
||||
for(var j=0;j<len;j++){
|
||||
this.createSubOption(len,next,this._data[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
WST.TreeSelector.prototype.getLevel = function(datasources,topId,currentitem){
|
||||
|
||||
var pid =currentitem.pid;
|
||||
if( pid !=topId)
|
||||
{
|
||||
for(var i =0 ;i<datasources.length;i++)
|
||||
{
|
||||
if( datasources[i].id == pid)
|
||||
{
|
||||
intLevel ++;
|
||||
this.getLevel(datasources,topId,datasources[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return intLevel;
|
||||
}
|
||||
//多选下拉框移动元素
|
||||
WST.multSelect = function(opts){
|
||||
var e1 = document.getElementById(opts.left);
|
||||
var e2 = document.getElementById(opts.right);
|
||||
for(var i=0;i<e1.options.length;i++){
|
||||
if(e1.options[i].selected){
|
||||
var e = e1.options[i];
|
||||
e2.options.add(new Option(e.text, e.value));
|
||||
e1.remove(i);
|
||||
i=i-1
|
||||
}
|
||||
}
|
||||
document.getElementById(opts.val).value=getValue(document.getElementById(opts.vtarget));
|
||||
|
||||
function getValue(geto){
|
||||
var ids = [];
|
||||
for(var i=0;i<geto.options.length;i++){
|
||||
ids.push(geto.options[i].value);
|
||||
}
|
||||
return ids.join(',');
|
||||
}
|
||||
}
|
||||
// 只能輸入數字,且第一數字不能為0
|
||||
WST.digitalOnly = function(obj) {
|
||||
// 先把非数字的都替换掉
|
||||
obj.value=obj.value.replace(/\D/g, "");
|
||||
}
|
||||
/**
|
||||
* 获取版本
|
||||
*/
|
||||
WST.getWSTMARTVersion = function(url){
|
||||
$.post(url,{},function(data,textStatus){
|
||||
var json = {};
|
||||
try{
|
||||
if(typeof(data )=="object"){
|
||||
json = data;
|
||||
}else{
|
||||
json = eval("("+data+")");
|
||||
}
|
||||
}catch(e){}
|
||||
if(json){
|
||||
if(json.version && json.version!='same'){
|
||||
$('.wstmart-version-tips').show();
|
||||
$('#wstmart_version').html(json.version);
|
||||
$('#wstmart_down').attr('href',json.downloadUrl);
|
||||
}
|
||||
if(json.accredit=='no'){
|
||||
$('.wstmart-accredit-tips').show();
|
||||
}
|
||||
if(json.licenseStatus)$('#licenseStatus').html(json.licenseStatus);
|
||||
}
|
||||
});
|
||||
}
|
||||
/********************
|
||||
* 取窗口滚动条高度
|
||||
******************/
|
||||
WST.getScrollTop = function()
|
||||
{
|
||||
var scrollTop=0;
|
||||
if(document.documentElement&&document.documentElement.scrollTop)
|
||||
{
|
||||
scrollTop=document.documentElement.scrollTop;
|
||||
}
|
||||
else if(document.body)
|
||||
{
|
||||
scrollTop=document.body.scrollTop;
|
||||
}
|
||||
return scrollTop;
|
||||
}
|
||||
|
||||
/********************
|
||||
* 取文档内容实际高度
|
||||
*******************/
|
||||
WST.getScrollHeight = function()
|
||||
{
|
||||
return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
|
||||
}
|
||||
|
||||
//只能輸入數字
|
||||
WST.isNumberKey = function(evt){
|
||||
var charCode = (evt.which) ? evt.which : event.keyCode;
|
||||
if (charCode > 31 && (charCode < 48 || charCode > 57)){
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//只能輸入數字和小數點
|
||||
WST.isNumberdoteKey = function(evt){
|
||||
var e = evt || window.event;
|
||||
var srcElement = e.srcElement || e.target;
|
||||
|
||||
var charCode = (evt.which) ? evt.which : event.keyCode;
|
||||
if (charCode > 31 && ((charCode < 48 || charCode > 57) && charCode!=46)){
|
||||
return false;
|
||||
}else{
|
||||
if(charCode==46){
|
||||
var s = srcElement.value;
|
||||
if(s.length==0 || s.indexOf(".")!=-1){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//只能輸入數字和字母
|
||||
WST.isNumberCharKey = function(evt){
|
||||
var e = evt || window.event;
|
||||
var srcElement = e.srcElement || e.target;
|
||||
var charCode = (evt.which) ? evt.which : event.keyCode;
|
||||
|
||||
if((charCode>=48 && charCode<=57) || (charCode>=65 && charCode<=90) || (charCode>=97 && charCode<=122) || charCode==8){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
WST.isChinese = function(obj,isReplace){
|
||||
var pattern = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/i
|
||||
if(pattern.test(obj.value)){
|
||||
if(isReplace)obj.value=obj.value.replace(/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/ig,"");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Number.prototype.toFixed = function(exponent){
|
||||
return parseInt(this * Math.pow(10, exponent)+0.5 )/Math.pow(10,exponent);
|
||||
}
|
||||
|
||||
//用户名判断 (可输入"_",".","@", 数字,字母)
|
||||
WST.isUserName = function(evt){
|
||||
var evt = evt || window.event;
|
||||
var charCode = (evt.which) ? evt.which : evt.keyCode;
|
||||
if((charCode==95 || charCode==46 || charCode==64) || (charCode>=48 && charCode<=57) || (charCode>=65 && charCode<=90) || (charCode>=97 && charCode<=122) || charCode==8){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
WST.isEmail =function(v){
|
||||
var tel = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");
|
||||
return(tel.test(v));
|
||||
}
|
||||
//判断是否电话
|
||||
WST.isTel = function(v){
|
||||
var tel = new RegExp("^[[0-9]{3}-|\[0-9]{4}-]?(\[0-9]{8}|[0-9]{7})?$");
|
||||
return(tel.test(v));
|
||||
}
|
||||
WST.isPhone = function(v){
|
||||
var tel = new RegExp("^[1][0-9]{10}$");
|
||||
return(tel.test(v));
|
||||
}
|
||||
//判断url
|
||||
WST.isUrl = function(str){
|
||||
if(str==null||str=="") return false;
|
||||
var result=str.match(/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\"])*$/);
|
||||
if(result==null)return false;
|
||||
return true;
|
||||
}
|
||||
//比较时间差
|
||||
WST.getTimeDiff = function(startTime,endTime,diffType){
|
||||
//将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
|
||||
startTime = startTime.replace(/-/g, "/");
|
||||
endTime = endTime.replace(/-/g, "/");
|
||||
//将计算间隔类性字符转换为小写
|
||||
diffType = diffType.toLowerCase();
|
||||
var sTime = new Date(startTime); //开始时间
|
||||
var eTime = new Date(endTime); //结束时间
|
||||
//作为除数的数字
|
||||
var divNum = 1;
|
||||
switch (diffType) {
|
||||
case "second":
|
||||
divNum = 1000;
|
||||
break;
|
||||
case "minute":
|
||||
divNum = 1000 * 60;
|
||||
break;
|
||||
case "hour":
|
||||
divNum = 1000 * 3600;
|
||||
break;
|
||||
case "day":
|
||||
divNum = 1000 * 3600 * 24;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
|
||||
}
|
||||
/**
|
||||
* 截取字符串
|
||||
*/
|
||||
WST.cutStr = function (str,len)
|
||||
{
|
||||
if(!str || str=='')return '';
|
||||
var strlen = 0;
|
||||
var s = "";
|
||||
for(var i = 0;i < str.length;i++)
|
||||
{
|
||||
if(strlen >= len){
|
||||
return s + "...";
|
||||
}
|
||||
if(str.charCodeAt(i) > 128)
|
||||
strlen += 2;
|
||||
else
|
||||
strlen++;
|
||||
s += str.charAt(i);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
WST.checkChks = function(obj,cobj){
|
||||
$(cobj).each(function(){
|
||||
$(this)[0].checked = obj.checked;
|
||||
})
|
||||
}
|
||||
WST.getChks = function(obj){
|
||||
var ids = [];
|
||||
$(obj).each(function(){
|
||||
if($(this)[0].checked)ids.push($(this).val());
|
||||
});
|
||||
return ids;
|
||||
}
|
||||
WST.showHide = function(t,str){
|
||||
var s = str.split(',');
|
||||
if(t){
|
||||
for(var i=0;i<s.length;i++){
|
||||
$(s[i]).show();
|
||||
}
|
||||
}else{
|
||||
for(var i=0;i<s.length;i++){
|
||||
$(s[i]).hide();
|
||||
}
|
||||
}
|
||||
s = null;
|
||||
}
|
||||
WST.blank = function(str,defaultVal){
|
||||
if(str=='0000-00-00')str = '';
|
||||
if(str=='0000-00-00 00:00:00')str = '';
|
||||
if(!str)str = '';
|
||||
if(typeof(str)=='null')str = '';
|
||||
if(typeof(str)=='undefined')str = '';
|
||||
if(str=='' && defaultVal)str = defaultVal;
|
||||
return str;
|
||||
}
|
||||
WST.limitDecimal = function(obj,len){
|
||||
var s = obj.value;
|
||||
if(s.indexOf(".")>-1){
|
||||
if((s.length - s.indexOf(".")-1)>len){
|
||||
obj.value = s.substring(0,s.indexOf(".")+len+1);
|
||||
}
|
||||
}
|
||||
s = null;
|
||||
}
|
||||
WST.getParams = function(obj){
|
||||
var params = {};
|
||||
var chk = {},s;
|
||||
$(obj).each(function(){
|
||||
if($(this)[0].type=='hidden' || $(this)[0].type=='number' || $(this)[0].type=='tel' || $(this)[0].type=='password' || $(this)[0].type=='select-one' || $(this)[0].type=='textarea' || $(this)[0].type=='text'){
|
||||
params[$(this).attr('id')] = $.trim($(this).val());
|
||||
}else if($(this)[0].type=='radio'){
|
||||
if($(this).attr('name')){
|
||||
params[$(this).attr('name')] = $('input[name='+$(this).attr('name')+']:checked').val();
|
||||
}
|
||||
}else if($(this)[0].type=='checkbox'){
|
||||
if($(this).attr('name') && !chk[$(this).attr('name')]){
|
||||
s = [];
|
||||
chk[$(this).attr('name')] = 1;
|
||||
$('input[name='+$(this).attr('name')+']:checked').each(function(){
|
||||
s.push($(this).val());
|
||||
});
|
||||
params[$(this).attr('name')] = s.join(',');
|
||||
}
|
||||
}
|
||||
});
|
||||
chk=null,s=null;
|
||||
return params;
|
||||
}
|
||||
WST.setValue = function(name, value){
|
||||
var first = name.substr(0,1), input, i = 0, val;
|
||||
if("#" === first || "." === first){
|
||||
input = $(name);
|
||||
} else {
|
||||
input = $("[name='" + name + "']");
|
||||
}
|
||||
|
||||
if(input.eq(0).is(":radio")) { //单选按钮
|
||||
input.filter("[value='" + value + "']").each(function(){this.checked = true});
|
||||
} else if(input.eq(0).is(":checkbox")) { //复选框
|
||||
if(!$.isArray(value)){
|
||||
val = new Array();
|
||||
val[0] = value;
|
||||
} else {
|
||||
val = value;
|
||||
}
|
||||
for(i = 0, len = val.length; i < len; i++){
|
||||
input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
|
||||
}
|
||||
} else { //其他表单选项直接设置值
|
||||
input.val(value);
|
||||
}
|
||||
}
|
||||
WST.setValues = function(obj){
|
||||
var input,value,val;
|
||||
for(var key in obj){
|
||||
if($('#'+key)[0]){
|
||||
WST.setValue('#'+key,obj[key]);
|
||||
}else if($("[name='" + key + "']")[0]){
|
||||
WST.setValue(key,obj[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$(function(){
|
||||
/**
|
||||
* 获取WSTMart基础配置
|
||||
* @type {object}
|
||||
*/
|
||||
WST.conf = window.conf;
|
||||
/* 基础对象检测 */
|
||||
WST.conf || $.error("WSTMart基础配置没有正确加载!");
|
||||
if(WST.conf.ROUTES)WST.conf.ROUTES = eval("("+WST.conf.ROUTES+")");
|
||||
/**
|
||||
* 解析URL
|
||||
* @param {string} url 被解析的URL
|
||||
* @return {object} 解析后的数据
|
||||
*/
|
||||
WST.parse_url = function(url){
|
||||
var parse = url.match(/^(?:([a-z]+):\/\/)?([\w-]+(?:\.[\w-]+)+)?(?::(\d+))?([\w-\/]+)?(?:\?((?:\w+=[^#&=\/]*)?(?:&\w+=[^#&=\/]*)*))?(?:#([\w-]+))?$/i);
|
||||
parse || $.error("url格式不正确!");
|
||||
return {
|
||||
"scheme" : parse[1],
|
||||
"host" : parse[2],
|
||||
"port" : parse[3],
|
||||
"path" : parse[4],
|
||||
"query" : parse[5],
|
||||
"fragment" : parse[6]
|
||||
};
|
||||
}
|
||||
|
||||
WST.parse_str = function(str){
|
||||
var value = str.split("&"), vars = {}, param;
|
||||
for(var i=0;i<value.length;i++){
|
||||
param = value[i].split("=");
|
||||
vars[param[0]] = param[1];
|
||||
}
|
||||
return vars;
|
||||
}
|
||||
WST.initU = function(url,vars){
|
||||
if(typeof vars === "string"){
|
||||
vars = this.parse_str(vars);
|
||||
}
|
||||
var newUrl = WST.conf.ROUTES[url];
|
||||
var urlparams = newUrl.match(/<(\w+(\??))>/g);
|
||||
var tmpv = null;
|
||||
for(var v in vars){
|
||||
tmpv = '<'+v+'>';
|
||||
if($.inArray(tmpv,urlparams)>-1){
|
||||
newUrl = newUrl.replace(tmpv,vars[v]);
|
||||
delete vars[v];
|
||||
}
|
||||
}
|
||||
tmpv = urlparams = null;
|
||||
if(false !== WST.conf.SUFFIX){
|
||||
newUrl += "." + WST.conf.SUFFIX;
|
||||
}
|
||||
if($.isPlainObject(vars)){
|
||||
var tmp = $.param(vars);
|
||||
if(tmp!='')newUrl += "?"+tmp;
|
||||
tmp = null;
|
||||
}
|
||||
//url = url.replace(new RegExp("%2F","gm"),"+");
|
||||
newUrl = WST.conf.APP + "/"+newUrl;
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
WST.U0 = function(url, vars){
|
||||
if(!url || url=='')return '';
|
||||
var info = this.parse_url(url), path = [], reg;
|
||||
/* 验证info */
|
||||
info.path || $.error("url格式错误!");
|
||||
url = info.path;
|
||||
/* 解析URL */
|
||||
path = url.split("/");
|
||||
path = [path.pop(), path.pop(), path.pop()].reverse();
|
||||
path[1] || $.error("WST.U(" + url + ")没有指定控制器");
|
||||
|
||||
/* 解析参数 */
|
||||
if(typeof vars === "string"){
|
||||
vars = this.parse_str(vars);
|
||||
}
|
||||
/* 解析URL自带的参数 */
|
||||
info.query && $.extend(vars, this.parse_str(info.query));
|
||||
if(false !== WST.conf.SUFFIX){
|
||||
url += "." + WST.conf.SUFFIX;
|
||||
}
|
||||
if($.isPlainObject(vars)){
|
||||
var tmp = $.param(vars);
|
||||
if(tmp!='')url += "?"+tmp;
|
||||
tmp = null;
|
||||
}
|
||||
//url = url.replace(new RegExp("%2F","gm"),"+");
|
||||
url = WST.conf.APP + "/"+url;
|
||||
return url;
|
||||
}
|
||||
WST.U = function(url,vars){
|
||||
if(WST.conf.ROUTES && WST.conf.ROUTES[url]){
|
||||
return WST.initU(url,vars);
|
||||
}else{
|
||||
return WST.U0(url, vars);
|
||||
}
|
||||
}
|
||||
|
||||
WST.AU = function(url, vars){
|
||||
if(!url || url=='')return '';
|
||||
var info = this.parse_url(url);
|
||||
url = info.path;
|
||||
path = url.split("/");
|
||||
url = "addon/";
|
||||
path = [path.pop(), path.pop()].reverse();
|
||||
path[0] || $.error("WST.AU(" + url + ")没有指定控制器");
|
||||
path[1] || $.error("WST.AU(" + url + ")没有指定接口");
|
||||
url = url + info.scheme + "-" + path.join('-');
|
||||
/* 解析参数 */
|
||||
if(typeof vars === "string"){
|
||||
vars = this.parse_str(vars);
|
||||
}
|
||||
info.query && $.extend(vars, this.parse_str(info.query));
|
||||
if(false !== WST.conf.SUFFIX){
|
||||
url += "." + WST.conf.SUFFIX;
|
||||
}
|
||||
if($.isPlainObject(vars)){
|
||||
var tmp = $.param(vars);
|
||||
if(tmp!='')url += "?"+tmp;
|
||||
tmp = null;
|
||||
}
|
||||
return WST.conf.APP + "/"+url;
|
||||
}
|
||||
});
|
||||
|
||||
WST.replaceImg = function(v,str){
|
||||
var vs = v.split('.');
|
||||
return v.replace("."+vs[1],str+"."+vs[1]);
|
||||
}
|
8
static/js/jquery.min.js
vendored
Executable file
8
static/js/jquery.min.js
vendored
Executable file
File diff suppressed because one or more lines are too long
1
static/js/jquery.min.map
Executable file
1
static/js/jquery.min.map
Executable file
File diff suppressed because one or more lines are too long
88
static/js/qrcode.js
Executable file
88
static/js/qrcode.js
Executable file
@ -0,0 +1,88 @@
|
||||
|
||||
var qrcode=function(){var qrcode=function(typeNumber,errorCorrectLevel){var PAD0=0xEC;var PAD1=0x11;var _typeNumber=typeNumber;var _errorCorrectLevel=QRErrorCorrectLevel[errorCorrectLevel];var _modules=null;var _moduleCount=0;var _dataCache=null;var _dataList=new Array();var _this={};var makeImpl=function(test,maskPattern){_moduleCount=_typeNumber*4+17;_modules=function(moduleCount){var modules=new Array(moduleCount);for(var row=0;row<moduleCount;row+=1){modules[row]=new Array(moduleCount);for(var col=0;col<moduleCount;col+=1){modules[row][col]=null;}}
|
||||
return modules;}(_moduleCount);setupPositionProbePattern(0,0);setupPositionProbePattern(_moduleCount-7,0);setupPositionProbePattern(0,_moduleCount-7);setupPositionAdjustPattern();setupTimingPattern();setupTypeInfo(test,maskPattern);if(_typeNumber>=7){setupTypeNumber(test);}
|
||||
if(_dataCache==null){_dataCache=createData(_typeNumber,_errorCorrectLevel,_dataList);}
|
||||
mapData(_dataCache,maskPattern);};var setupPositionProbePattern=function(row,col){for(var r=-1;r<=7;r+=1){if(row+r<=-1||_moduleCount<=row+r)continue;for(var c=-1;c<=7;c+=1){if(col+c<=-1||_moduleCount<=col+c)continue;if((0<=r&&r<=6&&(c==0||c==6))||(0<=c&&c<=6&&(r==0||r==6))||(2<=r&&r<=4&&2<=c&&c<=4)){_modules[row+r][col+c]=true;}else{_modules[row+r][col+c]=false;}}}};var getBestMaskPattern=function(){var minLostPoint=0;var pattern=0;for(var i=0;i<8;i+=1){makeImpl(true,i);var lostPoint=QRUtil.getLostPoint(_this);if(i==0||minLostPoint>lostPoint){minLostPoint=lostPoint;pattern=i;}}
|
||||
return pattern;};var setupTimingPattern=function(){for(var r=8;r<_moduleCount-8;r+=1){if(_modules[r][6]!=null){continue;}
|
||||
_modules[r][6]=(r%2==0);}
|
||||
for(var c=8;c<_moduleCount-8;c+=1){if(_modules[6][c]!=null){continue;}
|
||||
_modules[6][c]=(c%2==0);}};var setupPositionAdjustPattern=function(){var pos=QRUtil.getPatternPosition(_typeNumber);for(var i=0;i<pos.length;i+=1){for(var j=0;j<pos.length;j+=1){var row=pos[i];var col=pos[j];if(_modules[row][col]!=null){continue;}
|
||||
for(var r=-2;r<=2;r+=1){for(var c=-2;c<=2;c+=1){if(r==-2||r==2||c==-2||c==2||(r==0&&c==0)){_modules[row+r][col+c]=true;}else{_modules[row+r][col+c]=false;}}}}}};var setupTypeNumber=function(test){var bits=QRUtil.getBCHTypeNumber(_typeNumber);for(var i=0;i<18;i+=1){var mod=(!test&&((bits>>i)&1)==1);_modules[Math.floor(i/3)][i%3+_moduleCount-8-3]=mod;}
|
||||
for(var i=0;i<18;i+=1){var mod=(!test&&((bits>>i)&1)==1);_modules[i%3+_moduleCount-8-3][Math.floor(i/3)]=mod;}};var setupTypeInfo=function(test,maskPattern){var data=(_errorCorrectLevel<<3)|maskPattern;var bits=QRUtil.getBCHTypeInfo(data);for(var i=0;i<15;i+=1){var mod=(!test&&((bits>>i)&1)==1);if(i<6){_modules[i][8]=mod;}else if(i<8){_modules[i+1][8]=mod;}else{_modules[_moduleCount-15+i][8]=mod;}}
|
||||
for(var i=0;i<15;i+=1){var mod=(!test&&((bits>>i)&1)==1);if(i<8){_modules[8][_moduleCount-i-1]=mod;}else if(i<9){_modules[8][15-i-1+1]=mod;}else{_modules[8][15-i-1]=mod;}}
|
||||
_modules[_moduleCount-8][8]=(!test);};var mapData=function(data,maskPattern){var inc=-1;var row=_moduleCount-1;var bitIndex=7;var byteIndex=0;var maskFunc=QRUtil.getMaskFunction(maskPattern);for(var col=_moduleCount-1;col>0;col-=2){if(col==6)col-=1;while(true){for(var c=0;c<2;c+=1){if(_modules[row][col-c]==null){var dark=false;if(byteIndex<data.length){dark=(((data[byteIndex]>>>bitIndex)&1)==1);}
|
||||
var mask=maskFunc(row,col-c);if(mask){dark=!dark;}
|
||||
_modules[row][col-c]=dark;bitIndex-=1;if(bitIndex==-1){byteIndex+=1;bitIndex=7;}}}
|
||||
row+=inc;if(row<0||_moduleCount<=row){row-=inc;inc=-inc;break;}}}};var createBytes=function(buffer,rsBlocks){var offset=0;var maxDcCount=0;var maxEcCount=0;var dcdata=new Array(rsBlocks.length);var ecdata=new Array(rsBlocks.length);for(var r=0;r<rsBlocks.length;r+=1){var dcCount=rsBlocks[r].dataCount;var ecCount=rsBlocks[r].totalCount-dcCount;maxDcCount=Math.max(maxDcCount,dcCount);maxEcCount=Math.max(maxEcCount,ecCount);dcdata[r]=new Array(dcCount);for(var i=0;i<dcdata[r].length;i+=1){dcdata[r][i]=0xff&buffer.getBuffer()[i+offset];}
|
||||
offset+=dcCount;var rsPoly=QRUtil.getErrorCorrectPolynomial(ecCount);var rawPoly=qrPolynomial(dcdata[r],rsPoly.getLength()-1);var modPoly=rawPoly.mod(rsPoly);ecdata[r]=new Array(rsPoly.getLength()-1);for(var i=0;i<ecdata[r].length;i+=1){var modIndex=i+modPoly.getLength()-ecdata[r].length;ecdata[r][i]=(modIndex>=0)?modPoly.getAt(modIndex):0;}}
|
||||
var totalCodeCount=0;for(var i=0;i<rsBlocks.length;i+=1){totalCodeCount+=rsBlocks[i].totalCount;}
|
||||
var data=new Array(totalCodeCount);var index=0;for(var i=0;i<maxDcCount;i+=1){for(var r=0;r<rsBlocks.length;r+=1){if(i<dcdata[r].length){data[index]=dcdata[r][i];index+=1;}}}
|
||||
for(var i=0;i<maxEcCount;i+=1){for(var r=0;r<rsBlocks.length;r+=1){if(i<ecdata[r].length){data[index]=ecdata[r][i];index+=1;}}}
|
||||
return data;};var createData=function(typeNumber,errorCorrectLevel,dataList){var rsBlocks=QRRSBlock.getRSBlocks(typeNumber,errorCorrectLevel);var buffer=qrBitBuffer();for(var i=0;i<dataList.length;i+=1){var data=dataList[i];buffer.put(data.getMode(),4);buffer.put(data.getLength(),QRUtil.getLengthInBits(data.getMode(),typeNumber));data.write(buffer);}
|
||||
var totalDataCount=0;for(var i=0;i<rsBlocks.length;i+=1){totalDataCount+=rsBlocks[i].dataCount;}
|
||||
if(buffer.getLengthInBits()>totalDataCount*8){throw new Error('code length overflow. ('
|
||||
+buffer.getLengthInBits()
|
||||
+'>'
|
||||
+totalDataCount*8
|
||||
+')');}
|
||||
if(buffer.getLengthInBits()+4<=totalDataCount*8){buffer.put(0,4);}
|
||||
while(buffer.getLengthInBits()%8!=0){buffer.putBit(false);}
|
||||
while(true){if(buffer.getLengthInBits()>=totalDataCount*8){break;}
|
||||
buffer.put(PAD0,8);if(buffer.getLengthInBits()>=totalDataCount*8){break;}
|
||||
buffer.put(PAD1,8);}
|
||||
return createBytes(buffer,rsBlocks);};_this.addData=function(data){var newData=qr8BitByte(data);_dataList.push(newData);_dataCache=null;};_this.isDark=function(row,col){if(row<0||_moduleCount<=row||col<0||_moduleCount<=col){throw new Error(row+','+col);}
|
||||
return _modules[row][col];};_this.getModuleCount=function(){return _moduleCount;};_this.make=function(){makeImpl(false,getBestMaskPattern());};_this.createTableTag=function(cellSize,margin){cellSize=cellSize||2;margin=(typeof margin=='undefined')?cellSize*4:margin;var qrHtml='';qrHtml+='<table style="';qrHtml+=' border-width: 0px; border-style: none;';qrHtml+=' border-collapse: collapse;';qrHtml+=' padding: 0px; margin: '+margin+'px;';qrHtml+='">';qrHtml+='<tbody>';for(var r=0;r<_this.getModuleCount();r+=1){qrHtml+='<tr>';for(var c=0;c<_this.getModuleCount();c+=1){qrHtml+='<td style="';qrHtml+=' border-width: 0px; border-style: none;';qrHtml+=' border-collapse: collapse;';qrHtml+=' padding: 0px; margin: 0px;';qrHtml+=' width: '+cellSize+'px;';qrHtml+=' height: '+cellSize+'px;';qrHtml+=' background-color: ';qrHtml+=_this.isDark(r,c)?'#000000':'#ffffff';qrHtml+=';';qrHtml+='"/>';}
|
||||
qrHtml+='</tr>';}
|
||||
qrHtml+='</tbody>';qrHtml+='</table>';return qrHtml;};_this.createImgTag=function(cellSize,margin){cellSize=cellSize||2;margin=(typeof margin=='undefined')?cellSize*4:margin;var size=_this.getModuleCount()*cellSize+margin*2;var min=margin;var max=size-margin;return createImgTag(size,size,function(x,y){if(min<=x&&x<max&&min<=y&&y<max){var c=Math.floor((x-min)/cellSize);var r=Math.floor((y-min)/cellSize);return _this.isDark(r,c)?0:1;}else{return 1;}});};return _this;};qrcode.stringToBytes=function(s){var bytes=new Array();for(var i=0;i<s.length;i+=1){var c=s.charCodeAt(i);bytes.push(c&0xff);}
|
||||
return bytes;};qrcode.createStringToBytes=function(unicodeData,numChars){var unicodeMap=function(){var bin=base64DecodeInputStream(unicodeData);var read=function(){var b=bin.read();if(b==-1)throw new Error();return b;};var count=0;var unicodeMap={};while(true){var b0=bin.read();if(b0==-1)break;var b1=read();var b2=read();var b3=read();var k=String.fromCharCode((b0<<8)|b1);var v=(b2<<8)|b3;unicodeMap[k]=v;count+=1;}
|
||||
if(count!=numChars){throw new Error(count+' != '+numChars);}
|
||||
return unicodeMap;}();var unknownChar='?'.charCodeAt(0);return function(s){var bytes=new Array();for(var i=0;i<s.length;i+=1){var c=s.charCodeAt(i);if(c<128){bytes.push(c);}else{var b=unicodeMap[s.charAt(i)];if(typeof b=='number'){if((b&0xff)==b){bytes.push(b);}else{bytes.push(b>>>8);bytes.push(b&0xff);}}else{bytes.push(unknownChar);}}}
|
||||
return bytes;};};var QRMode={MODE_NUMBER:1<<0,MODE_ALPHA_NUM:1<<1,MODE_8BIT_BYTE:1<<2,MODE_KANJI:1<<3};var QRErrorCorrectLevel={L:1,M:0,Q:3,H:2};var QRMaskPattern={PATTERN000:0,PATTERN001:1,PATTERN010:2,PATTERN011:3,PATTERN100:4,PATTERN101:5,PATTERN110:6,PATTERN111:7};var QRUtil=function(){var PATTERN_POSITION_TABLE=[[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,22,38],[6,24,42],[6,26,46],[6,28,50],[6,30,54],[6,32,58],[6,34,62],[6,26,46,66],[6,26,48,70],[6,26,50,74],[6,30,54,78],[6,30,56,82],[6,30,58,86],[6,34,62,90],[6,28,50,72,94],[6,26,50,74,98],[6,30,54,78,102],[6,28,54,80,106],[6,32,58,84,110],[6,30,58,86,114],[6,34,62,90,118],[6,26,50,74,98,122],[6,30,54,78,102,126],[6,26,52,78,104,130],[6,30,56,82,108,134],[6,34,60,86,112,138],[6,30,58,86,114,142],[6,34,62,90,118,146],[6,30,54,78,102,126,150],[6,24,50,76,102,128,154],[6,28,54,80,106,132,158],[6,32,58,84,110,136,162],[6,26,54,82,110,138,166],[6,30,58,86,114,142,170]];var G15=(1<<10)|(1<<8)|(1<<5)|(1<<4)|(1<<2)|(1<<1)|(1<<0);var G18=(1<<12)|(1<<11)|(1<<10)|(1<<9)|(1<<8)|(1<<5)|(1<<2)|(1<<0);var G15_MASK=(1<<14)|(1<<12)|(1<<10)|(1<<4)|(1<<1);var _this={};var getBCHDigit=function(data){var digit=0;while(data!=0){digit+=1;data>>>=1;}
|
||||
return digit;};_this.getBCHTypeInfo=function(data){var d=data<<10;while(getBCHDigit(d)-getBCHDigit(G15)>=0){d^=(G15<<(getBCHDigit(d)-getBCHDigit(G15)));}
|
||||
return((data<<10)|d)^G15_MASK;};_this.getBCHTypeNumber=function(data){var d=data<<12;while(getBCHDigit(d)-getBCHDigit(G18)>=0){d^=(G18<<(getBCHDigit(d)-getBCHDigit(G18)));}
|
||||
return(data<<12)|d;};_this.getPatternPosition=function(typeNumber){return PATTERN_POSITION_TABLE[typeNumber-1];};_this.getMaskFunction=function(maskPattern){switch(maskPattern){case QRMaskPattern.PATTERN000:return function(i,j){return(i+j)%2==0;};case QRMaskPattern.PATTERN001:return function(i,j){return i%2==0;};case QRMaskPattern.PATTERN010:return function(i,j){return j%3==0;};case QRMaskPattern.PATTERN011:return function(i,j){return(i+j)%3==0;};case QRMaskPattern.PATTERN100:return function(i,j){return(Math.floor(i/2)+Math.floor(j/3))%2==0;};case QRMaskPattern.PATTERN101:return function(i,j){return(i*j)%2+(i*j)%3==0;};case QRMaskPattern.PATTERN110:return function(i,j){return((i*j)%2+(i*j)%3)%2==0;};case QRMaskPattern.PATTERN111:return function(i,j){return((i*j)%3+(i+j)%2)%2==0;};default:throw new Error('bad maskPattern:'+maskPattern);}};_this.getErrorCorrectPolynomial=function(errorCorrectLength){var a=qrPolynomial([1],0);for(var i=0;i<errorCorrectLength;i+=1){a=a.multiply(qrPolynomial([1,QRMath.gexp(i)],0));}
|
||||
return a;};_this.getLengthInBits=function(mode,type){if(1<=type&&type<10){switch(mode){case QRMode.MODE_NUMBER:return 10;case QRMode.MODE_ALPHA_NUM:return 9;case QRMode.MODE_8BIT_BYTE:return 8;case QRMode.MODE_KANJI:return 8;default:throw new Error('mode:'+mode);}}else if(type<27){switch(mode){case QRMode.MODE_NUMBER:return 12;case QRMode.MODE_ALPHA_NUM:return 11;case QRMode.MODE_8BIT_BYTE:return 16;case QRMode.MODE_KANJI:return 10;default:throw new Error('mode:'+mode);}}else if(type<41){switch(mode){case QRMode.MODE_NUMBER:return 14;case QRMode.MODE_ALPHA_NUM:return 13;case QRMode.MODE_8BIT_BYTE:return 16;case QRMode.MODE_KANJI:return 12;default:throw new Error('mode:'+mode);}}else{throw new Error('type:'+type);}};_this.getLostPoint=function(qrcode){var moduleCount=qrcode.getModuleCount();var lostPoint=0;for(var row=0;row<moduleCount;row+=1){for(var col=0;col<moduleCount;col+=1){var sameCount=0;var dark=qrcode.isDark(row,col);for(var r=-1;r<=1;r+=1){if(row+r<0||moduleCount<=row+r){continue;}
|
||||
for(var c=-1;c<=1;c+=1){if(col+c<0||moduleCount<=col+c){continue;}
|
||||
if(r==0&&c==0){continue;}
|
||||
if(dark==qrcode.isDark(row+r,col+c)){sameCount+=1;}}}
|
||||
if(sameCount>5){lostPoint+=(3+sameCount-5);}}};for(var row=0;row<moduleCount-1;row+=1){for(var col=0;col<moduleCount-1;col+=1){var count=0;if(qrcode.isDark(row,col))count+=1;if(qrcode.isDark(row+1,col))count+=1;if(qrcode.isDark(row,col+1))count+=1;if(qrcode.isDark(row+1,col+1))count+=1;if(count==0||count==4){lostPoint+=3;}}}
|
||||
for(var row=0;row<moduleCount;row+=1){for(var col=0;col<moduleCount-6;col+=1){if(qrcode.isDark(row,col)&&!qrcode.isDark(row,col+1)&&qrcode.isDark(row,col+2)&&qrcode.isDark(row,col+3)&&qrcode.isDark(row,col+4)&&!qrcode.isDark(row,col+5)&&qrcode.isDark(row,col+6)){lostPoint+=40;}}}
|
||||
for(var col=0;col<moduleCount;col+=1){for(var row=0;row<moduleCount-6;row+=1){if(qrcode.isDark(row,col)&&!qrcode.isDark(row+1,col)&&qrcode.isDark(row+2,col)&&qrcode.isDark(row+3,col)&&qrcode.isDark(row+4,col)&&!qrcode.isDark(row+5,col)&&qrcode.isDark(row+6,col)){lostPoint+=40;}}}
|
||||
var darkCount=0;for(var col=0;col<moduleCount;col+=1){for(var row=0;row<moduleCount;row+=1){if(qrcode.isDark(row,col)){darkCount+=1;}}}
|
||||
var ratio=Math.abs(100*darkCount/moduleCount/moduleCount-50)/5;lostPoint+=ratio*10;return lostPoint;};return _this;}();var QRMath=function(){var EXP_TABLE=new Array(256);var LOG_TABLE=new Array(256);for(var i=0;i<8;i+=1){EXP_TABLE[i]=1<<i;}
|
||||
for(var i=8;i<256;i+=1){EXP_TABLE[i]=EXP_TABLE[i-4]^EXP_TABLE[i-5]^EXP_TABLE[i-6]^EXP_TABLE[i-8];}
|
||||
for(var i=0;i<255;i+=1){LOG_TABLE[EXP_TABLE[i]]=i;}
|
||||
var _this={};_this.glog=function(n){if(n<1){throw new Error('glog('+n+')');}
|
||||
return LOG_TABLE[n];};_this.gexp=function(n){while(n<0){n+=255;}
|
||||
while(n>=256){n-=255;}
|
||||
return EXP_TABLE[n];};return _this;}();function qrPolynomial(num,shift){if(typeof num.length=='undefined'){throw new Error(num.length+'/'+shift);}
|
||||
var _num=function(){var offset=0;while(offset<num.length&&num[offset]==0){offset+=1;}
|
||||
var _num=new Array(num.length-offset+shift);for(var i=0;i<num.length-offset;i+=1){_num[i]=num[i+offset];}
|
||||
return _num;}();var _this={};_this.getAt=function(index){return _num[index];};_this.getLength=function(){return _num.length;};_this.multiply=function(e){var num=new Array(_this.getLength()+e.getLength()-1);for(var i=0;i<_this.getLength();i+=1){for(var j=0;j<e.getLength();j+=1){num[i+j]^=QRMath.gexp(QRMath.glog(_this.getAt(i))+QRMath.glog(e.getAt(j)));}}
|
||||
return qrPolynomial(num,0);};_this.mod=function(e){if(_this.getLength()-e.getLength()<0){return _this;}
|
||||
var ratio=QRMath.glog(_this.getAt(0))-QRMath.glog(e.getAt(0));var num=new Array(_this.getLength());for(var i=0;i<_this.getLength();i+=1){num[i]=_this.getAt(i);}
|
||||
for(var i=0;i<e.getLength();i+=1){num[i]^=QRMath.gexp(QRMath.glog(e.getAt(i))+ratio);}
|
||||
return qrPolynomial(num,0).mod(e);};return _this;};var QRRSBlock=function(){var RS_BLOCK_TABLE=[[1,26,19],[1,26,16],[1,26,13],[1,26,9],[1,44,34],[1,44,28],[1,44,22],[1,44,16],[1,70,55],[1,70,44],[2,35,17],[2,35,13],[1,100,80],[2,50,32],[2,50,24],[4,25,9],[1,134,108],[2,67,43],[2,33,15,2,34,16],[2,33,11,2,34,12],[2,86,68],[4,43,27],[4,43,19],[4,43,15],[2,98,78],[4,49,31],[2,32,14,4,33,15],[4,39,13,1,40,14],[2,121,97],[2,60,38,2,61,39],[4,40,18,2,41,19],[4,40,14,2,41,15],[2,146,116],[3,58,36,2,59,37],[4,36,16,4,37,17],[4,36,12,4,37,13],[2,86,68,2,87,69],[4,69,43,1,70,44],[6,43,19,2,44,20],[6,43,15,2,44,16]];var qrRSBlock=function(totalCount,dataCount){var _this={};_this.totalCount=totalCount;_this.dataCount=dataCount;return _this;};var _this={};var getRsBlockTable=function(typeNumber,errorCorrectLevel){switch(errorCorrectLevel){case QRErrorCorrectLevel.L:return RS_BLOCK_TABLE[(typeNumber-1)*4+0];case QRErrorCorrectLevel.M:return RS_BLOCK_TABLE[(typeNumber-1)*4+1];case QRErrorCorrectLevel.Q:return RS_BLOCK_TABLE[(typeNumber-1)*4+2];case QRErrorCorrectLevel.H:return RS_BLOCK_TABLE[(typeNumber-1)*4+3];default:return undefined;}};_this.getRSBlocks=function(typeNumber,errorCorrectLevel){var rsBlock=getRsBlockTable(typeNumber,errorCorrectLevel);if(typeof rsBlock=='undefined'){throw new Error('bad rs block @ typeNumber:'+typeNumber+'/errorCorrectLevel:'+errorCorrectLevel);}
|
||||
var length=rsBlock.length/3;var list=new Array();for(var i=0;i<length;i+=1){var count=rsBlock[i*3+0];var totalCount=rsBlock[i*3+1];var dataCount=rsBlock[i*3+2];for(var j=0;j<count;j+=1){list.push(qrRSBlock(totalCount,dataCount));}}
|
||||
return list;};return _this;}();var qrBitBuffer=function(){var _buffer=new Array();var _length=0;var _this={};_this.getBuffer=function(){return _buffer;};_this.getAt=function(index){var bufIndex=Math.floor(index/8);return((_buffer[bufIndex]>>>(7-index%8))&1)==1;};_this.put=function(num,length){for(var i=0;i<length;i+=1){_this.putBit(((num>>>(length-i-1))&1)==1);}};_this.getLengthInBits=function(){return _length;};_this.putBit=function(bit){var bufIndex=Math.floor(_length/8);if(_buffer.length<=bufIndex){_buffer.push(0);}
|
||||
if(bit){_buffer[bufIndex]|=(0x80>>>(_length%8));}
|
||||
_length+=1;};return _this;};var qr8BitByte=function(data){var _mode=QRMode.MODE_8BIT_BYTE;var _data=data;var _bytes=qrcode.stringToBytes(data);var _this={};_this.getMode=function(){return _mode;};_this.getLength=function(buffer){return _bytes.length;};_this.write=function(buffer){for(var i=0;i<_bytes.length;i+=1){buffer.put(_bytes[i],8);}};return _this;};var byteArrayOutputStream=function(){var _bytes=new Array();var _this={};_this.writeByte=function(b){_bytes.push(b&0xff);};_this.writeShort=function(i){_this.writeByte(i);_this.writeByte(i>>>8);};_this.writeBytes=function(b,off,len){off=off||0;len=len||b.length;for(var i=0;i<len;i+=1){_this.writeByte(b[i+off]);}};_this.writeString=function(s){for(var i=0;i<s.length;i+=1){_this.writeByte(s.charCodeAt(i));}};_this.toByteArray=function(){return _bytes;};_this.toString=function(){var s='';s+='[';for(var i=0;i<_bytes.length;i+=1){if(i>0){s+=',';}
|
||||
s+=_bytes[i];}
|
||||
s+=']';return s;};return _this;};var base64EncodeOutputStream=function(){var _buffer=0;var _buflen=0;var _length=0;var _base64='';var _this={};var writeEncoded=function(b){_base64+=String.fromCharCode(encode(b&0x3f));};var encode=function(n){if(n<0){}else if(n<26){return 0x41+n;}else if(n<52){return 0x61+(n-26);}else if(n<62){return 0x30+(n-52);}else if(n==62){return 0x2b;}else if(n==63){return 0x2f;}
|
||||
throw new Error('n:'+n);};_this.writeByte=function(n){_buffer=(_buffer<<8)|(n&0xff);_buflen+=8;_length+=1;while(_buflen>=6){writeEncoded(_buffer>>>(_buflen-6));_buflen-=6;}};_this.flush=function(){if(_buflen>0){writeEncoded(_buffer<<(6-_buflen));_buffer=0;_buflen=0;}
|
||||
if(_length%3!=0){var padlen=3-_length%3;for(var i=0;i<padlen;i+=1){_base64+='=';}}};_this.toString=function(){return _base64;};return _this;};var base64DecodeInputStream=function(str){var _str=str;var _pos=0;var _buffer=0;var _buflen=0;var _this={};_this.read=function(){while(_buflen<8){if(_pos>=_str.length){if(_buflen==0){return-1;}
|
||||
throw new Error('unexpected end of file./'+_buflen);}
|
||||
var c=_str.charAt(_pos);_pos+=1;if(c=='='){_buflen=0;return-1;}else if(c.match(/^\s$/)){continue;}
|
||||
_buffer=(_buffer<<6)|decode(c.charCodeAt(0));_buflen+=6;}
|
||||
var n=(_buffer>>>(_buflen-8))&0xff;_buflen-=8;return n;};var decode=function(c){if(0x41<=c&&c<=0x5a){return c-0x41;}else if(0x61<=c&&c<=0x7a){return c-0x61+26;}else if(0x30<=c&&c<=0x39){return c-0x30+52;}else if(c==0x2b){return 62;}else if(c==0x2f){return 63;}else{throw new Error('c:'+c);}};return _this;};var gifImage=function(width,height){var _width=width;var _height=height;var _data=new Array(width*height);var _this={};_this.setPixel=function(x,y,pixel){_data[y*_width+x]=pixel;};_this.write=function(out){out.writeString('GIF87a');out.writeShort(_width);out.writeShort(_height);out.writeByte(0x80);out.writeByte(0);out.writeByte(0);out.writeByte(0x00);out.writeByte(0x00);out.writeByte(0x00);out.writeByte(0xff);out.writeByte(0xff);out.writeByte(0xff);out.writeString(',');out.writeShort(0);out.writeShort(0);out.writeShort(_width);out.writeShort(_height);out.writeByte(0);var lzwMinCodeSize=2;var raster=getLZWRaster(lzwMinCodeSize);out.writeByte(lzwMinCodeSize);var offset=0;while(raster.length-offset>255){out.writeByte(255);out.writeBytes(raster,offset,255);offset+=255;}
|
||||
out.writeByte(raster.length-offset);out.writeBytes(raster,offset,raster.length-offset);out.writeByte(0x00);out.writeString(';');};var bitOutputStream=function(out){var _out=out;var _bitLength=0;var _bitBuffer=0;var _this={};_this.write=function(data,length){if((data>>>length)!=0){throw new Error('length over');}
|
||||
while(_bitLength+length>=8){_out.writeByte(0xff&((data<<_bitLength)|_bitBuffer));length-=(8-_bitLength);data>>>=(8-_bitLength);_bitBuffer=0;_bitLength=0;}
|
||||
_bitBuffer=(data<<_bitLength)|_bitBuffer;_bitLength=_bitLength+length;};_this.flush=function(){if(_bitLength>0){_out.writeByte(_bitBuffer);}};return _this;};var getLZWRaster=function(lzwMinCodeSize){var clearCode=1<<lzwMinCodeSize;var endCode=(1<<lzwMinCodeSize)+1;var bitLength=lzwMinCodeSize+1;var table=lzwTable();for(var i=0;i<clearCode;i+=1){table.add(String.fromCharCode(i));}
|
||||
table.add(String.fromCharCode(clearCode));table.add(String.fromCharCode(endCode));var byteOut=byteArrayOutputStream();var bitOut=bitOutputStream(byteOut);bitOut.write(clearCode,bitLength);var dataIndex=0;var s=String.fromCharCode(_data[dataIndex]);dataIndex+=1;while(dataIndex<_data.length){var c=String.fromCharCode(_data[dataIndex]);dataIndex+=1;if(table.contains(s+c)){s=s+c;}else{bitOut.write(table.indexOf(s),bitLength);if(table.size()<0xfff){if(table.size()==(1<<bitLength)){bitLength+=1;}
|
||||
table.add(s+c);}
|
||||
s=c;}}
|
||||
bitOut.write(table.indexOf(s),bitLength);bitOut.write(endCode,bitLength);bitOut.flush();return byteOut.toByteArray();};var lzwTable=function(){var _map={};var _size=0;var _this={};_this.add=function(key){if(_this.contains(key)){throw new Error('dup key:'+key);}
|
||||
_map[key]=_size;_size+=1;};_this.size=function(){return _size;};_this.indexOf=function(key){return _map[key];};_this.contains=function(key){return typeof _map[key]!='undefined';};return _this;};return _this;};var createImgTag=function(width,height,getPixel,alt){var gif=gifImage(width,height);for(var y=0;y<height;y+=1){for(var x=0;x<width;x+=1){gif.setPixel(x,y,getPixel(x,y));}}
|
||||
var b=byteArrayOutputStream();gif.write(b);var base64=base64EncodeOutputStream();var bytes=b.toByteArray();for(var i=0;i<bytes.length;i+=1){base64.writeByte(bytes[i]);}
|
||||
base64.flush();var img='';img+='<img';img+='\u0020src="';img+='data:image/gif;base64,';img+=base64;img+='"';img+='\u0020width="';img+=width;img+='"';img+='\u0020height="';img+=height;img+='"';if(alt){img+='\u0020alt="';img+=alt;img+='"';}
|
||||
img+='/>';return img;};return qrcode;}();/* |xGv00|ca8fc6bde81a353e7cede123b304bdb9 */
|
790
static/js/rsa.js
Executable file
790
static/js/rsa.js
Executable file
@ -0,0 +1,790 @@
|
||||
// Copyright (c) 2005 Tom Wu
|
||||
// All Rights Reserved.
|
||||
// See "LICENSE" for details.
|
||||
|
||||
// Basic JavaScript BN library - subset useful for RSA encryption.
|
||||
|
||||
// Bits per digit
|
||||
var dbits;
|
||||
|
||||
// JavaScript engine analysis
|
||||
var canary = 0xdeadbeefcafe;
|
||||
var j_lm = ((canary&0xffffff)==0xefcafe);
|
||||
|
||||
// (public) Constructor
|
||||
function BigInteger(a,b,c) {
|
||||
if(a != null)
|
||||
if("number" == typeof a) this.fromNumber(a,b,c);
|
||||
else if(b == null && "string" != typeof a) this.fromString(a,256);
|
||||
else this.fromString(a,b);
|
||||
}
|
||||
|
||||
// return new, unset BigInteger
|
||||
function nbi() { return new BigInteger(null); }
|
||||
|
||||
// am: Compute w_j += (x*this_i), propagate carries,
|
||||
// c is initial carry, returns final carry.
|
||||
// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
|
||||
// We need to select the fastest one that works in this environment.
|
||||
|
||||
// am1: use a single mult and divide to get the high bits,
|
||||
// max digit bits should be 26 because
|
||||
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
|
||||
function am1(i,x,w,j,c,n) {
|
||||
while(--n >= 0) {
|
||||
var v = x*this[i++]+w[j]+c;
|
||||
c = Math.floor(v/0x4000000);
|
||||
w[j++] = v&0x3ffffff;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
// am2 avoids a big mult-and-extract completely.
|
||||
// Max digit bits should be <= 30 because we do bitwise ops
|
||||
// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
|
||||
function am2(i,x,w,j,c,n) {
|
||||
var xl = x&0x7fff, xh = x>>15;
|
||||
while(--n >= 0) {
|
||||
var l = this[i]&0x7fff;
|
||||
var h = this[i++]>>15;
|
||||
var m = xh*l+h*xl;
|
||||
l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
|
||||
c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
|
||||
w[j++] = l&0x3fffffff;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
// Alternately, set max digit bits to 28 since some
|
||||
// browsers slow down when dealing with 32-bit numbers.
|
||||
function am3(i,x,w,j,c,n) {
|
||||
var xl = x&0x3fff, xh = x>>14;
|
||||
while(--n >= 0) {
|
||||
var l = this[i]&0x3fff;
|
||||
var h = this[i++]>>14;
|
||||
var m = xh*l+h*xl;
|
||||
l = xl*l+((m&0x3fff)<<14)+w[j]+c;
|
||||
c = (l>>28)+(m>>14)+xh*h;
|
||||
w[j++] = l&0xfffffff;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
|
||||
BigInteger.prototype.am = am2;
|
||||
dbits = 30;
|
||||
}
|
||||
else if(j_lm && (navigator.appName != "Netscape")) {
|
||||
BigInteger.prototype.am = am1;
|
||||
dbits = 26;
|
||||
}
|
||||
else { // Mozilla/Netscape seems to prefer am3
|
||||
BigInteger.prototype.am = am3;
|
||||
dbits = 28;
|
||||
}
|
||||
|
||||
BigInteger.prototype.DB = dbits;
|
||||
BigInteger.prototype.DM = ((1<<dbits)-1);
|
||||
BigInteger.prototype.DV = (1<<dbits);
|
||||
|
||||
var BI_FP = 52;
|
||||
BigInteger.prototype.FV = Math.pow(2,BI_FP);
|
||||
BigInteger.prototype.F1 = BI_FP-dbits;
|
||||
BigInteger.prototype.F2 = 2*dbits-BI_FP;
|
||||
|
||||
// Digit conversions
|
||||
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
var BI_RC = new Array();
|
||||
var rr,vv;
|
||||
rr = "0".charCodeAt(0);
|
||||
for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
|
||||
rr = "a".charCodeAt(0);
|
||||
for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
||||
rr = "A".charCodeAt(0);
|
||||
for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
|
||||
|
||||
function int2char(n) { return BI_RM.charAt(n); }
|
||||
function intAt(s,i) {
|
||||
var c = BI_RC[s.charCodeAt(i)];
|
||||
return (c==null)?-1:c;
|
||||
}
|
||||
|
||||
// (protected) copy this to r
|
||||
function bnpCopyTo(r) {
|
||||
for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
|
||||
r.t = this.t;
|
||||
r.s = this.s;
|
||||
}
|
||||
|
||||
// (protected) set from integer value x, -DV <= x < DV
|
||||
function bnpFromInt(x) {
|
||||
this.t = 1;
|
||||
this.s = (x<0)?-1:0;
|
||||
if(x > 0) this[0] = x;
|
||||
else if(x < -1) this[0] = x+DV;
|
||||
else this.t = 0;
|
||||
}
|
||||
|
||||
// return bigint initialized to value
|
||||
function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
|
||||
|
||||
// (protected) set from string and radix
|
||||
function bnpFromString(s,b) {
|
||||
var k;
|
||||
if(b == 16) k = 4;
|
||||
else if(b == 8) k = 3;
|
||||
else if(b == 256) k = 8; // byte array
|
||||
else if(b == 2) k = 1;
|
||||
else if(b == 32) k = 5;
|
||||
else if(b == 4) k = 2;
|
||||
else { this.fromRadix(s,b); return; }
|
||||
this.t = 0;
|
||||
this.s = 0;
|
||||
var i = s.length, mi = false, sh = 0;
|
||||
while(--i >= 0) {
|
||||
var x = (k==8)?s[i]&0xff:intAt(s,i);
|
||||
if(x < 0) {
|
||||
if(s.charAt(i) == "-") mi = true;
|
||||
continue;
|
||||
}
|
||||
mi = false;
|
||||
if(sh == 0)
|
||||
this[this.t++] = x;
|
||||
else if(sh+k > this.DB) {
|
||||
this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
|
||||
this[this.t++] = (x>>(this.DB-sh));
|
||||
}
|
||||
else
|
||||
this[this.t-1] |= x<<sh;
|
||||
sh += k;
|
||||
if(sh >= this.DB) sh -= this.DB;
|
||||
}
|
||||
if(k == 8 && (s[0]&0x80) != 0) {
|
||||
this.s = -1;
|
||||
if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
|
||||
}
|
||||
this.clamp();
|
||||
if(mi) BigInteger.ZERO.subTo(this,this);
|
||||
}
|
||||
|
||||
// (protected) clamp off excess high words
|
||||
function bnpClamp() {
|
||||
var c = this.s&this.DM;
|
||||
while(this.t > 0 && this[this.t-1] == c) --this.t;
|
||||
}
|
||||
|
||||
// (public) return string representation in given radix
|
||||
function bnToString(b) {
|
||||
if(this.s < 0) return "-"+this.negate().toString(b);
|
||||
var k;
|
||||
if(b == 16) k = 4;
|
||||
else if(b == 8) k = 3;
|
||||
else if(b == 2) k = 1;
|
||||
else if(b == 32) k = 5;
|
||||
else if(b == 4) k = 2;
|
||||
else return this.toRadix(b);
|
||||
var km = (1<<k)-1, d, m = false, r = "", i = this.t;
|
||||
var p = this.DB-(i*this.DB)%k;
|
||||
if(i-- > 0) {
|
||||
if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
|
||||
while(i >= 0) {
|
||||
if(p < k) {
|
||||
d = (this[i]&((1<<p)-1))<<(k-p);
|
||||
d |= this[--i]>>(p+=this.DB-k);
|
||||
}
|
||||
else {
|
||||
d = (this[i]>>(p-=k))&km;
|
||||
if(p <= 0) { p += this.DB; --i; }
|
||||
}
|
||||
if(d > 0) m = true;
|
||||
if(m) r += int2char(d);
|
||||
}
|
||||
}
|
||||
return m?r:"0";
|
||||
}
|
||||
|
||||
// (public) -this
|
||||
function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
|
||||
|
||||
// (public) |this|
|
||||
function bnAbs() { return (this.s<0)?this.negate():this; }
|
||||
|
||||
// (public) return + if this > a, - if this < a, 0 if equal
|
||||
function bnCompareTo(a) {
|
||||
var r = this.s-a.s;
|
||||
if(r != 0) return r;
|
||||
var i = this.t;
|
||||
r = i-a.t;
|
||||
if(r != 0) return r;
|
||||
while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// returns bit length of the integer x
|
||||
function nbits(x) {
|
||||
var r = 1, t;
|
||||
if((t=x>>>16) != 0) { x = t; r += 16; }
|
||||
if((t=x>>8) != 0) { x = t; r += 8; }
|
||||
if((t=x>>4) != 0) { x = t; r += 4; }
|
||||
if((t=x>>2) != 0) { x = t; r += 2; }
|
||||
if((t=x>>1) != 0) { x = t; r += 1; }
|
||||
return r;
|
||||
}
|
||||
|
||||
// (public) return the number of bits in "this"
|
||||
function bnBitLength() {
|
||||
if(this.t <= 0) return 0;
|
||||
return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
|
||||
}
|
||||
|
||||
// (protected) r = this << n*DB
|
||||
function bnpDLShiftTo(n,r) {
|
||||
var i;
|
||||
for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
|
||||
for(i = n-1; i >= 0; --i) r[i] = 0;
|
||||
r.t = this.t+n;
|
||||
r.s = this.s;
|
||||
}
|
||||
|
||||
// (protected) r = this >> n*DB
|
||||
function bnpDRShiftTo(n,r) {
|
||||
for(var i = n; i < this.t; ++i) r[i-n] = this[i];
|
||||
r.t = Math.max(this.t-n,0);
|
||||
r.s = this.s;
|
||||
}
|
||||
|
||||
// (protected) r = this << n
|
||||
function bnpLShiftTo(n,r) {
|
||||
var bs = n%this.DB;
|
||||
var cbs = this.DB-bs;
|
||||
var bm = (1<<cbs)-1;
|
||||
var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
|
||||
for(i = this.t-1; i >= 0; --i) {
|
||||
r[i+ds+1] = (this[i]>>cbs)|c;
|
||||
c = (this[i]&bm)<<bs;
|
||||
}
|
||||
for(i = ds-1; i >= 0; --i) r[i] = 0;
|
||||
r[ds] = c;
|
||||
r.t = this.t+ds+1;
|
||||
r.s = this.s;
|
||||
r.clamp();
|
||||
}
|
||||
|
||||
// (protected) r = this >> n
|
||||
function bnpRShiftTo(n,r) {
|
||||
r.s = this.s;
|
||||
var ds = Math.floor(n/this.DB);
|
||||
if(ds >= this.t) { r.t = 0; return; }
|
||||
var bs = n%this.DB;
|
||||
var cbs = this.DB-bs;
|
||||
var bm = (1<<bs)-1;
|
||||
r[0] = this[ds]>>bs;
|
||||
for(var i = ds+1; i < this.t; ++i) {
|
||||
r[i-ds-1] |= (this[i]&bm)<<cbs;
|
||||
r[i-ds] = this[i]>>bs;
|
||||
}
|
||||
if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
|
||||
r.t = this.t-ds;
|
||||
r.clamp();
|
||||
}
|
||||
|
||||
// (protected) r = this - a
|
||||
function bnpSubTo(a,r) {
|
||||
var i = 0, c = 0, m = Math.min(a.t,this.t);
|
||||
while(i < m) {
|
||||
c += this[i]-a[i];
|
||||
r[i++] = c&this.DM;
|
||||
c >>= this.DB;
|
||||
}
|
||||
if(a.t < this.t) {
|
||||
c -= a.s;
|
||||
while(i < this.t) {
|
||||
c += this[i];
|
||||
r[i++] = c&this.DM;
|
||||
c >>= this.DB;
|
||||
}
|
||||
c += this.s;
|
||||
}
|
||||
else {
|
||||
c += this.s;
|
||||
while(i < a.t) {
|
||||
c -= a[i];
|
||||
r[i++] = c&this.DM;
|
||||
c >>= this.DB;
|
||||
}
|
||||
c -= a.s;
|
||||
}
|
||||
r.s = (c<0)?-1:0;
|
||||
if(c < -1) r[i++] = this.DV+c;
|
||||
else if(c > 0) r[i++] = c;
|
||||
r.t = i;
|
||||
r.clamp();
|
||||
}
|
||||
|
||||
// (protected) r = this * a, r != this,a (HAC 14.12)
|
||||
// "this" should be the larger one if appropriate.
|
||||
function bnpMultiplyTo(a,r) {
|
||||
var x = this.abs(), y = a.abs();
|
||||
var i = x.t;
|
||||
r.t = i+y.t;
|
||||
while(--i >= 0) r[i] = 0;
|
||||
for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
|
||||
r.s = 0;
|
||||
r.clamp();
|
||||
if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
|
||||
}
|
||||
|
||||
// (protected) r = this^2, r != this (HAC 14.16)
|
||||
function bnpSquareTo(r) {
|
||||
var x = this.abs();
|
||||
var i = r.t = 2*x.t;
|
||||
while(--i >= 0) r[i] = 0;
|
||||
for(i = 0; i < x.t-1; ++i) {
|
||||
var c = x.am(i,x[i],r,2*i,0,1);
|
||||
if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
|
||||
r[i+x.t] -= x.DV;
|
||||
r[i+x.t+1] = 1;
|
||||
}
|
||||
}
|
||||
if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
|
||||
r.s = 0;
|
||||
r.clamp();
|
||||
}
|
||||
|
||||
// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
|
||||
// r != q, this != m. q or r may be null.
|
||||
function bnpDivRemTo(m,q,r) {
|
||||
var pm = m.abs();
|
||||
if(pm.t <= 0) return;
|
||||
var pt = this.abs();
|
||||
if(pt.t < pm.t) {
|
||||
if(q != null) q.fromInt(0);
|
||||
if(r != null) this.copyTo(r);
|
||||
return;
|
||||
}
|
||||
if(r == null) r = nbi();
|
||||
var y = nbi(), ts = this.s, ms = m.s;
|
||||
var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus
|
||||
if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
|
||||
else { pm.copyTo(y); pt.copyTo(r); }
|
||||
var ys = y.t;
|
||||
var y0 = y[ys-1];
|
||||
if(y0 == 0) return;
|
||||
var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
|
||||
var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
|
||||
var i = r.t, j = i-ys, t = (q==null)?nbi():q;
|
||||
y.dlShiftTo(j,t);
|
||||
if(r.compareTo(t) >= 0) {
|
||||
r[r.t++] = 1;
|
||||
r.subTo(t,r);
|
||||
}
|
||||
BigInteger.ONE.dlShiftTo(ys,t);
|
||||
t.subTo(y,y); // "negative" y so we can replace sub with am later
|
||||
while(y.t < ys) y[y.t++] = 0;
|
||||
while(--j >= 0) {
|
||||
// Estimate quotient digit
|
||||
var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
|
||||
if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
|
||||
y.dlShiftTo(j,t);
|
||||
r.subTo(t,r);
|
||||
while(r[i] < --qd) r.subTo(t,r);
|
||||
}
|
||||
}
|
||||
if(q != null) {
|
||||
r.drShiftTo(ys,q);
|
||||
if(ts != ms) BigInteger.ZERO.subTo(q,q);
|
||||
}
|
||||
r.t = ys;
|
||||
r.clamp();
|
||||
if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
|
||||
if(ts < 0) BigInteger.ZERO.subTo(r,r);
|
||||
}
|
||||
|
||||
// (public) this mod a
|
||||
function bnMod(a) {
|
||||
var r = nbi();
|
||||
this.abs().divRemTo(a,null,r);
|
||||
if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Modular reduction using "classic" algorithm
|
||||
function Classic(m) { this.m = m; }
|
||||
function cConvert(x) {
|
||||
if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
|
||||
else return x;
|
||||
}
|
||||
function cRevert(x) { return x; }
|
||||
function cReduce(x) { x.divRemTo(this.m,null,x); }
|
||||
function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
|
||||
function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
|
||||
|
||||
Classic.prototype.convert = cConvert;
|
||||
Classic.prototype.revert = cRevert;
|
||||
Classic.prototype.reduce = cReduce;
|
||||
Classic.prototype.mulTo = cMulTo;
|
||||
Classic.prototype.sqrTo = cSqrTo;
|
||||
|
||||
// (protected) return "-1/this % 2^DB"; useful for Mont. reduction
|
||||
// justification:
|
||||
// xy == 1 (mod m)
|
||||
// xy = 1+km
|
||||
// xy(2-xy) = (1+km)(1-km)
|
||||
// x[y(2-xy)] = 1-k^2m^2
|
||||
// x[y(2-xy)] == 1 (mod m^2)
|
||||
// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
|
||||
// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
|
||||
// JS multiply "overflows" differently from C/C++, so care is needed here.
|
||||
function bnpInvDigit() {
|
||||
if(this.t < 1) return 0;
|
||||
var x = this[0];
|
||||
if((x&1) == 0) return 0;
|
||||
var y = x&3; // y == 1/x mod 2^2
|
||||
y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
|
||||
y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
|
||||
y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
|
||||
// last step - calculate inverse mod DV directly;
|
||||
// assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
|
||||
y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
|
||||
// we really want the negative inverse, and -DV < y < DV
|
||||
return (y>0)?this.DV-y:-y;
|
||||
}
|
||||
|
||||
// Montgomery reduction
|
||||
function Montgomery(m) {
|
||||
this.m = m;
|
||||
this.mp = m.invDigit();
|
||||
this.mpl = this.mp&0x7fff;
|
||||
this.mph = this.mp>>15;
|
||||
this.um = (1<<(m.DB-15))-1;
|
||||
this.mt2 = 2*m.t;
|
||||
}
|
||||
|
||||
// xR mod m
|
||||
function montConvert(x) {
|
||||
var r = nbi();
|
||||
x.abs().dlShiftTo(this.m.t,r);
|
||||
r.divRemTo(this.m,null,r);
|
||||
if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
|
||||
return r;
|
||||
}
|
||||
|
||||
// x/R mod m
|
||||
function montRevert(x) {
|
||||
var r = nbi();
|
||||
x.copyTo(r);
|
||||
this.reduce(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
// x = x/R mod m (HAC 14.32)
|
||||
function montReduce(x) {
|
||||
while(x.t <= this.mt2) // pad x so am has enough room later
|
||||
x[x.t++] = 0;
|
||||
for(var i = 0; i < this.m.t; ++i) {
|
||||
// faster way of calculating u0 = x[i]*mp mod DV
|
||||
var j = x[i]&0x7fff;
|
||||
var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
|
||||
// use am to combine the multiply-shift-add into one call
|
||||
j = i+this.m.t;
|
||||
x[j] += this.m.am(0,u0,x,i,0,this.m.t);
|
||||
// propagate carry
|
||||
while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
|
||||
}
|
||||
x.clamp();
|
||||
x.drShiftTo(this.m.t,x);
|
||||
if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
|
||||
}
|
||||
|
||||
// r = "x^2/R mod m"; x != r
|
||||
function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
|
||||
|
||||
// r = "xy/R mod m"; x,y != r
|
||||
function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
|
||||
|
||||
Montgomery.prototype.convert = montConvert;
|
||||
Montgomery.prototype.revert = montRevert;
|
||||
Montgomery.prototype.reduce = montReduce;
|
||||
Montgomery.prototype.mulTo = montMulTo;
|
||||
Montgomery.prototype.sqrTo = montSqrTo;
|
||||
|
||||
// (protected) true iff this is even
|
||||
function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
|
||||
|
||||
// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
|
||||
function bnpExp(e,z) {
|
||||
if(e > 0xffffffff || e < 1) return BigInteger.ONE;
|
||||
var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
|
||||
g.copyTo(r);
|
||||
while(--i >= 0) {
|
||||
z.sqrTo(r,r2);
|
||||
if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
|
||||
else { var t = r; r = r2; r2 = t; }
|
||||
}
|
||||
return z.revert(r);
|
||||
}
|
||||
|
||||
// (public) this^e % m, 0 <= e < 2^32
|
||||
function bnModPowInt(e,m) {
|
||||
var z;
|
||||
if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
|
||||
return this.exp(e,z);
|
||||
}
|
||||
|
||||
// protected
|
||||
BigInteger.prototype.copyTo = bnpCopyTo;
|
||||
BigInteger.prototype.fromInt = bnpFromInt;
|
||||
BigInteger.prototype.fromString = bnpFromString;
|
||||
BigInteger.prototype.clamp = bnpClamp;
|
||||
BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
|
||||
BigInteger.prototype.drShiftTo = bnpDRShiftTo;
|
||||
BigInteger.prototype.lShiftTo = bnpLShiftTo;
|
||||
BigInteger.prototype.rShiftTo = bnpRShiftTo;
|
||||
BigInteger.prototype.subTo = bnpSubTo;
|
||||
BigInteger.prototype.multiplyTo = bnpMultiplyTo;
|
||||
BigInteger.prototype.squareTo = bnpSquareTo;
|
||||
BigInteger.prototype.divRemTo = bnpDivRemTo;
|
||||
BigInteger.prototype.invDigit = bnpInvDigit;
|
||||
BigInteger.prototype.isEven = bnpIsEven;
|
||||
BigInteger.prototype.exp = bnpExp;
|
||||
|
||||
// public
|
||||
BigInteger.prototype.toString = bnToString;
|
||||
BigInteger.prototype.negate = bnNegate;
|
||||
BigInteger.prototype.abs = bnAbs;
|
||||
BigInteger.prototype.compareTo = bnCompareTo;
|
||||
BigInteger.prototype.bitLength = bnBitLength;
|
||||
BigInteger.prototype.mod = bnMod;
|
||||
BigInteger.prototype.modPowInt = bnModPowInt;
|
||||
|
||||
// "constants"
|
||||
BigInteger.ZERO = nbv(0);
|
||||
BigInteger.ONE = nbv(1);
|
||||
|
||||
|
||||
// prng4.js - uses Arcfour as a PRNG
|
||||
|
||||
function Arcfour() {
|
||||
this.i = 0;
|
||||
this.j = 0;
|
||||
this.S = new Array();
|
||||
}
|
||||
|
||||
// Initialize arcfour context from key, an array of ints, each from [0..255]
|
||||
function ARC4init(key) {
|
||||
var i, j, t;
|
||||
for(i = 0; i < 256; ++i)
|
||||
this.S[i] = i;
|
||||
j = 0;
|
||||
for(i = 0; i < 256; ++i) {
|
||||
j = (j + this.S[i] + key[i % key.length]) & 255;
|
||||
t = this.S[i];
|
||||
this.S[i] = this.S[j];
|
||||
this.S[j] = t;
|
||||
}
|
||||
this.i = 0;
|
||||
this.j = 0;
|
||||
}
|
||||
|
||||
function ARC4next() {
|
||||
var t;
|
||||
this.i = (this.i + 1) & 255;
|
||||
this.j = (this.j + this.S[this.i]) & 255;
|
||||
t = this.S[this.i];
|
||||
this.S[this.i] = this.S[this.j];
|
||||
this.S[this.j] = t;
|
||||
return this.S[(t + this.S[this.i]) & 255];
|
||||
}
|
||||
|
||||
Arcfour.prototype.init = ARC4init;
|
||||
Arcfour.prototype.next = ARC4next;
|
||||
|
||||
// Plug in your RNG constructor here
|
||||
function prng_newstate() {
|
||||
return new Arcfour();
|
||||
}
|
||||
|
||||
// Pool size must be a multiple of 4 and greater than 32.
|
||||
// An array of bytes the size of the pool will be passed to init()
|
||||
var rng_psize = 256;
|
||||
|
||||
|
||||
// Random number generator - requires a PRNG backend, e.g. prng4.js
|
||||
|
||||
// For best results, put code like
|
||||
// <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
|
||||
// in your main HTML document.
|
||||
|
||||
var rng_state;
|
||||
var rng_pool;
|
||||
var rng_pptr;
|
||||
|
||||
// Mix in a 32-bit integer into the pool
|
||||
function rng_seed_int(x) {
|
||||
rng_pool[rng_pptr++] ^= x & 255;
|
||||
rng_pool[rng_pptr++] ^= (x >> 8) & 255;
|
||||
rng_pool[rng_pptr++] ^= (x >> 16) & 255;
|
||||
rng_pool[rng_pptr++] ^= (x >> 24) & 255;
|
||||
if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
|
||||
}
|
||||
|
||||
// Mix in the current time (w/milliseconds) into the pool
|
||||
function rng_seed_time() {
|
||||
rng_seed_int(new Date().getTime());
|
||||
}
|
||||
|
||||
// Initialize the pool with junk if needed.
|
||||
if(rng_pool == null) {
|
||||
rng_pool = new Array();
|
||||
rng_pptr = 0;
|
||||
var t;
|
||||
if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {
|
||||
// Extract entropy (256 bits) from NS4 RNG if available
|
||||
var z = window.crypto.random(32);
|
||||
for(t = 0; t < z.length; ++t)
|
||||
rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
|
||||
}
|
||||
while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
|
||||
t = Math.floor(65536 * Math.random());
|
||||
rng_pool[rng_pptr++] = t >>> 8;
|
||||
rng_pool[rng_pptr++] = t & 255;
|
||||
}
|
||||
rng_pptr = 0;
|
||||
rng_seed_time();
|
||||
//rng_seed_int(window.screenX);
|
||||
//rng_seed_int(window.screenY);
|
||||
}
|
||||
|
||||
function rng_get_byte() {
|
||||
if(rng_state == null) {
|
||||
rng_seed_time();
|
||||
rng_state = prng_newstate();
|
||||
rng_state.init(rng_pool);
|
||||
for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
|
||||
rng_pool[rng_pptr] = 0;
|
||||
rng_pptr = 0;
|
||||
//rng_pool = null;
|
||||
}
|
||||
// TODO: allow reseeding after first request
|
||||
return rng_state.next();
|
||||
}
|
||||
|
||||
function rng_get_bytes(ba) {
|
||||
var i;
|
||||
for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
|
||||
}
|
||||
|
||||
function SecureRandom() {}
|
||||
|
||||
SecureRandom.prototype.nextBytes = rng_get_bytes;
|
||||
|
||||
|
||||
// Depends on jsbn.js and rng.js
|
||||
|
||||
// Version 1.1: support utf-8 encoding in pkcs1pad2
|
||||
|
||||
// convert a (hex) string to a bignum object
|
||||
function parseBigInt(str,r) {
|
||||
return new BigInteger(str,r);
|
||||
}
|
||||
|
||||
function linebrk(s,n) {
|
||||
var ret = "";
|
||||
var i = 0;
|
||||
while(i + n < s.length) {
|
||||
ret += s.substring(i,i+n) + "\n";
|
||||
i += n;
|
||||
}
|
||||
return ret + s.substring(i,s.length);
|
||||
}
|
||||
|
||||
function byte2Hex(b) {
|
||||
if(b < 0x10)
|
||||
return "0" + b.toString(16);
|
||||
else
|
||||
return b.toString(16);
|
||||
}
|
||||
|
||||
// PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
|
||||
function pkcs1pad2(s,n) {
|
||||
if(n < s.length + 11) { // TODO: fix for utf-8
|
||||
alert("Message too long for RSA");
|
||||
return null;
|
||||
}
|
||||
var ba = new Array();
|
||||
var i = s.length - 1;
|
||||
while(i >= 0 && n > 0) {
|
||||
var c = s.charCodeAt(i--);
|
||||
if(c < 128) { // encode using utf-8
|
||||
ba[--n] = c;
|
||||
}
|
||||
else if((c > 127) && (c < 2048)) {
|
||||
ba[--n] = (c & 63) | 128;
|
||||
ba[--n] = (c >> 6) | 192;
|
||||
}
|
||||
else {
|
||||
ba[--n] = (c & 63) | 128;
|
||||
ba[--n] = ((c >> 6) & 63) | 128;
|
||||
ba[--n] = (c >> 12) | 224;
|
||||
}
|
||||
}
|
||||
ba[--n] = 0;
|
||||
var rng = new SecureRandom();
|
||||
var x = new Array();
|
||||
while(n > 2) { // random non-zero pad
|
||||
x[0] = 0;
|
||||
while(x[0] == 0) rng.nextBytes(x);
|
||||
ba[--n] = x[0];
|
||||
}
|
||||
ba[--n] = 2;
|
||||
ba[--n] = 0;
|
||||
return new BigInteger(ba);
|
||||
}
|
||||
|
||||
// "empty" RSA key constructor
|
||||
function RSAKey() {
|
||||
this.n = null;
|
||||
this.e = 0;
|
||||
this.d = null;
|
||||
this.p = null;
|
||||
this.q = null;
|
||||
this.dmp1 = null;
|
||||
this.dmq1 = null;
|
||||
this.coeff = null;
|
||||
}
|
||||
|
||||
// Set the public key fields N and e from hex strings
|
||||
function RSASetPublic(N,E) {
|
||||
if(N != null && E != null && N.length > 0 && E.length > 0) {
|
||||
this.n = parseBigInt(N,16);
|
||||
this.e = parseInt(E,16);
|
||||
}
|
||||
else
|
||||
alert("Invalid RSA public key");
|
||||
}
|
||||
|
||||
// Perform raw public operation on "x": return x^e (mod n)
|
||||
function RSADoPublic(x) {
|
||||
return x.modPowInt(this.e, this.n);
|
||||
}
|
||||
|
||||
// Return the PKCS#1 RSA encryption of "text" as an even-length hex string
|
||||
function RSAEncrypt(text) {
|
||||
var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
|
||||
if(m == null) return null;
|
||||
var c = this.doPublic(m);
|
||||
if(c == null) return null;
|
||||
var h = c.toString(16);
|
||||
if((h.length & 1) == 0) return h; else return "0" + h;
|
||||
}
|
||||
|
||||
// Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
|
||||
//function RSAEncryptB64(text) {
|
||||
// var h = this.encrypt(text);
|
||||
// if(h) return hex2b64(h); else return null;
|
||||
//}
|
||||
|
||||
// protected
|
||||
RSAKey.prototype.doPublic = RSADoPublic;
|
||||
|
||||
// public
|
||||
RSAKey.prototype.setPublic = RSASetPublic;
|
||||
RSAKey.prototype.encrypt = RSAEncrypt;
|
||||
//RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
|
Reference in New Issue
Block a user