From 606dc1aaf88faf3ae0a72104d197118cf7563a64 Mon Sep 17 00:00:00 2001 From: Jan Winter Date: Mon, 2 Aug 2021 10:27:34 +0200 Subject: [PATCH 1/5] Replace mcrypt with native cryptor functions + update alogrithm name MCRYPT_RIJNDAEL_128 seems to be alias name for AES-256-CBC --- lib/RNCryptor.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/RNCryptor.js b/lib/RNCryptor.js index 195d6dd..5963fe5 100644 --- a/lib/RNCryptor.js +++ b/lib/RNCryptor.js @@ -1,12 +1,11 @@ (function() { var crypto = require('crypto'); - var MCrypt = require('mcrypt').MCrypt; var _settings = {}; var _configure_settings = function(version) { var settings = { - algorithm: 'rijndael-128', + algorithm: 'aes-256-cbc', salt_length: 8, iv_length: 16, pbkdf2: { @@ -128,17 +127,12 @@ }; var _generate_iv = function (block_size) { - var mcrypt = new MCrypt(_settings.algorithm, _settings.mode); - var iv = mcrypt.generateIv(); - - return iv.slice(0, block_size); + return crypto.randomBytes(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); + 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([ components.headers.version, @@ -228,11 +222,8 @@ } var key = _generate_key(password, components.headers.encryption_salt); - var mcrypt = new MCrypt(_settings.algorithm, _settings.mode); - mcrypt.open(key, components.headers.iv); - - var padded_plain_text = mcrypt.decrypt(components.cipher_text); - return _strip_pkcs7_padding(padded_plain_text); + const decipher = crypto.createDecipheriv(_settings.algorithm, key, components.headers.iv) + return Buffer.concat([decipher.update(components.cipher_text), decipher.final()]).toString() }; module.exports = RNCryptor; From 1331afb41e42d6ab852288fcabc0a6c6b19a2a0c Mon Sep 17 00:00:00 2001 From: Jan Winter Date: Mon, 2 Aug 2021 10:29:20 +0200 Subject: [PATCH 2/5] Remove legacy mode setting + unnecessary padding functions crypto's createCipheriv / createDecipheriv handle the padding for you --- lib/RNCryptor.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/RNCryptor.js b/lib/RNCryptor.js index 5963fe5..5c92b33 100644 --- a/lib/RNCryptor.js +++ b/lib/RNCryptor.js @@ -19,7 +19,6 @@ switch(version) { case 3: - settings.mode = 'cbc'; settings.options = 1; settings.hmac.includes_header = true; settings.hmac.algorithm = 'sha256'; @@ -108,11 +107,6 @@ return crypto.createHmac(_settings.hmac.algorithm, hmac_key).update(hmac_message).digest(); }; - var _strip_pkcs7_padding = function(plain_text) { - var pad_length = plain_text.slice(-1).toString().charCodeAt(); - return plain_text.slice(0, plain_text.length - pad_length); - }; - var _generate_initialized_components = function(version) { return { headers: { @@ -148,12 +142,6 @@ return Buffer.concat([data, hmac]).toString('base64'); }; - var _add_pkcs7_padding = function (plain_text, block_size) { - var pad_size = block_size - (plain_text.length % block_size); - var padding = new Buffer(new Array(pad_size + 1).join(String.fromCharCode(pad_size)), 'binary'); - return Buffer.concat([plain_text, padding]); - }; - var RNCryptor = {}; RNCryptor.GenerateKey = _generate_key; From ddbd2bf72d59fe83c5e0df17d01fadce9e632d31 Mon Sep 17 00:00:00 2001 From: Jan Winter Date: Mon, 2 Aug 2021 10:30:16 +0200 Subject: [PATCH 3/5] Remove unnecessary dependencies + bump version --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 3d376e1..18c4bc6 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,11 @@ { "name": "jscryptor", - "version": "0.0.12", + "version": "0.0.13", "description": "Javascript implementation of RNCryptor", "main": "index.js", "scripts": { "test": "mocha" }, - "dependencies": { - "mcrypt": "^0.1" - }, "devDependencies": { "chai": "^3.5.0", "mocha": "^2.5.3" @@ -28,5 +25,8 @@ "bugs": { "url": "https://github.com/chesstrian/JSCryptor/issues" }, - "homepage": "https://github.com/chesstrian/JSCryptor" + "homepage": "https://github.com/chesstrian/JSCryptor", + "dependencies": { + "mcrypt": "^0.1" + } } From 8b632f1ca49b0d0cfb8532676eea4d2cc6a48258 Mon Sep 17 00:00:00 2001 From: Jan Winter Date: Mon, 2 Aug 2021 10:33:38 +0200 Subject: [PATCH 4/5] Remove unnecessary dependencies + add new node versions --- .travis.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7662623..7d28c4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ addons: - ubuntu-toolchain-r-test packages: - g++-4.8 - - libmcrypt4 - - libmcrypt-dev node_js: - "0.10" @@ -22,3 +20,7 @@ node_js: - "5" - "6" - "8" + - "10" + - "12" + - "14" + - "16" From 914aaa59665780f7ee2fa81cccd170d2c7127337 Mon Sep 17 00:00:00 2001 From: Jan Winter Date: Mon, 2 Aug 2021 10:33:53 +0200 Subject: [PATCH 5/5] Update README --- README.md | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/README.md b/README.md index 24137c6..7fb700d 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,8 @@ This code is based on the [PHP implementation of RNCryptor](https://github.com/R ## Important Recent Changes Now a `Buffer` is returned, use `.toString()` to convert the result to whatever format you need. -## Install on Linux (Debian) +## Install ```bash -sudo apt-get install libmcrypt4 libmcrypt-dev -npm install jscryptor -``` - -## Install on Mac OS X w/ Homebrew -```bash -brew install libmcrypt npm install jscryptor ```