-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
JSEncrypt.ts
218 lines (203 loc) · 7.31 KB
/
JSEncrypt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { b64tohex, hex2b64 } from "./lib/jsbn/base64";
import { JSEncryptRSAKey } from "./JSEncryptRSAKey";
const version = process.env.npm_package_version;
export interface IJSEncryptOptions {
default_key_size?: string;
default_public_exponent?: string;
log?: boolean;
}
/**
*
* @param {Object} [options = {}] - An object to customize JSEncrypt behaviour
* possible parameters are:
* - default_key_size {number} default: 1024 the key size in bit
* - default_public_exponent {string} default: '010001' the hexadecimal representation of the public exponent
* - log {boolean} default: false whether log warn/error or not
* @constructor
*/
export class JSEncrypt {
constructor(options: IJSEncryptOptions = {}) {
options = options || {};
this.default_key_size = options.default_key_size
? parseInt(options.default_key_size, 10)
: 1024;
this.default_public_exponent = options.default_public_exponent || "010001"; // 65537 default openssl public exponent for rsa key type
this.log = options.log || false;
// The private and public key.
this.key = null;
}
private default_key_size: number;
private default_public_exponent: string;
private log: boolean;
private key: JSEncryptRSAKey;
public static version: string = version;
/**
* Method to set the rsa key parameter (one method is enough to set both the public
* and the private key, since the private key contains the public key paramenters)
* Log a warning if logs are enabled
* @param {Object|string} key the pem encoded string or an object (with or without header/footer)
* @public
*/
public setKey(key: string) {
if (this.log && this.key) {
console.warn("A key was already set, overriding existing.");
}
this.key = new JSEncryptRSAKey(key);
}
/**
* Proxy method for setKey, for api compatibility
* @see setKey
* @public
*/
public setPrivateKey(privkey: string) {
// Create the key.
this.setKey(privkey);
}
/**
* Proxy method for setKey, for api compatibility
* @see setKey
* @public
*/
public setPublicKey(pubkey: string) {
// Sets the public key.
this.setKey(pubkey);
}
/**
* Proxy method for RSAKey object's decrypt, decrypt the string using the private
* components of the rsa key object. Note that if the object was not set will be created
* on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
* @param {string} str base64 encoded crypted string to decrypt
* @return {string} the decrypted string
* @public
*/
public decrypt(str: string) {
// Return the decrypted string.
try {
return this.getKey().decrypt(b64tohex(str));
} catch (ex) {
return false;
}
}
/**
* Proxy method for RSAKey object's encrypt, encrypt the string using the public
* components of the rsa key object. Note that if the object was not set will be created
* on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
* @param {string} str the string to encrypt
* @return {string} the encrypted string encoded in base64
* @public
*/
public encrypt(str: string) {
// Return the encrypted string.
try {
return hex2b64(this.getKey().encrypt(str));
} catch (ex) {
return false;
}
}
/**
* Proxy method for RSAKey object's sign.
* @param {string} str the string to sign
* @param {function} digestMethod hash method
* @param {string} digestName the name of the hash algorithm
* @return {string} the signature encoded in base64
* @public
*/
public sign(
str: string,
digestMethod: (str: string) => string,
digestName: string,
): string | false {
// return the RSA signature of 'str' in 'hex' format.
try {
return hex2b64(this.getKey().sign(str, digestMethod, digestName));
} catch (ex) {
return false;
}
}
/**
* Proxy method for RSAKey object's verify.
* @param {string} str the string to verify
* @param {string} signature the signature encoded in base64 to compare the string to
* @param {function} digestMethod hash method
* @return {boolean} whether the data and signature match
* @public
*/
public verify(
str: string,
signature: string,
digestMethod: (str: string) => string,
): boolean {
// Return the decrypted 'digest' of the signature.
try {
return this.getKey().verify(str, b64tohex(signature), digestMethod);
} catch (ex) {
return false;
}
}
/**
* Getter for the current JSEncryptRSAKey object. If it doesn't exists a new object
* will be created and returned
* @param {callback} [cb] the callback to be called if we want the key to be generated
* in an async fashion
* @returns {JSEncryptRSAKey} the JSEncryptRSAKey object
* @public
*/
public getKey(cb?: () => void) {
// Only create new if it does not exist.
if (!this.key) {
// Get a new private key.
this.key = new JSEncryptRSAKey();
if (cb && {}.toString.call(cb) === "[object Function]") {
this.key.generateAsync(
this.default_key_size,
this.default_public_exponent,
cb,
);
return;
}
// Generate the key.
this.key.generate(this.default_key_size, this.default_public_exponent);
}
return this.key;
}
/**
* Returns the pem encoded representation of the private key
* If the key doesn't exists a new key will be created
* @returns {string} pem encoded representation of the private key WITH header and footer
* @public
*/
public getPrivateKey() {
// Return the private representation of this key.
return this.getKey().getPrivateKey();
}
/**
* Returns the pem encoded representation of the private key
* If the key doesn't exists a new key will be created
* @returns {string} pem encoded representation of the private key WITHOUT header and footer
* @public
*/
public getPrivateKeyB64() {
// Return the private representation of this key.
return this.getKey().getPrivateBaseKeyB64();
}
/**
* Returns the pem encoded representation of the public key
* If the key doesn't exists a new key will be created
* @returns {string} pem encoded representation of the public key WITH header and footer
* @public
*/
public getPublicKey() {
// Return the private representation of this key.
return this.getKey().getPublicKey();
}
/**
* Returns the pem encoded representation of the public key
* If the key doesn't exists a new key will be created
* @returns {string} pem encoded representation of the public key WITHOUT header and footer
* @public
*/
public getPublicKeyB64() {
// Return the private representation of this key.
return this.getKey().getPublicBaseKeyB64();
}
}