Skip to content

Commit 9c7d62c

Browse files
committed
formats refact
1 parent b5b7986 commit 9c7d62c

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

src/formats/formats.js

+40-55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
var _ = require('lodash');
2+
3+
function formatParse(format) {
4+
format = format.split('-');
5+
var keyType = 'private';
6+
var keyOpt = {type: 'default'};
7+
8+
for (var i = 1; i < format.length; i++) {
9+
if (format[i]) {
10+
switch (format[i]) {
11+
case 'public':
12+
keyType = format[i];
13+
break;
14+
case 'private':
15+
keyType = format[i];
16+
break;
17+
case 'pem':
18+
keyOpt.type = format[i];
19+
break;
20+
case 'der':
21+
keyOpt.type = format[i];
22+
break;
23+
}
24+
}
25+
}
26+
27+
return {scheme: format[0], keyType: keyType, keyOpt: keyOpt};
28+
}
29+
230
module.exports = {
331
pkcs1: require('./pkcs1'),
432
pkcs8: require('./pkcs8'),
@@ -21,40 +49,18 @@ module.exports = {
2149

2250
detectAndImport: function (key, data, format) {
2351
if (format === undefined) {
24-
for (var format in module.exports) {
25-
if (typeof module.exports[format].autoImport === 'function' && module.exports[format].autoImport(key, data)) {
52+
for (var scheme in module.exports) {
53+
if (typeof module.exports[scheme].autoImport === 'function' && module.exports[scheme].autoImport(key, data)) {
2654
return true;
2755
}
2856
}
2957
} else if (format) {
30-
var fmt = format.split('-');
31-
var keyType = 'private';
32-
var keyOpt = {type: 'default'};
33-
34-
for (var i = 1; i < fmt.length; i++) {
35-
if (fmt[i]) {
36-
switch (fmt[i]) {
37-
case 'public':
38-
keyType = fmt[i];
39-
break;
40-
case 'private':
41-
keyType = fmt[i];
42-
break;
43-
case 'pem':
44-
keyOpt.type = fmt[i];
45-
break;
46-
case 'der':
47-
keyOpt.type = fmt[i];
48-
break;
49-
}
50-
}
51-
}
52-
53-
if (module.exports[fmt[0]]) {
54-
if (keyType === 'private') {
55-
module.exports[fmt[0]].privateImport(key, data, keyOpt);
58+
var fmt = formatParse(format);
59+
if (module.exports[fmt.scheme]) {
60+
if (fmt.keyType === 'private') {
61+
module.exports[fmt.scheme].privateImport(key, data, fmt.keyOpt);
5662
} else {
57-
module.exports[fmt[0]].publicImport(key, data, keyOpt);
63+
module.exports[fmt.scheme].publicImport(key, data, fmt.keyOpt);
5864
}
5965
} else {
6066
throw Error('Unsupported key format');
@@ -66,40 +72,19 @@ module.exports = {
6672

6773
detectAndExport: function (key, format) {
6874
if (format) {
69-
var fmt = format.split('-');
70-
var keyType = 'private';
71-
var keyOpt = {type: 'default'};
72-
73-
for (var i = 1; i < fmt.length; i++) {
74-
if (fmt[i]) {
75-
switch (fmt[i]) {
76-
case 'public':
77-
keyType = fmt[i];
78-
break;
79-
case 'private':
80-
keyType = fmt[i];
81-
break;
82-
case 'pem':
83-
keyOpt.type = fmt[i];
84-
break;
85-
case 'der':
86-
keyOpt.type = fmt[i];
87-
break;
88-
}
89-
}
90-
}
75+
var fmt = formatParse(format);
9176

92-
if (module.exports[fmt[0]]) {
93-
if (keyType === 'private') {
77+
if (module.exports[fmt.scheme]) {
78+
if (fmt.keyType === 'private') {
9479
if (!key.isPrivate()) {
9580
throw Error("It is not private key");
9681
}
97-
return module.exports[fmt[0]].privateExport(key, keyOpt);
82+
return module.exports[fmt.scheme].privateExport(key, fmt.keyOpt);
9883
} else {
9984
if (!key.isPublic()) {
10085
throw Error("It is not public key");
10186
}
102-
return module.exports[fmt[0]].publicExport(key, keyOpt);
87+
return module.exports[fmt.scheme].publicExport(key, fmt.keyOpt);
10388
}
10489
} else {
10590
throw Error('Unsupported key format');

test/tests.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,18 @@ describe("NodeRSA", function(){
235235
'pkcs8-public-pem': {public: true, der: false, file: 'public_pkcs8.pem'},
236236

237237
'private': {public: false, der: false, file: 'private_pkcs1.pem'},
238-
'public': {public: true, der: false, file: 'public_pkcs8.pem'}
238+
'public': {public: true, der: false, file: 'public_pkcs8.pem'},
239+
'private-der': {public: false, der: true, file: 'private_pkcs1.der'},
240+
'public-der': {public: true, der: true, file: 'public_pkcs8.der'},
241+
242+
'pkcs1': {public: false, der: false, file: 'private_pkcs1.pem'},
243+
'pkcs1-private': {public: false, der: false, file: 'private_pkcs1.pem'},
244+
'pkcs1-der': {public: false, der: true, file: 'private_pkcs1.der'},
245+
'pkcs8': {public: false, der: false, file: 'private_pkcs8.pem'},
246+
'pkcs8-private': {public: false, der: false, file: 'private_pkcs8.pem'},
247+
'pkcs8-der': {public: false, der: true, file: 'private_pkcs8.der'},
248+
'pkcs1-public': {public: true, der: false, file: 'public_pkcs1.pem'},
249+
'pkcs8-public': {public: true, der: false, file: 'public_pkcs8.pem'}
239250
};
240251

241252
describe("Good cases", function () {

0 commit comments

Comments
 (0)