Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jscryptor-2

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscryptor-2 - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

192

lib/RNCryptor.js

@@ -1,10 +0,10 @@

(function() {
var crypto = require('crypto');
var MCrypt = require('mcrypt').MCrypt;
(function () {
var crypto = require("crypto");
const cryptian = require("cryptian");
var _settings = {};
var _configure_settings = function(version) {
var _configure_settings = function (version) {
var settings = {
algorithm: 'rijndael-128',
algorithm: "rijndael-128",
salt_length: 8,

@@ -14,19 +14,19 @@ iv_length: 16,

iterations: 10000,
key_length: 32
key_length: 32,
},
hmac: {
length: 32
}
length: 32,
},
};
switch(version) {
switch (version) {
case 3:
settings.mode = 'cbc';
settings.mode = "cbc";
settings.options = 1;
settings.hmac.includes_header = true;
settings.hmac.algorithm = 'sha256';
settings.hmac.algorithm = "sha256";
break;
default:
var err = "Unsupported schema version " + version;
throw err
throw err;
}

@@ -37,14 +37,18 @@

var _unpack_encrypted_base64_data = function(b64str) {
var data = Buffer.from(b64str, 'base64');
var _unpack_encrypted_base64_data = function (b64str) {
var data = Buffer.from(b64str, "base64");
var 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;
var cipher_text_length =
data.length - header_length - components.hmac.length;
components.cipher_text = data.slice(header_length, header_length + cipher_text_length);
components.cipher_text = data.slice(
header_length,
header_length + cipher_text_length
);

@@ -54,3 +58,3 @@ return components;

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

@@ -61,3 +65,3 @@

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

@@ -67,3 +71,6 @@ var options_char = buffer_data.slice(offset, offset + 1);

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

@@ -83,19 +90,28 @@

iv: iv,
length: offset
length: offset,
};
};
var _hmac_is_valid = function(components, password) {
var _hmac_is_valid = function (components, password) {
var 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) {
return crypto.pbkdf2Sync(password, salt, _settings.pbkdf2.iterations, _settings.pbkdf2.key_length, 'SHA1');
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('');
var _generate_hmac = function (components, hmac_key) {
var hmac_message = Buffer.from("");

@@ -107,5 +123,5 @@ if (_settings.hmac.includes_header) {

components.headers.options,
components.headers.encryption_salt || Buffer.from(''),
components.headers.hmac_salt || Buffer.from(''),
components.headers.iv
components.headers.encryption_salt || Buffer.from(""),
components.headers.hmac_salt || Buffer.from(""),
components.headers.iv,
]);

@@ -116,6 +132,9 @@ }

return crypto.createHmac(_settings.hmac.algorithm, hmac_key).update(hmac_message).digest();
return crypto
.createHmac(_settings.hmac.algorithm, hmac_key)
.update(hmac_message)
.digest();
};
var _strip_pkcs7_padding = function(plain_text) {
var _strip_pkcs7_padding = function (plain_text) {
var pad_length = plain_text.slice(-1).toString().charCodeAt();

@@ -125,12 +144,12 @@ return plain_text.slice(0, plain_text.length - pad_length);

var _generate_initialized_components = function(version) {
var _generate_initialized_components = function (version) {
return {
headers: {
version: Buffer.from(String.fromCharCode(version)),
options: Buffer.from(String.fromCharCode(_settings.options))
}
options: Buffer.from(String.fromCharCode(_settings.options)),
},
};
};
var _generate_salt = function() {
var _generate_salt = function () {
return _generate_iv(_settings.salt_length);

@@ -140,21 +159,27 @@ };

var _generate_iv = function (block_size) {
var mcrypt = new MCrypt(_settings.algorithm, _settings.mode);
var iv = mcrypt.generateIv();
const iv = crypto.randomBytes(16);
return iv.slice(0, block_size);
};
var _encrypt = function(plain_text, components, encryption_key, hmac_key) {
var padded_plain_text = _add_pkcs7_padding(plain_text, components.headers.iv.length);
var mcrypt = new MCrypt(_settings.algorithm, _settings.mode);
mcrypt.open(encryption_key, components.headers.iv);
components.cipher_text = mcrypt.encrypt(padded_plain_text);
var _encrypt = function (plain_text, components, encryption_key, hmac_key) {
var padded_plain_text = _add_pkcs7_padding(
plain_text,
components.headers.iv.length
);
// var mcrypt = new MCrypt(_settings.algorithm, _settings.mode);
// mcrypt.open(encryption_key, components.headers.iv);
// components.cipher_text = mcrypt.encrypt(padded_plain_text);
const aes128 = new cryptian.algorithm.Rijndael128();
aes128.setKey(encryption_key);
const cipher = new cryptian.mode.cbc.Cipher(aes128, components.headers.iv);
components.cipher_text = cipher.transform(padded_plain_text);
var data = Buffer.concat([
components.headers.version,
components.headers.options,
components.headers.encryption_salt || Buffer.from(''),
components.headers.hmac_salt || Buffer.from(''),
components.headers.encryption_salt || Buffer.from(""),
components.headers.hmac_salt || Buffer.from(""),
components.headers.iv,
components.cipher_text
components.cipher_text,
]);

@@ -164,3 +189,3 @@

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

@@ -170,3 +195,6 @@

var pad_size = block_size - (plain_text.length % block_size);
var padding = Buffer.from(new Array(pad_size + 1).join(String.fromCharCode(pad_size)), 'binary');
var padding = Buffer.from(
new Array(pad_size + 1).join(String.fromCharCode(pad_size)),
"binary"
);
return Buffer.concat([plain_text, padding]);

@@ -179,6 +207,7 @@ };

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

@@ -192,3 +221,6 @@ _configure_settings(version);

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

@@ -199,9 +231,19 @@

RNCryptor.EncryptWithArbitrarySalts = function(plain_text, password, encryption_salt, hmac_salt, iv, version) {
RNCryptor.EncryptWithArbitrarySalts = function (
plain_text,
password,
encryption_salt,
hmac_salt,
iv,
version
) {
version || (version = 3);
Buffer.isBuffer(plain_text) || (plain_text = Buffer.from(plain_text, 'binary'));
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'));
Buffer.isBuffer(hmac_salt) || (hmac_salt = Buffer.from(hmac_salt, 'binary'));
Buffer.isBuffer(iv) || (iv = Buffer.from(iv, 'binary'));
Buffer.isBuffer(encryption_salt) ||
(encryption_salt = Buffer.from(encryption_salt, "binary"));
Buffer.isBuffer(hmac_salt) ||
(hmac_salt = Buffer.from(hmac_salt, "binary"));
Buffer.isBuffer(iv) || (iv = Buffer.from(iv, "binary"));

@@ -221,8 +263,16 @@ _configure_settings(version);

RNCryptor.EncryptWithArbitraryKeys = function (plain_text, encryption_key, hmac_key, iv, version) {
RNCryptor.EncryptWithArbitraryKeys = function (
plain_text,
encryption_key,
hmac_key,
iv,
version
) {
version || (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'));
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"));

@@ -237,6 +287,6 @@ _settings.options = 0;

RNCryptor.Decrypt = function(b64str, password) {
RNCryptor.Decrypt = function (b64str, password) {
var components = _unpack_encrypted_base64_data(b64str);
Buffer.isBuffer(password) || (password = Buffer.from(password, 'binary'));
Buffer.isBuffer(password) || (password = Buffer.from(password, "binary"));

@@ -247,7 +297,15 @@ if (!_hmac_is_valid(components, password)) {

// var mcrypt = new MCrypt(_settings.algorithm, _settings.mode);
// mcrypt.open(key, components.headers.iv);
// var padded_plain_text = mcrypt.decrypt(components.cipher_text);
var key = _generate_key(password, components.headers.encryption_salt);
var mcrypt = new MCrypt(_settings.algorithm, _settings.mode);
mcrypt.open(key, components.headers.iv);
const aes128 = new cryptian.algorithm.Rijndael128();
aes128.setKey(key);
const cipher = new cryptian.mode.cbc.Decipher(
aes128,
components.headers.iv
);
var padded_plain_text = cipher.transform(components.cipher_text);
var padded_plain_text = mcrypt.decrypt(components.cipher_text);
return _strip_pkcs7_padding(padded_plain_text);

@@ -254,0 +312,0 @@ };

{
"name": "jscryptor-2",
"version": "0.0.1",
"version": "0.0.2",
"description": "Javascript implementation of RNCryptor",

@@ -10,3 +10,3 @@ "main": "index.js",

"dependencies": {
"mcrypt": "^0.1"
"cryptian": "^0.0.5"
},

@@ -13,0 +13,0 @@ "devDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc