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
129 lines (100 loc) · 2.97 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
124
125
126
127
128
129
/* 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
*/
const version = require("../version");
function NotImplementedException(message) {
this.message = message;
this.toString = function() { return "Not implemented: "+this.message; };
}
var ALGS = {
};
function NotImplementedException(message) {
this.message = message;
this.toString = function() { return "Not implemented: "+this.message; };
}
function KeyPair() {
this.publicKey = null;
this.secretKey = null;
this.algorithm = null;
this.keysize = null;
};
var _getAlgorithm = function _getAlgorithm() {
return this.algorithm + this.keysize.toString();
};
KeyPair.prototype = {
getAlgorithm: _getAlgorithm
};
exports.register = function(alg, cls) {
ALGS[alg] = cls;
};
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) {
if (!ALGS[obj.algorithm])
throw new NotImplementedException("no such algorithm: " + obj.algorithm);
var pk = new ALGS[obj.algorithm].PublicKey();
pk.algorithm = obj.algorithm;
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) {
if (!ALGS[obj.algorithm])
throw new NotImplementedException("no such algorithm: " + obj.algorithm);
var sk = new ALGS[obj.algorithm].SecretKey();
sk.algorithm = obj.algorithm;
sk.deserializeFromObject(obj);
return sk;
};
SecretKey.deserialize = function(str) {
var obj = JSON.parse(str);
return SecretKey.fromSimpleObject(obj);
};
// this looks at the method name
function dispatchOnDataFormatVersion(coreFunctionName) {
return function() {
var currentVersionString = version.getDataFormatVersion() || 'LEGACY';
currentVersionString = currentVersionString.replace(/\./g, '');
var methodName = "_" + currentVersionString + "_" + coreFunctionName;
// invoke
return this[methodName].apply(this, arguments);
};
}
exports.ALGS = ALGS;
exports.PublicKey = PublicKey;
exports.SecretKey = SecretKey;
exports.KeyPair = KeyPair;
exports.NotImplementedException = NotImplementedException;
exports.dispatchOnDataFormatVersion = dispatchOnDataFormatVersion;