Skip to content

Commit

Permalink
Update code to remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
chesstrian committed Feb 27, 2023
1 parent 6f3ff66 commit c95b935
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 103 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ npm test
```js
// Example taken from https://github.com/RNCryptor/RNCryptor-php/blob/master/examples/decrypt.php

var password = 'myPassword';
var b64string = "AwHsr+ZD87myaoHm51kZX96u4hhaTuLkEsHwpCRpDywMO1Moz35wdS6OuDgq+SIAK6BOSVKQFSbX/GiFSKhWNy1q94JidKc8hs581JwVJBrEEoxDaMwYE+a+sZeirThbfpup9WZQgp3XuZsGuZPGvy6CvHWt08vsxFAn9tiHW9EFVtdSK7kAGzpnx53OUSt451Jpy6lXl1TKek8m64RT4XPr";
const password = 'myPassword';
const b64string = "AwHsr+ZD87myaoHm51kZX96u4hhaTuLkEsHwpCRpDywMO1Moz35wdS6OuDgq+SIAK6BOSVKQFSbX/GiFSKhWNy1q94JidKc8hs581JwVJBrEEoxDaMwYE+a+sZeirThbfpup9WZQgp3XuZsGuZPGvy6CvHWt08vsxFAn9tiHW9EFVtdSK7kAGzpnx53OUSt451Jpy6lXl1TKek8m64RT4XPr";

var RNCryptor = require('jscryptor');
const RNCryptor = require('jscryptor');

console.time('Decrypting example');
var decrypted = RNCryptor.Decrypt(b64string, password);
const decrypted = RNCryptor.Decrypt(b64string, password);
console.timeEnd('Decrypting example');
console.log("Result:", decrypted.toString());
```

