walletlink-e
Version:
WalletLink JavaScript SDK
31 lines (30 loc) • 1.38 kB
JavaScript
;
// Copyright (c) 2018-2020 WalletLink.org <https://www.walletlink.org/>
// Copyright (c) 2018-2020 Coinbase, Inc. <https://www.coinbase.com/>
// Licensed under the Apache License, version 2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decrypt = exports.encrypt = void 0;
const crypto_1 = __importDefault(require("crypto"));
function encrypt(plainText, secret) {
const iv = crypto_1.default.randomBytes(12);
const cipher = crypto_1.default.createCipheriv("aes-256-gcm", Buffer.from(secret, "hex"), iv);
const encrypted = Buffer.concat([
cipher.update(Buffer.from(plainText, "utf8")),
cipher.final()
]);
return Buffer.concat([iv, cipher.getAuthTag(), encrypted]).toString("hex");
}
exports.encrypt = encrypt;
function decrypt(cipherText, secret) {
const buf = Buffer.from(cipherText, "hex");
const iv = buf.slice(0, 12);
const authTag = buf.slice(12, 28);
const encrypted = buf.slice(28);
const decipher = crypto_1.default.createDecipheriv("aes-256-gcm", Buffer.from(secret, "hex"), iv);
decipher.setAuthTag(authTag);
return Buffer.concat([decipher.update(encrypted), decipher.final()]).toString("utf8");
}
exports.decrypt = decrypt;