Innovenergy_trunk/frontend/node_modules/testcafe-safe-storage/lib/crypto.js

98 lines
9.0 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = __importDefault(require("crypto"));
const util_1 = require("util");
const file_js_1 = require("./file.js");
const ASYMMETRIC_KEYS_ALGO = 'rsa';
const ASYMMETRIC_KEYS_CIPHER = 'aes-256-cbc';
const ASYMMETRIC_KEYS_LENGTH = 4096;
const ASYMMETRIC_KEYS_FORMAT = {
format: 'pem',
type: 'pkcs8'
};
const SYMMETRIC_ALGO = 'aes-256-gcm';
// NOTE: AES-256 requires a 256 bit key. 256 bits === 32 bytes
const PASSPHRASE_SIZE = 32;
const generateKeyPair = (0, util_1.promisify)(crypto_1.default.generateKeyPair);
const randomBytes = (0, util_1.promisify)(crypto_1.default.randomBytes);
const scrypt = (0, util_1.promisify)(crypto_1.default.scrypt);
class CryptoContext {
async _generateNonce() {
return await randomBytes(PASSPHRASE_SIZE);
}
async _generateKeyPair() {
return await generateKeyPair(ASYMMETRIC_KEYS_ALGO, { modulusLength: ASYMMETRIC_KEYS_LENGTH });
}
async _generatePassphrase(nonce) {
const bytes = await randomBytes(PASSPHRASE_SIZE);
return await scrypt(bytes, nonce, PASSPHRASE_SIZE);
}
async _saveNonce(nonce) {
await (0, file_js_1.save)(file_js_1.FILE_TYPE.NONCE, nonce);
}
async _savePrivateKey(privateKey, nonce) {
const data = privateKey.export(Object.assign({ cipher: ASYMMETRIC_KEYS_CIPHER, passphrase: nonce }, ASYMMETRIC_KEYS_FORMAT));
await (0, file_js_1.save)(file_js_1.FILE_TYPE.PRIVATE_KEY, data);
}
async _savePassphrase(passphrase, publicKey) {
await (0, file_js_1.save)(file_js_1.FILE_TYPE.PASSPHRASE, crypto_1.default.publicEncrypt(publicKey, passphrase));
}
async _saveAuthTag(authTag) {
await (0, file_js_1.save)(file_js_1.FILE_TYPE.AUTH_TAG, authTag);
}
async _loadAuthTag() {
return await (0, file_js_1.load)(file_js_1.FILE_TYPE.AUTH_TAG);
}
async _loadNonce() {
return await (0, file_js_1.load)(file_js_1.FILE_TYPE.NONCE);
}
async _loadPrivateKey(nonce) {
const data = await (0, file_js_1.load)(file_js_1.FILE_TYPE.PRIVATE_KEY);
return crypto_1.default.createPrivateKey(Object.assign({ key: data, passphrase: nonce }, ASYMMETRIC_KEYS_FORMAT));
}
async _loadPassphrase(privateKey) {
const data = await (0, file_js_1.load)(file_js_1.FILE_TYPE.PASSPHRASE);
return crypto_1.default.privateDecrypt(privateKey, data);
}
async _generateEncryptionKeys() {
const nonce = await this._generateNonce();
const passphrase = await this._generatePassphrase(nonce);
const { publicKey, privateKey } = await this._generateKeyPair();
await this._saveNonce(nonce);
await this._savePrivateKey(privateKey, nonce);
await this._savePassphrase(passphrase, publicKey);
return { nonce, passphrase };
}
async _loadDecryptionKeys() {
const authTag = await this._loadAuthTag();
const nonce = await this._loadNonce();
const privateKey = await this._loadPrivateKey(nonce);
const passphrase = await this._loadPassphrase(privateKey);
return { authTag, nonce, passphrase };
}
async _createCipher() {
const { nonce, passphrase } = await this._generateEncryptionKeys();
return crypto_1.default.createCipheriv(SYMMETRIC_ALGO, passphrase, nonce);
}
async _createDecipher() {
const { nonce, passphrase, authTag } = await this._loadDecryptionKeys();
const decipher = await crypto_1.default.createDecipheriv(SYMMETRIC_ALGO, passphrase, nonce);
decipher.setAuthTag(authTag);
return decipher;
}
async encrypt(data) {
const cipher = await this._createCipher();
const result = Buffer.concat([cipher.update(data), cipher.final()]);
await this._saveAuthTag(cipher.getAuthTag());
return result;
}
async decrypt(data) {
const decipher = await this._createDecipher();
return Buffer.concat([decipher.update(data), decipher.final()]);
}
}
exports.default = CryptoContext;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3J5cHRvLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3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