root / trunk / web / dojo / dojox / encoding / crypto / RSAKey-ext.js
History | View | Annotate | Download (2.4 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.encoding.crypto.RSAKey-ext"]){ |
||
9 | dojo._hasResource["dojox.encoding.crypto.RSAKey-ext"]=true; |
||
10 | dojo.provide("dojox.encoding.crypto.RSAKey-ext");
|
||
11 | dojo.experimental("dojox.encoding.crypto.RSAKey-ext");
|
||
12 | dojo.require("dojox.encoding.crypto.RSAKey");
|
||
13 | dojo.require("dojox.math.BigInteger-ext");
|
||
14 | (function(){
|
||
15 | var _1=dojox.math.BigInteger;
|
||
16 | function _2(d,n){ |
||
17 | var b=d.toByteArray();
|
||
18 | for(var i=0,_3=b.length;i<_3&&!b[i];++i){ |
||
19 | } |
||
20 | if(b.length-i!==n-1||b[i]!==2){ |
||
21 | return null; |
||
22 | } |
||
23 | for(++i;b[i];){
|
||
24 | if(++i>=_3){
|
||
25 | return null; |
||
26 | } |
||
27 | } |
||
28 | var _4=""; |
||
29 | while(++i<_3){
|
||
30 | _4+=String.fromCharCode(b[i]); |
||
31 | } |
||
32 | return _4;
|
||
33 | }; |
||
34 | dojo.extend(dojox.encoding.crypto.RSAKey,{setPrivate:function(N,E,D){ |
||
35 | if(N&&E&&N.length&&E.length){
|
||
36 | this.n=new _1(N,16); |
||
37 | this.e=parseInt(E,16); |
||
38 | this.d=new _1(D,16); |
||
39 | }else{
|
||
40 | throw new Error("Invalid RSA private key"); |
||
41 | } |
||
42 | },setPrivateEx:function(N,E,D,P,Q,DP,DQ,C){ |
||
43 | if(N&&E&&N.length&&E.length){
|
||
44 | this.n=new _1(N,16); |
||
45 | this.e=parseInt(E,16); |
||
46 | this.d=new _1(D,16); |
||
47 | this.p=new _1(P,16); |
||
48 | this.q=new _1(Q,16); |
||
49 | this.dmp1=new _1(DP,16); |
||
50 | this.dmq1=new _1(DQ,16); |
||
51 | this.coeff=new _1(C,16); |
||
52 | }else{
|
||
53 | throw new Error("Invalid RSA private key"); |
||
54 | } |
||
55 | },generate:function(B,E){ |
||
56 | var _5=this.rngf(),qs=B>>1; |
||
57 | this.e=parseInt(E,16); |
||
58 | var ee=new _1(E,16); |
||
59 | for(;;){
|
||
60 | for(;;){
|
||
61 | this.p=new _1(B-qs,1,_5); |
||
62 | if(!this.p.subtract(_1.ONE).gcd(ee).compareTo(_1.ONE)&&this.p.isProbablePrime(10)){ |
||
63 | break;
|
||
64 | } |
||
65 | } |
||
66 | for(;;){
|
||
67 | this.q=new _1(qs,1,_5); |
||
68 | if(!this.q.subtract(_1.ONE).gcd(ee).compareTo(_1.ONE)&&this.q.isProbablePrime(10)){ |
||
69 | break;
|
||
70 | } |
||
71 | } |
||
72 | if(this.p.compareTo(this.q)<=0){ |
||
73 | var t=this.p; |
||
74 | this.p=this.q; |
||
75 | this.q=t;
|
||
76 | } |
||
77 | var p1=this.p.subtract(_1.ONE); |
||
78 | var q1=this.q.subtract(_1.ONE); |
||
79 | var _6=p1.multiply(q1);
|
||
80 | if(!_6.gcd(ee).compareTo(_1.ONE)){
|
||
81 | this.n=this.p.multiply(this.q); |
||
82 | this.d=ee.modInverse(_6);
|
||
83 | this.dmp1=this.d.mod(p1); |
||
84 | this.dmq1=this.d.mod(q1); |
||
85 | this.coeff=this.q.modInverse(this.p); |
||
86 | break;
|
||
87 | } |
||
88 | } |
||
89 | _5.destroy(); |
||
90 | },decrypt:function(_7){ |
||
91 | var c=new _1(_7,16),m; |
||
92 | if(!this.p||!this.q){ |
||
93 | m=c.modPow(this.d,this.n); |
||
94 | }else{
|
||
95 | var cp=c.mod(this.p).modPow(this.dmp1,this.p),cq=c.mod(this.q).modPow(this.dmq1,this.q); |
||
96 | while(cp.compareTo(cq)<0){ |
||
97 | cp=cp.add(this.p);
|
||
98 | } |
||
99 | m=cp.subtract(cq).multiply(this.coeff).mod(this.p).multiply(this.q).add(cq); |
||
100 | } |
||
101 | if(!m){
|
||
102 | return null; |
||
103 | } |
||
104 | return _2(m,(this.n.bitLength()+7)>>3); |
||
105 | }}); |
||
106 | })(); |
||
107 | } |