strong-cryptor
Version:
Strong encryptor and decryptor nodejs
158 lines • 7.34 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var crypto_1 = __importDefault(require("crypto"));
var fs_1 = __importStar(require("fs"));
var config_1 = require("../config");
var provider_1 = require("../provider/");
var regex_1 = require("../regex");
var utils_1 = require("../utils");
/**
* decrypt the encrypted data from strong-cryptor to string
* @param {string} encryptedData encrypted string
* @param {string} key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data (must be same with the encryption process), please store it in the safe places
* @param {Encoding} encoding string/text encoding you can choose `base64`(default) or `hex` (must be same with the encryption process)
* @returns {string} return decrypted string
*/
function stringDecryptor(encryptedData, key, encoding) {
if (encoding === void 0) { encoding = 'base64'; }
if (key.length !== 32) {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
var _a = utils_1.getIvAndEncryptedDataOnly(encryptedData, encoding), ivString = _a.ivString, encryptedDataString = _a.encryptedDataString;
if (encoding === 'base64' && !regex_1.base64Regex.test(ivString)) {
throw new provider_1.MalformatedError('Encrypted data malformated');
}
else if (encoding === 'hex' && !regex_1.hexRegex.test(ivString)) {
throw new provider_1.MalformatedError('Encrypted data malformated');
}
try {
var iv = Buffer.from(ivString, encoding);
var encryptedText = Buffer.from(encryptedDataString, encoding);
var decipher = crypto_1.default.createDecipheriv(config_1.ALGORITHM, Buffer.from(key), iv);
var decrypted = decipher.update(encryptedText);
var decriptedString = decrypted.toString() + decipher.final().toString();
return decriptedString;
}
catch (error) {
throw new provider_1.MalformatedError('Encrypted data malformated');
}
}
/**
* decrypt the encrypted data from strong-cryptor to Buffer
* @param {string} encryptedData encrypted string
* @param {string} key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data (must be same with the encryption process), please store it in the safe places
* @param {Encoding} encoding string/text encoding you can choose `base64`(default) or `hex` (must be same with the encryption process)
* @returns {Buffer} return decrypted data as a Buffer
*/
function bufferDecryptor(encryptedData, key, encoding) {
if (encoding === void 0) { encoding = 'base64'; }
if (key.length !== 32) {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
var _a = utils_1.getIvAndEncryptedDataOnly(encryptedData, encoding), ivString = _a.ivString, encryptedDataString = _a.encryptedDataString;
if (encoding === 'base64' && !regex_1.base64Regex.test(ivString)) {
throw new provider_1.MalformatedError('Encrypted data malformated');
}
else if (encoding === 'hex' && !regex_1.hexRegex.test(ivString)) {
throw new provider_1.MalformatedError('Encrypted data malformated');
}
try {
var iv = Buffer.from(ivString, encoding);
var encryptedText = Buffer.from(encryptedDataString, encoding);
var decipher = crypto_1.default.createDecipheriv(config_1.ALGORITHM, Buffer.from(key), iv);
var decrypted = decipher.update(encryptedText);
var decryptedBuffer = Buffer.concat([decrypted, decipher.final()]);
return decryptedBuffer;
}
catch (error) {
throw new provider_1.MalformatedError('Encrypted data malformated');
}
}
/**
* decrypt the encrypted data from strong-cryptor
* @param {string} encryptedData encrypted string
* @param {string} key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data (must be same with the encryption process), please store it in the safe places
* @param {IDecryptionOptions} options decryption options, see [[IDecryptionOptions]] for more details
* @returns {string|Buffer} return decrypted string/Buffer
*/
function decrypt(encryptedString, key, options) {
if (options === void 0) { options = {}; }
var encryptionCount = options.encryptionCount || 1;
var data = encryptedString;
var stringResult = '';
var bufferResult = Buffer.from('');
var encoding = options.encoding || 'base64';
for (var i = 1; i <= encryptionCount; i++) {
if (i === encryptionCount && options.toBuffer) {
bufferResult = bufferDecryptor(data, key, encoding);
continue;
}
else if (i === encryptionCount && !options.toBuffer) {
stringResult = stringDecryptor(data, key, encoding);
}
data = stringDecryptor(data, key, encoding);
}
if (options.toBuffer) {
if (options.writeToFile) {
fs_1.default.writeFileSync(options.writeToFile, bufferResult);
}
return bufferResult; // escaping typing error
}
else {
if (options.writeToFile) {
fs_1.default.writeFileSync(options.writeToFile, stringResult);
}
return stringResult; // escaping typing error
}
}
exports.decrypt = decrypt;
/**
* decrypt file that already encrypted from strong-cryptor
* @param {string} filePath path to file
* @param {string} key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data (must be same with the encryption process), please store it in the safe places
* @param {IDecryptionOptions} options decryption options, see [[IDecryptionOptions]] for more details
* @returns {string|Buffer} return decrypted string/Buffer
*/
function decryptFile(filePath, key, options) {
if (options === void 0) { options = {}; }
var encryptionCount = options.encryptionCount || 1;
var stringResult = '';
var bufferResult = Buffer.from('');
var encoding = options.encoding || 'base64';
// @ts-ignore
var data = Buffer.from(fs_1.readFileSync(filePath), encoding).toString();
for (var i = 1; i <= encryptionCount; i++) {
if (i === encryptionCount && options.toBuffer) {
bufferResult = bufferDecryptor(data, key, encoding);
continue;
}
else if (i === encryptionCount && !options.toBuffer) {
stringResult = stringDecryptor(data, key, encoding);
}
data = stringDecryptor(data, key, encoding);
}
if (options.toBuffer) {
if (options.writeToFile) {
fs_1.default.writeFileSync(options.writeToFile, bufferResult);
}
return bufferResult; // escaping typing error
}
else {
if (options.writeToFile) {
fs_1.default.writeFileSync(options.writeToFile, stringResult);
}
return stringResult; // escaping typing error
}
}
exports.decryptFile = decryptFile;
//# sourceMappingURL=decryptor.js.map