summaryrefslogtreecommitdiff
path: root/frontend/delta/js/Clipperz/Crypto/RSA.js
Unidiff
Diffstat (limited to 'frontend/delta/js/Clipperz/Crypto/RSA.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/Clipperz/Crypto/RSA.js146
1 files changed, 146 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/Crypto/RSA.js b/frontend/delta/js/Clipperz/Crypto/RSA.js
new file mode 100644
index 0000000..5a480f1
--- a/dev/null
+++ b/frontend/delta/js/Clipperz/Crypto/RSA.js
@@ -0,0 +1,146 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24try { if (typeof(Clipperz.Crypto.BigInt) == 'undefined') { throw ""; }} catch (e) {
25 throw "Clipperz.Crypto.RSA depends on Clipperz.Crypto.BigInt!";
26}
27
28if (typeof(Clipperz.Crypto.RSA) == 'undefined') { Clipperz.Crypto.RSA = {}; }
29
30Clipperz.Crypto.RSA.VERSION = "0.1";
31Clipperz.Crypto.RSA.NAME = "Clipperz.RSA";
32
33//#############################################################################
34
35MochiKit.Base.update(Clipperz.Crypto.RSA, {
36
37 //-------------------------------------------------------------------------
38
39 'publicKeyWithValues': function (e, d, n) {
40 varresult;
41
42 result = {};
43
44 if (e.isBigInt) {
45 result.e = e;
46 } else {
47 result.e = new Clipperz.Crypto.BigInt(e, 16);
48 }
49
50 if (d.isBigInt) {
51 result.d = d;
52 } else {
53 result.d = new Clipperz.Crypto.BigInt(d, 16);
54 }
55
56 if (n.isBigInt) {
57 result.n = n;
58 } else {
59 result.n = new Clipperz.Crypto.BigInt(n, 16);
60 }
61
62 return result;
63 },
64
65 'privateKeyWithValues': function(e, d, n) {
66 return Clipperz.Crypto.RSA.publicKeyWithValues(e, d, n);
67 },
68
69 //-----------------------------------------------------------------------------
70
71 'encryptUsingPublicKey': function (aKey, aMessage) {
72 varmessageValue;
73 varresult;
74
75 messageValue = new Clipperz.Crypto.BigInt(aMessage, 16);
76 result = messageValue.powerModule(aKey.e, aKey.n);
77
78 return result.asString(16);
79 },
80
81 //.............................................................................
82
83 'decryptUsingPublicKey': function (aKey, aMessage) {
84 return Clipperz.Crypto.RSA.encryptUsingPublicKey(aKey, aMessage);
85 },
86
87 //-----------------------------------------------------------------------------
88
89 'encryptUsingPrivateKey': function (aKey, aMessage) {
90 varmessageValue;
91 varresult;
92
93 messageValue = new Clipperz.Crypto.BigInt(aMessage, 16);
94 result = messageValue.powerModule(aKey.d, aKey.n);
95
96 return result.asString(16);
97 },
98
99 //.............................................................................
100
101 'decryptUsingPrivateKey': function (aKey, aMessage) {
102 return Clipperz.Crypto.RSA.encryptUsingPrivateKey(aKey, aMessage);
103 },
104
105 //-----------------------------------------------------------------------------
106
107 'generatePublicKey': function(aNumberOfBits) {
108 varresult;
109 vare;
110 vard;
111 varn;
112
113 e = new Clipperz.Crypto.BigInt("10001", 16);
114
115 {
116 var p, q;
117 varphi;
118
119 do {
120 p = Clipperz.Crypto.BigInt.randomPrime(aNumberOfBits);
121 } while (p.module(e).equals(1));
122
123 do {
124 q = Clipperz.Crypto.BigInt.randomPrime(aNumberOfBits);
125 } while ((q.equals(p)) || (q.module(e).equals(1)));
126
127 n = p.multiply(q);
128 phi = (p.subtract(1).multiply(q.subtract(1)));
129 d = e.powerModule(-1, phi);
130 }
131
132 result = Clipperz.Crypto.RSA.publicKeyWithValues(e, d, n);
133
134 return result;
135 },
136
137 //-------------------------------------------------------------------------
138
139 __syntaxFix__: "syntax fix"
140
141 //-------------------------------------------------------------------------
142
143});
144
145//#############################################################################
146