This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
123 lines (98 loc) · 2.6 KB
/
index.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* baseline objects for all algorithms
*/
var error = require("../error");
var KeySizeNotSupportedError = error.KeySizeNotSupportedError;
var NotImplementedError = error.NotImplementedError;
var ALGS = { };
function KeyPair() {
this.publicKey = null;
this.secretKey = null;
this.algorithm = null;
this.keysize = null;
}
var _getAlgorithm = function _getAlgorithm() {
return this.algorithm.substr(0,2) + this.keysize.toString();
};
KeyPair.prototype = {
getAlgorithm: _getAlgorithm
};
exports.register = function(alg, cls) {
ALGS[alg] = cls;
};
exports.findAlgorithm = function(objOrStr) {
if (typeof objOrStr === 'string') {
objOrStr = { algorithm: objOrStr };
}
objOrStr = extractAlg(objOrStr);
return objOrStr ? ALGS[objOrStr] : undefined;
};
function extractAlg(obj) {
var alg = obj.algorithm;
if (!alg) {
alg = obj.kty;
}
if (!ALGS[alg]) {
throw new NotImplementedError("no such algorithm: " + alg);
}
return alg;
}
function PublicKey() {
}
PublicKey.prototype = {
// produce a ready-to-be-JSON'ed object
toSimpleObject: function() {
var obj = { algorithm: this.algorithm };
this.serializeToObject(obj);
return obj;
},
// ok, JSON'ify it
serialize: function() {
return JSON.stringify(this.toSimpleObject());
},
getAlgorithm : _getAlgorithm
};
PublicKey.fromSimpleObject = function(obj) {
var alg = extractAlg(obj);
var pk = new ALGS[alg].PublicKey();
pk.algorithm = alg;
pk.deserializeFromObject(obj);
return pk;
};
PublicKey.deserialize = function(str) {
var obj = JSON.parse(str);
return PublicKey.fromSimpleObject(obj);
};
function SecretKey() {
}
SecretKey.prototype = {
toSimpleObject: function() {
var obj = { algorithm: this.algorithm };
this.serializeToObject(obj);
return obj;
},
serialize: function() {
return JSON.stringify(this.toSimpleObject());
},
getAlgorithm: _getAlgorithm
};
SecretKey.fromSimpleObject = function(obj) {
var alg = extractAlg(obj);
var sk = new ALGS[alg].SecretKey();
sk.algorithm = alg;
sk.deserializeFromObject(obj);
return sk;
};
SecretKey.deserialize = function(str) {
var obj = JSON.parse(str);
return SecretKey.fromSimpleObject(obj);
};
exports.ALGS = ALGS;
exports.PublicKey = PublicKey;
exports.SecretKey = SecretKey;
exports.KeyPair = KeyPair;
exports.KeySizeNotSupportedError = KeySizeNotSupportedError;
exports.NotImplementedError = NotImplementedError;