📄 crypto.js.svn-base
字号:
/* * Copyright (c) 2003-2005 Tom Wu * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * In addition, the following condition applies: * * All redistributions must retain an intact copy of this copyright notice * and disclaimer. */// The code has been adapted for use as a benchmark by Google.var Crypto = new BenchmarkSuite('Crypto', 203037, [ new Benchmark("Encrypt", encrypt), new Benchmark("Decrypt", decrypt)]);// Basic JavaScript BN library - subset useful for RSA encryption.// Bits per digitvar dbits;var BI_DB;var BI_DM;var BI_DV;var BI_FP;var BI_FV;var BI_F1;var BI_F2;// JavaScript engine analysisvar canary = 0xdeadbeefcafe;var j_lm = ((canary&0xffffff)==0xefcafe);// (public) Constructorfunction BigInteger(a,b,c) { this.array = new Array(); 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 BigIntegerfunction 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) { var this_array = this.array; var w_array = w.array; while(--n >= 0) { var v = x*this_array[i++]+w_array[j]+c; c = Math.floor(v/0x4000000); w_array[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 this_array = this.array; var w_array = w.array; var xl = x&0x7fff, xh = x>>15; while(--n >= 0) { var l = this_array[i]&0x7fff; var h = this_array[i++]>>15; var m = xh*l+h*xl; l = xl*l+((m&0x7fff)<<15)+w_array[j]+(c&0x3fffffff); c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); w_array[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 this_array = this.array; var w_array = w.array; var xl = x&0x3fff, xh = x>>14; while(--n >= 0) { var l = this_array[i]&0x3fff; var h = this_array[i++]>>14; var m = xh*l+h*xl; l = xl*l+((m&0x3fff)<<14)+w_array[j]+c; c = (l>>28)+(m>>14)+xh*h; w_array[j++] = l&0xfffffff; } return c;}// This is tailored to VMs with 2-bit tagging. It makes sure// that all the computations stay within the 29 bits available.function am4(i,x,w,j,c,n) { var this_array = this.array; var w_array = w.array; var xl = x&0x1fff, xh = x>>13; while(--n >= 0) { var l = this_array[i]&0x1fff; var h = this_array[i++]>>13; var m = xh*l+h*xl; l = xl*l+((m&0x1fff)<<13)+w_array[j]+c; c = (l>>26)+(m>>13)+xh*h; w_array[j++] = l&0x3ffffff; } return c;}// am3/28 is best for SM, Rhino, but am4/26 is best for v8.// Kestrel (Opera 9.5) gets its best result with am4/26.// IE7 does 9% better with am3/28 than with am4/26.// Firefox (SM) gets 10% faster with am3/28 than with am4/26.setupEngine = function(fn, bits) { BigInteger.prototype.am = fn; dbits = bits; BI_DB = dbits; BI_DM = ((1<<dbits)-1); BI_DV = (1<<dbits); BI_FP = 52; BI_FV = Math.pow(2,BI_FP); BI_F1 = BI_FP-dbits; BI_F2 = 2*dbits-BI_FP;}// Digit conversionsvar 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 rfunction bnpCopyTo(r) { var this_array = this.array; var r_array = r.array; for(var i = this.t-1; i >= 0; --i) r_array[i] = this_array[i]; r.t = this.t; r.s = this.s;}// (protected) set from integer value x, -DV <= x < DVfunction bnpFromInt(x) { var this_array = this.array; this.t = 1; this.s = (x<0)?-1:0; if(x > 0) this_array[0] = x; else if(x < -1) this_array[0] = x+DV; else this.t = 0;}// return bigint initialized to valuefunction nbv(i) { var r = nbi(); r.fromInt(i); return r; }// (protected) set from string and radixfunction bnpFromString(s,b) { var this_array = this.array; 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_array[this.t++] = x; else if(sh+k > BI_DB) { this_array[this.t-1] |= (x&((1<<(BI_DB-sh))-1))<<sh; this_array[this.t++] = (x>>(BI_DB-sh)); } else this_array[this.t-1] |= x<<sh; sh += k; if(sh >= BI_DB) sh -= BI_DB; } if(k == 8 && (s[0]&0x80) != 0) { this.s = -1; if(sh > 0) this_array[this.t-1] |= ((1<<(BI_DB-sh))-1)<<sh; } this.clamp(); if(mi) BigInteger.ZERO.subTo(this,this);}// (protected) clamp off excess high wordsfunction bnpClamp() { var this_array = this.array; var c = this.s&BI_DM; while(this.t > 0 && this_array[this.t-1] == c) --this.t;}// (public) return string representation in given radixfunction bnToString(b) { var this_array = this.array; 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 = BI_DB-(i*BI_DB)%k; if(i-- > 0) { if(p < BI_DB && (d = this_array[i]>>p) > 0) { m = true; r = int2char(d); } while(i >= 0) { if(p < k) { d = (this_array[i]&((1<<p)-1))<<(k-p); d |= this_array[--i]>>(p+=BI_DB-k); } else { d = (this_array[i]>>(p-=k))&km; if(p <= 0) { p += BI_DB; --i; } } if(d > 0) m = true; if(m) r += int2char(d); } } return m?r:"0";}// (public) -thisfunction 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 equalfunction bnCompareTo(a) { var this_array = this.array; var a_array = a.array; 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_array[i]-a_array[i]) != 0) return r; return 0;}// returns bit length of the integer xfunction 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() { var this_array = this.array; if(this.t <= 0) return 0; return BI_DB*(this.t-1)+nbits(this_array[this.t-1]^(this.s&BI_DM));}// (protected) r = this << n*DBfunction bnpDLShiftTo(n,r) { var this_array = this.array; var r_array = r.array; var i; for(i = this.t-1; i >= 0; --i) r_array[i+n] = this_array[i]; for(i = n-1; i >= 0; --i) r_array[i] = 0; r.t = this.t+n; r.s = this.s;}// (protected) r = this >> n*DBfunction bnpDRShiftTo(n,r) { var this_array = this.array; var r_array = r.array; for(var i = n; i < this.t; ++i) r_array[i-n] = this_array[i]; r.t = Math.max(this.t-n,0); r.s = this.s;}// (protected) r = this << nfunction bnpLShiftTo(n,r) { var this_array = this.array; var r_array = r.array; var bs = n%BI_DB; var cbs = BI_DB-bs; var bm = (1<<cbs)-1; var ds = Math.floor(n/BI_DB), c = (this.s<<bs)&BI_DM, i; for(i = this.t-1; i >= 0; --i) { r_array[i+ds+1] = (this_array[i]>>cbs)|c; c = (this_array[i]&bm)<<bs; } for(i = ds-1; i >= 0; --i) r_array[i] = 0; r_array[ds] = c; r.t = this.t+ds+1; r.s = this.s; r.clamp();}// (protected) r = this >> nfunction bnpRShiftTo(n,r) { var this_array = this.array; var r_array = r.array; r.s = this.s; var ds = Math.floor(n/BI_DB); if(ds >= this.t) { r.t = 0; return; } var bs = n%BI_DB; var cbs = BI_DB-bs; var bm = (1<<bs)-1; r_array[0] = this_array[ds]>>bs; for(var i = ds+1; i < this.t; ++i) { r_array[i-ds-1] |= (this_array[i]&bm)<<cbs; r_array[i-ds] = this_array[i]>>bs; } if(bs > 0) r_array[this.t-ds-1] |= (this.s&bm)<<cbs; r.t = this.t-ds; r.clamp();}// (protected) r = this - afunction bnpSubTo(a,r) { var this_array = this.array; var r_array = r.array; var a_array = a.array; var i = 0, c = 0, m = Math.min(a.t,this.t); while(i < m) { c += this_array[i]-a_array[i]; r_array[i++] = c&BI_DM; c >>= BI_DB; } if(a.t < this.t) { c -= a.s; while(i < this.t) { c += this_array[i]; r_array[i++] = c&BI_DM; c >>= BI_DB; } c += this.s; } else { c += this.s; while(i < a.t) { c -= a_array[i]; r_array[i++] = c&BI_DM; c >>= BI_DB; } c -= a.s; } r.s = (c<0)?-1:0; if(c < -1) r_array[i++] = BI_DV+c; else if(c > 0) r_array[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 this_array = this.array; var r_array = r.array; var x = this.abs(), y = a.abs(); var y_array = y.array; var i = x.t; r.t = i+y.t; while(--i >= 0) r_array[i] = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -