ecdh-es
Version:
Elliptic Curve Diffie-Hellman with ephemeral-static keys implementation for NodeJS
76 lines (62 loc) • 1.89 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var BigInt, buff_eq, createHash, createHmac, get_pub, hmac, rand, rand_key, randomBuffer, reader, sha256, sha512, _ref,
__slice = [].slice;
BigInt = require('bigi');
_ref = require('crypto'), createHash = _ref.createHash, createHmac = _ref.createHmac;
randomBuffer = require('secure-random').randomBuffer;
reader = function(buff, pos) {
if (pos == null) {
pos = 0;
}
return function(len) {
if (len != null) {
return buff.slice(pos, (pos += len));
} else {
return buff.slice(pos);
}
};
};
sha256 = function(d) {
return createHash('sha256').update(d).digest();
};
sha512 = function(d) {
return createHash('sha512').update(d).digest();
};
hmac = function() {
var d, data, h, key, _i, _len;
key = arguments[0], data = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
h = createHmac('sha256', key);
for (_i = 0, _len = data.length; _i < _len; _i++) {
d = data[_i];
h.update(d);
}
return h.digest();
};
rand = function() {
var entropy;
entropy = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return hmac.apply(null, [randomBuffer(32)].concat(__slice.call(entropy)));
};
rand_key = function() {
var curve, entropy;
curve = arguments[0], entropy = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return BigInt.fromBuffer(rand.apply(null, entropy)).mod(curve.n);
};
get_pub = function(curve, privkey) {
return curve.G.multiply(privkey).getEncoded(true);
};
buff_eq = function(a, b) {
return a.toString('hex') === b.toString('hex');
};
module.exports = {
reader: reader,
sha256: sha256,
sha512: sha512,
hmac: hmac,
rand: rand,
rand_key: rand_key,
get_pub: get_pub,
buff_eq: buff_eq
};
}).call(this);