You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-28
Original file line number
Diff line number
Diff line change
@@ -46,22 +46,23 @@ This library developed and tested primary for Node.js, but it still can work in
46
46
```javascript
47
47
var NodeRSA =require('node-rsa');
48
48
49
-
var key =newNodeRSA([key], [options]);
49
+
var key =newNodeRSA([keyData, [format]], [options]);
50
50
```
51
51
52
-
**key** - parameters of a generated key or the key in PEM format.<br/>
53
-
**options** - additional settings
52
+
* keyData — `{string|buffer|object}` — parameters for generating key or the key in one of supported formats.<br/>
53
+
* format — `{string}` — format for importing key. See more details about formats in [Export/Import](#importexport-keys) section.<br/>
54
+
* options — `{object}` — additional settings.
54
55
55
56
#### Options
56
-
You can specify some options by second constructor argument, or over `key.setOptions()` method.
57
+
You can specify some options by second/third constructor argument, or over `key.setOptions()` method.
57
58
58
-
***environment** - working environment, `'browser'` or `'node'`. Default autodetect.
59
-
***encryptionScheme** - padding scheme for encrypt/decrypt. Can be `'pkcs1_oaep'` or `'pkcs1'`. Default `'pkcs1_oaep'`.
60
-
***signingScheme** - scheme used for signing and verifying. Can be `'pkcs1'` or `'pss'` or 'scheme-hash' format string (eg `'pss-sha1'`). Default `'pkcs1-sha256'`, or, if chosen pss: `'pss-sha1'`.
59
+
* environment — working environment, `'browser'` or `'node'`. Default autodetect.
60
+
* encryptionScheme — padding scheme for encrypt/decrypt. Can be `'pkcs1_oaep'` or `'pkcs1'`. Default `'pkcs1_oaep'`.
61
+
* signingScheme — scheme used for signing and verifying. Can be `'pkcs1'` or `'pss'` or 'scheme-hash' format string (eg `'pss-sha1'`). Default `'pkcs1-sha256'`, or, if chosen pss: `'pss-sha1'`.
61
62
62
63
**Advanced options:**<br/>
63
64
You also can specify advanced options for some schemes like this:
64
-
```
65
+
```javascript
65
66
options = {
66
67
encryptionScheme: {
67
68
scheme:'pkcs1_oaep', //scheme
@@ -78,7 +79,6 @@ options = {
78
79
79
80
This lib supporting next hash algorithms: `'md5'`, `'ripemd160'`, `'sha1'`, `'sha256'`, `'sha512'` in browser and node environment and additional `'md4'`, `'sha'`, `'sha224'`, `'sha384'` in node only.
80
81
81
-
82
82
#### Creating "empty" key
83
83
```javascript
84
84
var key =newNodeRSA();
@@ -89,6 +89,15 @@ var key = new NodeRSA();
89
89
var key =newNodeRSA({b:512});
90
90
```
91
91
92
+
Also you can use next method:
93
+
94
+
```javascript
95
+
key.generateKeyPair([bits], [exp]);
96
+
```
97
+
98
+
* bits — `{int}` — key size in bits. 2048 by default.
99
+
* exp — `{int}` — public exponent. 65537 by default.
100
+
92
101
#### Load key from PEM string
93
102
94
103
```javascript
@@ -103,19 +112,45 @@ var key = new NodeRSA('-----BEGIN RSA PRIVATE KEY-----\n'+
103
112
'-----END RSA PRIVATE KEY-----');
104
113
```
105
114
106
-
Also you can use next methods:
107
-
115
+
### Import/Export keys
108
116
```javascript
109
-
key.generateKeyPair([bits], [exp]);
110
-
key.importKey(pem_string|buffer_contains_pem);
117
+
key.importKey(keyData, [format]);
118
+
key.exportKey([format]);
111
119
```
112
-
**bits** - key size in bits. 2048 by default.
113
-
**exp** - public exponent. 65537 by default.
114
120
115
-
### Export keys
121
+
* keyData — `{string|buffer}` — key in PEM string **or** Buffer contains PEM string **or** Buffer contains DER encoded data.
122
+
* format — `{string}` — format id for export/import.
123
+
124
+
#### Format string syntax
125
+
Format string composed of several parts: `scheme-[key_type]-[output_type]`<br/>
126
+
127
+
Scheme — NodeRSA supports multiple format schemes for import/export keys:
128
+
129
+
*`'pkcs1'` — public key starts from `'-----BEGIN RSA PUBLIC KEY-----'` header and private key starts from `'-----BEGIN RSA PRIVATE KEY-----' header`
130
+
*`'pkcs8'` — public key starts from `'-----BEGIN PUBLIC KEY-----'` header and private key starts from `'-----BEGIN PRIVATE KEY-----' header`
131
+
132
+
Key type — can be `'private'` or `'public'`. Default `'private'`<br/>
133
+
Output type — can be:
134
+
135
+
*`'pem'` — Base64 encoded string with header and footer. Used by default.
136
+
*`'der'` — Binary encoded key data.
137
+
138
+
**Notice:** For import, if *keyData* is PEM string or buffer containing string, you can do not specify format, but if you provide *keyData* as DER you must specify it in format string.
139
+
140
+
**Shortcuts and examples**
141
+
*`'private'` or `'pkcs1'` or `'pkcs1-private'` == `'pkcs1-private-pem'` — private key encoded in pcks1 scheme as pem string.
142
+
*`'public'` or `'pkcs8-public'` == `'pkcs8-public-pem'` — public key encoded in pcks8 scheme as pem string.
143
+
*`'pkcs8'` or `'pkcs8-private'` == `'pkcs8-private-pem'` — private key encoded in pcks8 scheme as pem string.
144
+
*`'pkcs1-der'` == `'pkcs1-private-der'` — private key encoded in pcks1 scheme as binary buffer.
145
+
*`'pkcs8-public-der'` — public key encoded in pcks8 scheme as binary buffer.
146
+
147
+
**Code example**
148
+
116
149
```javascript
117
-
key.exportPrivate();
118
-
key.exportPublic();
150
+
var keyData ='-----BEGIN PUBLIC KEY----- ... -----BEGIN PRIVATE KEY-----';
151
+
key.importKey(keyData, 'pkcs8');
152
+
var publicDer =key.exportKey('pkcs8-public-der');
153
+
var privateDer =key.exportKey('pkcs1-der');
119
154
```
120
155
121
156
### Properties
@@ -125,7 +160,7 @@ key.exportPublic();
125
160
key.isPrivate();
126
161
key.isPublic([strict]);
127
162
```
128
-
**strict** - if true method will return false if key pair have private exponent. Default `false`.
163
+
strict — `{boolean}` — if true method will return false if key pair have private exponent. Default `false`.
129
164
130
165
```javascript
131
166
key.isEmpty();
@@ -149,16 +184,18 @@ Return max data size for encrypt in bytes.
**buffer** - data for encrypting, may be string, Buffer, or any object/array. Arrays and objects will encoded to JSON string first.<br/>
153
-
**encoding** - encoding for output result, may be `'buffer'`, `'binary'`, `'hex'` or `'base64'`. Default `'buffer'`.<br/>
154
-
**source_encoding** - source encoding, works only with string buffer. Can take standard Node.js Buffer encodings (hex, utf8, base64, etc). `'utf8'` by default.<br/>
187
+
188
+
* buffer — `{buffer}` — data for encrypting, may be string, Buffer, or any object/array. Arrays and objects will encoded to JSON string first.<br/>
189
+
* encoding — `{string}` — encoding for output result, may be `'buffer'`, `'binary'`, `'hex'` or `'base64'`. Default `'buffer'`.<br/>
190
+
* source_encoding — `{string}` — source encoding, works only with string buffer. Can take standard Node.js Buffer encodings (hex, utf8, base64, etc). `'utf8'` by default.<br/>
155
191
156
192
```javascript
157
193
key.decrypt(buffer, [encoding]);
158
194
```
159
195
Return decrypted data.<br/>
160
-
**buffer** - data for decrypting. Takes Buffer object or base64 encoded string.<br/>
161
-
**encoding** - encoding for result string. Can also take `'buffer'` for raw Buffer object, or `'json'` for automatic JSON.parse result. Default `'buffer'`.
196
+
197
+
* buffer — `{buffer}` — data for decrypting. Takes Buffer object or base64 encoded string.<br/>
198
+
* encoding — `{string}` — encoding for result string. Can also take `'buffer'` for raw Buffer object, or `'json'` for automatic JSON.parse result. Default `'buffer'`.
162
199
163
200
### Signing/Verifying
164
201
```javascript
@@ -170,17 +207,23 @@ Return signature for buffer. All the arguments are the same as for `encrypt` met
**buffer** - data for check, same as `encrypt` method.<br/>
174
-
**signature** - signature for check, result of `sign` method.<br/>
175
-
**source_encoding** - same as for `encrypt` method.<br/>
176
-
**signature_encoding** - encoding of given signature. May be `'buffer'`, `'binary'`, `'hex'` or `'base64'`. Default `'buffer'`.
210
+
211
+
* buffer — `{buffer}` — data for check, same as `encrypt` method.<br/>
212
+
* signature — `{string}` — signature for check, result of `sign` method.<br/>
213
+
* source_encoding — `{string}` — same as for `encrypt` method.<br/>
214
+
* signature_encoding — `{string}` — encoding of given signature. May be `'buffer'`, `'binary'`, `'hex'` or `'base64'`. Default `'buffer'`.
177
215
178
216
## Contributing
179
217
180
218
Questions, comments, bug reports, and pull requests are all welcome.
181
219
182
220
## Changelog
183
221
222
+
### 0.2.10
223
+
***Methods `.exportPrivate()` and `.exportPublic()` was replaced by `.exportKey([format])`.**
224
+
* By default `.exportKey()` returns private key as `.exportPrivate()`, if you need public key from `.exportPublic()` you must specify format as `'public'` or `'pkcs8-public-pem'`.
225
+
* Method `.importKey(key, [format])` now has second argument.
226
+
184
227
### 0.2.0
185
228
***`.getPublicPEM()` method was renamed to `.exportPublic()`**
186
229
***`.getPrivatePEM()` method was renamed to `.exportPrivate()`**
0 commit comments