-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCipher.js
128 lines (90 loc) · 2.36 KB
/
Cipher.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
var PeerCipher = require('./PeerCipher');
var Cipher = module.exports = function(code, name) {
this.code = code;
this.name = name;
};
var proto = Cipher.prototype;
/*** static methods ***/
var _codes = {}, _names = {}, _default;
Cipher.register = function(cipher, isDefault) {
if(_codes[cipher.code])
throw new Error('Cipher code `' + cipher.code + '` already exists');
if(_names[cipher.name])
throw new Error('Cipher name `' + cipher.name + '` already exists');
_codes[cipher.code] = cipher;
_names[cipher.name] = cipher;
if(isDefault || !_default)
_default = cipher;
return cipher;
};
Cipher.deregister = function(cipher) {
if(typeof cipher === 'string')
cipher = _names[cipher];
else if(typeof cipher === 'number')
cipher = _codes[cipher];
delete _codes[cipher.code];
delete _names[cipher.name];
if(_default == cipher) {
for(var i in _codes) {
_default = _codes[i];
break;
}
}
return cipher;
};
Cipher.getDefault = function() {
return _default;
};
Cipher.getByName = function(name) {
if(!_names.hasOwnProperty(name))
throw new Error('Cipher name `' + name + '` not found');
return _names[name];
};
Cipher.getByCode = function(code) {
if(!_codes.hasOwnProperty(code))
throw new Error('Cipher code `' + code + '` not found');
return _codes[code];
};
Cipher.getAllByCode = function() {
var map = {};
for(var i in _codes)
map[i] = _codes[i];
return map;
};
Cipher.getAllByName = function() {
var map = {};
for(var i in _names)
map[i] = _names[i];
return map;
};
/*** public methods ***/
proto.generate = function(callback) {
var self = this;
this._generate(function(err, data, buf) {
if(err) return callback(err);
callback(null, new PeerCipher(self, data), buf);
});
return this;
};
proto.parse = function(buf, isReply) {
throw new Error('Unimplemented method');
};
proto.apply = function(data, parsedData) {
throw new Error('Unimplemented method');
};
proto.generateHmac = function(data) {
throw new Error('Unimplemented method');
};
proto.verifyHmac = function(data, buf) {
throw new Error('Unimplemented method');
};
proto.encrypt = function(buf, data) {
throw new Error('Unimplemented method');
};
proto.decrypt = function(buf, data) {
throw new Error('Unimplemented method');
};
/*** protected methods ***/
proto._generate = function(callback) {
callback(new Error('Unimplemented method'));
};