forked from volschin/node-curve25519
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
33 lines (29 loc) · 795 Bytes
/
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
var binding = require("bindings")("curve");
var basepoint = (function() {
var buf = new Buffer(32);
buf[0] = 9;
for (var i=1; i<32; i++) {
buf[i] = 0;
}
return buf;
})();
exports.makeSecretKey = function(mysecret) {
if (!mysecret instanceof Buffer)
throw 'mysecret must be a Buffer';
if (mysecret.length != 32)
throw 'mysecret must be 32 bytes long';
mysecret[0] &= 248;
mysecret[31] &= 127;
mysecret[31] |= 64;
return mysecret;
}
exports.derivePublicKey = function (mysecret) {
var mypublic = new Buffer(32);
binding.curve(mypublic, mysecret, basepoint);
return mypublic;
}
exports.deriveSharedSecret = function (mysecret, hispublic) {
var sharedSecret = new Buffer(32);
binding.curve(sharedSecret, mysecret, hispublic);
return sharedSecret;
}