strong-cryptor
Version:
Strong encryptor and decryptor nodejs
101 lines • 4.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var crypto_1 = __importDefault(require("crypto"));
var fs_1 = __importDefault(require("fs"));
var config_1 = require("../config");
var provider_1 = require("../provider");
var utils_1 = require("../utils");
/**
* encrypt the given text with aes-256-cbc encryption algorithm
* @param data string or buffer that will be encrypted
* @param key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data, please store it in the safe places
* @param encoding string/text encoding you can choose `base64`(default) or `hex`
* @returns return encrypted string with selected encoding
*/
function encryptor(data, key, encoding) {
if (encoding === void 0) { encoding = 'base64'; }
/**
* this expression will create a random Initialization Vector(IV)
* for every encrypted data
*/
var iv = crypto_1.default.randomBytes(config_1.IV_LENGTH);
var cipher = crypto_1.default.createCipheriv(config_1.ALGORITHM, Buffer.from(key), iv);
var encryptedData = cipher.update(data);
encryptedData = Buffer.concat([encryptedData, cipher.final()]);
/**
* combine iv to returned encrypted data, and add the separator to distinguish the (IV)
* data and the encrypted data,
* and add separator to the end of data, this for decrypt purpose
*/
if (encoding === 'base64') {
return utils_1.removeBase64Padding(iv.toString(encoding)) + encryptedData.toString(encoding);
}
else {
return iv.toString(encoding) + encryptedData.toString(encoding);
}
/**
* the result of this function will be like this [IV][EncryptedData]
*/
}
/**
* encrypt the given text with aes-256-cbc encryption algorithm
* @param data string that will be encrypted
* @param key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data, please store it in the safe places
* @param options encryption options, see [[IEncryptionOptions]] for more details
* @returns return encrypted string with selected encoding
*/
function encrypt(data, key, options) {
if (options === void 0) { options = {}; }
if (key.length !== 32) {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
var encryptionCount = options.encryptionCount || 1;
var encoding = options.encoding || 'base64';
var result = '';
for (var i = 1; i <= encryptionCount; i++) {
if (i === 1) {
result = encryptor(data, key, encoding);
continue;
}
result = encryptor(result, key, encoding);
}
if (options.writeToFile) {
fs_1.default.writeFileSync(options.writeToFile, result);
}
return result;
}
exports.encrypt = encrypt;
/**
* encrypt the given text with aes-256-cbc encryption algorithm
* @param pathToFile string of file path
* @param key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data, please store it in the safe places
* @param options encryption options, see [[IEncryptionOptions]] for more details
* @returns return encrypted string with selected encoding
*/
function encryptFile(pathToFile, key, options) {
if (options === void 0) { options = {}; }
if (key.length !== 32) {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
var encryptionCount = options.encryptionCount || 1;
var encoding = options.encoding || 'base64';
// @ts-ignore
var data = Buffer.from(fs_1.default.readFileSync(pathToFile), 'binary');
var result = '';
for (var i = 1; i <= encryptionCount; i++) {
if (i === 1) {
result = encryptor(data, key, encoding);
continue;
}
result = encryptor(result, key, encoding);
}
if (options.writeToFile) {
fs_1.default.writeFileSync(options.writeToFile, result);
}
return result;
}
exports.encryptFile = encryptFile;
//# sourceMappingURL=encryptor.js.map