strong-cryptor
Version:
Strong encryptor and decryptor nodejs
203 lines • 9.87 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 provider_2 = require("./provider/");
var regex_1 = require("./regex");
var utils_1 = require("./utils");
var Decryptor = /** @class */ (function () {
function Decryptor(options) {
if (options.key.length !== 32) {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
this.options = options;
}
/**
* set key for decryption process
* @param {string} key (or secret) is 256bits (32 charcters) that used to encrypt dan decrypt the data, please store it in the safe places
*/
Decryptor.prototype.setKey = function (key) {
if (key.length !== 32) {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
this.options.key = key;
};
/**
* set options for decryption process
* @param {IDecryptionOptionsClassBase} options (decryption options, see [[IDecryptionOptionsClassBase]] for more details
*/
Decryptor.prototype.setOptions = function (options) {
this.options = __assign({}, options, { key: options.key ? options.key : this.options.key });
};
/**
* 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 {IDecryptionOptionsClassBase} options decryption options, see [[IDecryptionOptionsClassBase]] for more details
* @returns {string|Buffer} return decrypted string/Buffer
*/
Decryptor.prototype.decrypt = function (encryptedString, options) {
if (options === void 0) { options = this.options; }
var key = options.key || this.options.key;
if (key.length !== 32 || options.key === '') {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
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 = this.bufferDecryptor(data, key, encoding);
continue;
}
else if (i === encryptionCount && !options.toBuffer) {
stringResult = this.stringDecryptor(data, key, encoding);
}
data = this.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
}
};
/**
* 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 {IDecryptionOptionsClassBase} options decryption options, see [[IDecryptionOptionsClassBase]] for more details
* @returns {string|Buffer} return decrypted string/Buffer
*/
Decryptor.prototype.decryptFile = function (filePath, options) {
if (options === void 0) { options = this.options; }
var key = options.key || this.options.key;
if (key.length !== 32 || options.key === '') {
throw new provider_1.InvalidKeyError('Key must be 32 characters');
}
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 = this.bufferDecryptor(data, key, encoding);
continue;
}
else if (i === encryptionCount && !options.toBuffer) {
stringResult = this.stringDecryptor(data, key, encoding);
}
data = this.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
}
};
/**
* 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
*/
Decryptor.prototype.stringDecryptor = function (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_2.MalformatedError('Encrypted data malformated');
}
else if (encoding === 'hex' && !regex_1.hexRegex.test(ivString)) {
throw new provider_2.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_2.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
*/
Decryptor.prototype.bufferDecryptor = function (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_2.MalformatedError('Encrypted data malformated');
}
else if (encoding === 'hex' && !regex_1.hexRegex.test(ivString)) {
throw new provider_2.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_2.MalformatedError('Encrypted data malformated');
}
};
return Decryptor;
}());
exports.Decryptor = Decryptor;
//# sourceMappingURL=decryptor.js.map