root / trunk / web / dojo / dojox / encoding / crypto / RSAKey.js @ 13
History | View | Annotate | Download (1.42 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"]){ |
||
9 | dojo._hasResource["dojox.encoding.crypto.RSAKey"]=true; |
||
10 | dojo.provide("dojox.encoding.crypto.RSAKey");
|
||
11 | dojo.experimental("dojox.encoding.crypto.RSAKey");
|
||
12 | dojo.require("dojox.math.BigInteger");
|
||
13 | dojo.require("dojox.math.random.Simple");
|
||
14 | (function(){
|
||
15 | var dm=dojox.math,_1=dm.BigInteger,_2=dm.random.Simple,_3=function(){ |
||
16 | return new _2(); |
||
17 | }; |
||
18 | function _4(s,n,_5){ |
||
19 | if(n<s.length+11){ |
||
20 | throw new Error("Message too long for RSA"); |
||
21 | } |
||
22 | var ba=new Array(n); |
||
23 | var i=s.length;
|
||
24 | while(i&&n){
|
||
25 | ba[--n]=s.charCodeAt(--i); |
||
26 | } |
||
27 | ba[--n]=0;
|
||
28 | var _6=_5();
|
||
29 | var x=[0]; |
||
30 | while(n>2){ |
||
31 | x[0]=0; |
||
32 | while(x[0]==0){ |
||
33 | _6.nextBytes(x); |
||
34 | } |
||
35 | ba[--n]=x[0];
|
||
36 | } |
||
37 | ba[--n]=2;
|
||
38 | ba[--n]=0;
|
||
39 | _6.destroy(); |
||
40 | return new _1(ba); |
||
41 | }; |
||
42 | dojo.declare("dojox.encoding.crypto.RSAKey",null,{constructor:function(_7){ |
||
43 | this.rngf=_7||_3;
|
||
44 | this.e=0; |
||
45 | this.n=this.d=this.p=this.q=this.dmp1=this.dmq1=this.coeff=null; |
||
46 | },setPublic:function(N,E){ |
||
47 | if(N&&E&&N.length&&E.length){
|
||
48 | this.n=new _1(N,16); |
||
49 | this.e=parseInt(E,16); |
||
50 | }else{
|
||
51 | throw new Error("Invalid RSA public key"); |
||
52 | } |
||
53 | },encrypt:function(_8){ |
||
54 | var m=_4(_8,(this.n.bitLength()+7)>>3,this.rngf); |
||
55 | if(!m){
|
||
56 | return null; |
||
57 | } |
||
58 | var c=m.modPowInt(this.e,this.n); |
||
59 | if(!c){
|
||
60 | return null; |
||
61 | } |
||
62 | var h=c.toString(16); |
||
63 | return h.length%2?"0"+h:h; |
||
64 | }}); |
||
65 | })(); |
||
66 | } |