### A very good example, provided by @enricodeleo
```js
var fs = require('fs');
var RNCryptor = require('jscryptor');
const fs = require('fs');
const RNCryptor = require('jscryptor');

var password = 'myPassword';
const password = 'myPassword';

var img = fs.readFileSync('./Octocat.jpg');
var enc = RNCryptor.Encrypt(img, password);
const img = fs.readFileSync('./Octocat.jpg');
const enc = RNCryptor.Encrypt(img, password);

// Save encrypted image to a file, for sending to anywhere
fs.writeFileSync('./Octocat.enc', enc);

// Now, to decrypt the image:
var b64 = Buffer.from(fs.readFileSync('./Octocat.enc').toString(), 'base64');
var dec = RNCryptor.Decrypt(b64, password);
const b64 = Buffer.from(fs.readFileSync('./Octocat.enc').toString(), 'base64');
const dec = RNCryptor.Decrypt(b64, password);

fs.writeFileSync('./Octocat2.jpg', dec); // Image should open.
```
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(function() {
(function () {
module.exports = require('./lib/RNCryptor');
})();
95 changes: 46 additions & 49 deletions lib/RNCryptor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(function() {
var crypto = require('crypto');
(function () {
const crypto = require('crypto');

var _settings = {};
let _settings = {};

var _configure_settings = function(version) {
var settings = {
const _configure_settings = function (version) {
const settings = {
algorithm: 'aes-256-cbc',
salt_length: 8,
iv_length: 16,
Expand All @@ -17,53 +17,53 @@
}
};

switch(version) {
switch (version) {
case 3:
settings.options = 1;
settings.hmac.includes_header = true;
settings.hmac.algorithm = 'sha256';
break;
default:
throw "Unsupported schema version " + version
throw `Unsupported schema version ${version}`
}

_settings = settings;
};

var _unpack_encrypted_base64_data = function(b64str) {
var data = Buffer.from(b64str, 'base64');
const _unpack_encrypted_base64_data = function (b64str) {
const data = Buffer.from(b64str, 'base64');

var components = {
const components = {
headers: _parseHeaders(data),
hmac: data.slice(data.length -_settings.hmac.length)
hmac: data.slice(data.length - _settings.hmac.length)
};

var header_length = components.headers.length;
var cipher_text_length = data.length - header_length - components.hmac.length;
const header_length = components.headers.length;
const cipher_text_length = data.length - header_length - components.hmac.length;

components.cipher_text = data.slice(header_length, header_length + cipher_text_length);

return components;
};

var _parseHeaders = function(buffer_data) {
var offset = 0;
const _parseHeaders = function (buffer_data) {
let offset = 0;

var version_char = buffer_data.slice(offset, offset + 1);
const version_char = buffer_data.slice(offset, offset + 1);
offset += version_char.length;

_configure_settings(version_char.toString('binary').charCodeAt());

var options_char = buffer_data.slice(offset, offset + 1);
const options_char = buffer_data.slice(offset, offset + 1);
offset += options_char.length;

var encryption_salt = buffer_data.slice(offset, offset + _settings.salt_length);
const encryption_salt = buffer_data.slice(offset, offset + _settings.salt_length);
offset += encryption_salt.length;

var hmac_salt = buffer_data.slice(offset, offset + _settings.salt_length);
const hmac_salt = buffer_data.slice(offset, offset + _settings.salt_length);
offset += hmac_salt.length;

var iv = buffer_data.slice(offset, offset + _settings.iv_length);
const iv = buffer_data.slice(offset, offset + _settings.iv_length);
offset += iv.length;

return {
Expand All @@ -76,19 +76,19 @@
};
};

var _hmac_is_valid = function(components, password) {
var hmac_key = _generate_key(password, components.headers.hmac_salt);
const _hmac_is_valid = function (components, password) {
const hmac_key = _generate_key(password, components.headers.hmac_salt);

// For 0.11+ we can use Buffer.compare
return components.hmac.toString('hex') == _generate_hmac(components, hmac_key).toString('hex');
return components.hmac.toString('hex') === _generate_hmac(components, hmac_key).toString('hex');
};

var _generate_key = function (password, salt) {
const _generate_key = function (password, salt) {
return crypto.pbkdf2Sync(password, salt, _settings.pbkdf2.iterations, _settings.pbkdf2.key_length, 'SHA1');
};

var _generate_hmac = function(components, hmac_key) {
var hmac_message = Buffer.from('');
const _generate_hmac = function (components, hmac_key) {
let hmac_message = Buffer.from('');

if (_settings.hmac.includes_header) {
hmac_message = Buffer.concat([
Expand All @@ -106,7 +106,7 @@
return crypto.createHmac(_settings.hmac.algorithm, hmac_key).update(hmac_message).digest();
};

var _generate_initialized_components = function(version) {
const _generate_initialized_components = function (version) {
return {
headers: {
version: Buffer.from(String.fromCharCode(version)),
Expand All @@ -115,19 +115,19 @@
};
};

var _generate_salt = function() {
const _generate_salt = function () {
return _generate_iv(_settings.salt_length);
};

var _generate_iv = function (block_size) {
const _generate_iv = function (block_size) {
return crypto.randomBytes(block_size)
};

var _encrypt = function(plain_text, components, encryption_key, hmac_key) {
const _encrypt = function (plain_text, components, encryption_key, hmac_key) {
const cipher = crypto.createCipheriv(_settings.algorithm, encryption_key, components.headers.iv)
components.cipher_text = Buffer.concat([cipher.update(plain_text), cipher.final()])

var data = Buffer.concat([
const data = Buffer.concat([
components.headers.version,
components.headers.options,
components.headers.encryption_salt || Buffer.from(''),
Expand All @@ -136,35 +136,33 @@
components.cipher_text
]);

var hmac = _generate_hmac(components, hmac_key);
const hmac = _generate_hmac(components, hmac_key);

return Buffer.concat([data, hmac]).toString('base64');
};

var RNCryptor = {};
const RNCryptor = {};

RNCryptor.GenerateKey = _generate_key;

RNCryptor.Encrypt = function(plain_text, password, version) {
version || (version = 3);
RNCryptor.Encrypt = function (plain_text, password, version = 3) {
Buffer.isBuffer(plain_text) || (plain_text = Buffer.from(plain_text, 'binary'));
Buffer.isBuffer(password) || (password = Buffer.from(password, 'binary'));

_configure_settings(version);

var components = _generate_initialized_components(version);
const components = _generate_initialized_components(version);
components.headers.encryption_salt = _generate_salt();
components.headers.hmac_salt = _generate_salt();
components.headers.iv = _generate_iv(_settings.iv_length);

var encryption_key = _generate_key(password, components.headers.encryption_salt);
var hmac_key = _generate_key(password, components.headers.hmac_salt);
const encryption_key = _generate_key(password, components.headers.encryption_salt);
const hmac_key = _generate_key(password, components.headers.hmac_salt);

return _encrypt(plain_text, components, encryption_key, hmac_key);
};

RNCryptor.EncryptWithArbitrarySalts = function(plain_text, password, encryption_salt, hmac_salt, iv, version) {
version || (version = 3);
RNCryptor.EncryptWithArbitrarySalts = function (plain_text, password, encryption_salt, hmac_salt, iv, version = 3) {
Buffer.isBuffer(plain_text) || (plain_text = Buffer.from(plain_text, 'binary'));
Buffer.isBuffer(password) || (password = Buffer.from(password));
Buffer.isBuffer(encryption_salt) || (encryption_salt = Buffer.from(encryption_salt, 'binary'));
Expand All @@ -173,42 +171,41 @@

_configure_settings(version);

var components = _generate_initialized_components(version);
const components = _generate_initialized_components(version);
components.headers.encryption_salt = encryption_salt;
components.headers.hmac_salt = hmac_salt;
components.headers.iv = iv;

var encryption_key = _generate_key(password, encryption_salt);
var hmac_key = _generate_key(password, hmac_salt);
const encryption_key = _generate_key(password, encryption_salt);
const hmac_key = _generate_key(password, hmac_salt);

return _encrypt(plain_text, components, encryption_key, hmac_key);
};

RNCryptor.EncryptWithArbitraryKeys = function (plain_text, encryption_key, hmac_key, iv, version) {
version || (version = 3);
RNCryptor.EncryptWithArbitraryKeys = function (plain_text, encryption_key, hmac_key, iv, version = 3) {
Buffer.isBuffer(plain_text) || (plain_text = Buffer.from(plain_text, 'binary'));
Buffer.isBuffer(encryption_key) || (encryption_key = Buffer.from(encryption_key, 'binary'));
Buffer.isBuffer(hmac_key) || (hmac_key = Buffer.from(hmac_key, 'binary'));
Buffer.isBuffer(iv) || (iv = Buffer.from(iv, 'binary'));

_settings.options = 0;

var components = _generate_initialized_components(version);
const components = _generate_initialized_components(version);
components.headers.iv = iv;

return _encrypt(plain_text, components, encryption_key, hmac_key);
};

RNCryptor.Decrypt = function(b64str, password) {
var components = _unpack_encrypted_base64_data(b64str);
RNCryptor.Decrypt = function (b64str, password) {
const components = _unpack_encrypted_base64_data(b64str);

Buffer.isBuffer(password) || (password = Buffer.from(password, 'binary'));

if (!_hmac_is_valid(components, password)) {
return;
}

var key = _generate_key(password, components.headers.encryption_salt);
const key = _generate_key(password, components.headers.encryption_salt);
const decipher = crypto.createDecipheriv(_settings.algorithm, key, components.headers.iv)
return Buffer.concat([decipher.update(components.cipher_text), decipher.final()]).toString()
};
Expand Down
26 changes: 13 additions & 13 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
(function() {
var fs = require('fs');
var path = require('path');
(function () {
const fs = require('fs');
const path = require('path');

var spec_dir = path.join(__dirname, '../spec/vectors/v3/');
const spec_dir = path.join(__dirname, '../spec/vectors/v3/');

fs.readdirSync(spec_dir).forEach(function(file) {
var aux_object;
fs.readdirSync(spec_dir).forEach(function (file) {
let aux_object;
exports[file] = [];

var file_content = fs.readFileSync(spec_dir + file);
file_content.toString().split('\n').forEach(function(line) {
if(line === '' || line[0] === '#') return;
const file_content = fs.readFileSync(spec_dir + file);
file_content.toString().split('\n').forEach(function (line) {
if (line === '' || line[0] === '#') return;

var key_value = line.split(':');
if(key_value[0] === 'title') aux_object = {};
const key_value = line.split(':');
if (key_value[0] === 'title') aux_object = {};

if(['key_hex', 'ciphertext_hex', 'plaintext_hex'].indexOf(key_value[0]) >= 0)
if (['key_hex', 'ciphertext_hex', 'plaintext_hex'].indexOf(key_value[0]) >= 0)
key_value[1] = key_value[1].replace(/\s+/g, '');

aux_object[key_value[0]] = key_value[1].trim() || '';

if(['key_hex', 'ciphertext_hex'].indexOf(key_value[0]) >= 0) exports[file].push(aux_object);
if (['key_hex', 'ciphertext_hex'].indexOf(key_value[0]) >= 0) exports[file].push(aux_object);
});
});
})();
Loading

0 comments on commit c95b935

Please sign in to comment.