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
+50-14Lines changed: 50 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,18 +46,19 @@ 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 of a generated 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** section<br/>
54
+
**options** — `{object}` — additional settings
54
55
55
56
#### Options
56
57
You can specify some options by second 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:
@@ -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();
@@ -107,16 +107,48 @@ Also you can use next methods:
107
107
108
108
```javascript
109
109
key.generateKeyPair([bits], [exp]);
110
-
key.importKey(pem_string|buffer_contains_pem);
111
110
```
112
-
**bits**- key size in bits. 2048 by default.
113
-
**exp**- public exponent. 65537 by default.
111
+
**bits**— `{int}` — key size in bits. 2048 by default.
112
+
**exp**— `{int}` — public exponent. 65537 by default.
114
113
115
-
### Export keys
114
+
### Import/Export keys
116
115
```javascript
117
-
key.exportPrivate();
118
-
key.exportPublic();
116
+
key.importKey(keyData, [format]);
117
+
key.exportKey([format]);
118
+
```
119
+
**keyData** — `{string|buffer}` — key in PEM string **or** Buffer contains PEM string **or** Buffer contains DER encoded data.
120
+
**format** — `{string}` — format id for export/import.
121
+
122
+
#### Format string syntax
123
+
Format string composed of several parts: `scheme-[key_type]-[output_type]`
124
+
125
+
***Scheme** — NodeRSA supports multiple format schemes for import/export keys:
126
+
*`'pkcs1'` — public key starts from `'-----BEGIN RSA PUBLIC KEY-----'` header and private key starts from `'-----BEGIN RSA PRIVATE KEY-----' header`
127
+
*`'pkcs8'` — public key starts from `'-----BEGIN PUBLIC KEY-----'` header and private key starts from `'-----BEGIN PRIVATE KEY-----' header`
128
+
***Key type** — can be `'private'` or `'public'`. Default `'private'`
129
+
***Output type** — can be:
130
+
*`'pem'` — Base64 encoded string with header and footer. Used by default.
131
+
*`'der'` — Binary encoded key data.
132
+
133
+
Notice: If you provide **keyData** as DER you must specify it in format string.
134
+
135
+
136
+
**Shortcuts and examples**
137
+
*`'private'` or `'pkcs1'` or `'pkcs1-private'` — `'pkcs1-private-pem'` — private key encoded in pcks1 scheme as pem string.
138
+
*`'public'` or `'pkcs8-public'` — `'pkcs8-public-pem'` — public key encoded in pcks8 scheme as pem string.
139
+
*`'pkcs8'` or `'pkcs8-private'` — `'pkcs8-private-pem'` — private key encoded in pcks8 scheme as pem string.
140
+
*`'pkcs1-der'` — `'pkcs1-private-der'` — private key encoded in pcks1 scheme as binary buffer.
141
+
*`'pkcs8-public-der'` — public key encoded in pcks8 scheme as binary buffer.
142
+
143
+
**Code example**
144
+
119
145
```
146
+
var keyData = '-----BEGIN PUBLIC KEY-----' + .... + '-----BEGIN PRIVATE KEY-----';
147
+
key.importKey(keyData, 'pkcs8');
148
+
var publicDer = key.exportKey('pkcs8-public-der');
149
+
var privateDer = key.exportKey('pkcs1-der');
150
+
```
151
+
120
152
121
153
### Properties
122
154
@@ -125,7 +157,7 @@ key.exportPublic();
125
157
key.isPrivate();
126
158
key.isPublic([strict]);
127
159
```
128
-
**strict**- if true method will return false if key pair have private exponent. Default `false`.
160
+
**strict**— `{boolean}` if true method will return false if key pair have private exponent. Default `false`.
129
161
130
162
```javascript
131
163
key.isEmpty();
@@ -181,6 +213,10 @@ Questions, comments, bug reports, and pull requests are all welcome.
181
213
182
214
## Changelog
183
215
216
+
### 0.2.10
217
+
***Methods `.exportPrivate()` and `.exportPublic()` was replaced by `.exportKey([format])`. 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'`.**
218
+
* Method `.importKey(key, [format])` now has second argument.
219
+
184
220
### 0.2.0
185
221
***`.getPublicPEM()` method was renamed to `.exportPublic()`**
186
222
***`.getPrivatePEM()` method was renamed to `.exportPrivate()`**
0 commit comments