persistent-storage
Version:
Abstracts access to any storage object implementing the webstorage Storage interface, offering optional compression using lz-string and optional encryption using crypto
91 lines • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var crypto = require('crypto');
function generateSalt(lengthBytes) {
return crypto.randomBytes(lengthBytes).toString('hex');
}
exports.generateSalt = generateSalt;
function generateIV(lengthBytes) {
return crypto.randomBytes(lengthBytes);
}
exports.generateIV = generateIV;
var KeyDerivationOptions = (function () {
function KeyDerivationOptions(opts) {
this.salt = '';
this.iterations = 10;
this.derivedKeyLength = 16;
this.digest = 'sha512';
this.password = opts.password;
this.salt = opts.salt || this.salt;
this.iterations = opts.iterations || this.iterations;
this.derivedKeyLength = opts.derivedKeyLength || this.derivedKeyLength;
this.digest = opts.digest || this.digest;
}
return KeyDerivationOptions;
}());
exports.KeyDerivationOptions = KeyDerivationOptions;
function deriveEncryptionKey(options) {
return crypto.pbkdf2Sync(options.password, options.salt, options.iterations, options.derivedKeyLength, options.digest).toString('hex');
}
exports.deriveEncryptionKey = deriveEncryptionKey;
function encryptUtf8(str, options) {
return encrypt(str, 'utf8', options);
}
exports.encryptUtf8 = encryptUtf8;
function encryptUcs2(str, options) {
return encrypt(str, 'ucs2', options);
}
exports.encryptUcs2 = encryptUcs2;
function decryptUtf8(str, options) {
return decrypt(str, 'utf8', options);
}
exports.decryptUtf8 = decryptUtf8;
function decryptUcs2(str, options) {
return decrypt(str, 'ucs2', options);
}
exports.decryptUcs2 = decryptUcs2;
var MIN_CHUNK_SIZE = 32;
var chunkSize = 512;
function encrypt(str, srcEncoding, options) {
var cipher = crypto.createCipheriv(options.algorithm, options.key, options.iv);
var result = '', elapsed = 0, chunkStart = 0, chunk;
while (str.length > 0) {
if (chunkStart) {
//only want to adapt the chunkSize if we are processing something bigger than a single chunk
if (elapsed > 5 && chunkSize > MIN_CHUNK_SIZE) {
chunkSize /= 2;
}
else if (elapsed === 0) {
chunkSize *= 2;
}
}
chunkStart = Date.now();
chunk = str.slice(0, chunkSize);
str = str.slice(chunkSize);
result += cipher.update(chunk, srcEncoding).toString('ucs2');
elapsed = Date.now() - chunkStart;
}
return result + cipher.final().toString('ucs2');
}
function decrypt(str, destEncoding, options) {
var decipher = crypto.createDecipheriv(options.algorithm, options.key, options.iv);
var result = '', elapsed = 0, chunkStart = 0, chunk;
while (str.length > 0) {
if (chunkStart) {
//only want to adapt the chunkSize if we are processing something bigger than a single chunk
if (elapsed > 5 && chunkSize > MIN_CHUNK_SIZE) {
chunkSize /= 2;
}
else if (elapsed === 0) {
chunkSize *= 2;
}
}
chunkStart = Date.now();
chunk = str.slice(0, chunkSize);
str = str.slice(chunkSize);
result += decipher.update(chunk, 'ucs2', destEncoding);
elapsed = Date.now() - chunkStart;
}
return result + decipher.final(destEncoding);
}
//# sourceMappingURL=crypto.js.map