@stellot/crypto
Version:
Crypto libraries for front and backend
32 lines (31 loc) • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.decrypt = exports.encrypt = void 0;
const elliptic_1 = require("elliptic");
const js_sha256_1 = require("js-sha256");
const js_sha512_1 = require("js-sha512");
const secret_box_1 = require("@stellot/secret-box");
function encrypt(privKey, pubKey, input, nonce, authenticate = true) {
const key = deriveKey(privKey, pubKey);
return secret_box_1.encrypt(input, key, nonce, authenticate);
}
exports.encrypt = encrypt;
function decrypt(privKey, pubKey, input, nonce, authenticate = true) {
const key = deriveKey(privKey, pubKey);
return secret_box_1.decrypt(input, key, nonce, authenticate);
}
exports.decrypt = decrypt;
const ec = new elliptic_1.eddsa('ed25519');
function deriveKey(privKey, pubKey) {
const scalar = ec.decodeInt(getScalar(privKey));
const point = ec.decodePoint(pubKey.toString('hex'));
const secret = ec.encodePoint(point.mul(scalar));
return Buffer.from(js_sha256_1.sha256.arrayBuffer(secret));
}
function getScalar(seed) {
const hash = js_sha512_1.sha512.array(seed);
hash[0] &= 0xf8;
hash[31] &= 0x3f;
hash[31] |= 0x40;
return hash.slice(0, 32);
}