[Pkg-javascript-commits] [node-evp-bytestokey] 22/29: fix: maintain 1.0 api compatability

Bastien Roucariès rouca at moszumanska.debian.org
Fri Sep 8 09:56:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-evp-bytestokey.

commit 76b9886d53437051af6c8a2077811bd79318649d
Author: dignifiedquire <dignifiedquire at gmail.com>
Date:   Tue Sep 5 11:52:00 2017 +0200

    fix: maintain 1.0 api compatability
    
    - divide keyLen by 8, so 128 -> 16
    - make ivLen optional, defaulting to 0
---
 index.js      | 15 +++++++++++----
 test/index.js | 12 +++++++++++-
 test/main.cc  | 28 ++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/index.js b/index.js
index dfb1896..9a91a86 100644
--- a/index.js
+++ b/index.js
@@ -3,14 +3,21 @@ var MD5 = require('md5.js')
 
 /* eslint-disable camelcase */
 function EVP_BytesToKey (password, salt, keyLen, ivLen) {
-  if (!Buffer.isBuffer(password)) password = Buffer.from(password, 'binary')
+  if (!Buffer.isBuffer(password)) {
+    password = Buffer.from(password, 'binary')
+  }
   if (salt) {
-    if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, 'binary')
-    if (salt.length !== 8) throw new RangeError('salt should be Buffer with 8 byte length')
+    if (!Buffer.isBuffer(salt)) {
+      salt = Buffer.from(salt, 'binary')
+    }
+    if (salt.length !== 8) {
+      throw new RangeError('salt should be Buffer with 8 byte length')
+    }
   }
 
+  keyLen = keyLen / 8
   var key = Buffer.alloc(keyLen)
-  var iv = Buffer.alloc(ivLen)
+  var iv = Buffer.alloc(ivLen || 0)
   var tmp = Buffer.alloc(0)
 
   while (keyLen > 0 || ivLen > 0) {
diff --git a/test/index.js b/test/index.js
index 86fcac1..def0583 100644
--- a/test/index.js
+++ b/test/index.js
@@ -12,7 +12,7 @@ var crypto = require('crypto')
 var test = require('tape')
 var EVP_BytesToKey = require('../')
 
-var keyLen = 32
+var keyLen = 256
 var ivLen = 16
 
 for (var i = 0; i < 1000; ++i) {
@@ -22,6 +22,7 @@ for (var i = 0; i < 1000; ++i) {
   test('password: ' + password.toString('base64') + ', salt: null', function (t) {
     var result = EVP_BytesToKey(password, null, keyLen, ivLen)
     var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv16(null, password, 1)
+
     t.same(result, expected)
     t.end()
   })
@@ -32,6 +33,15 @@ for (var i = 0; i < 1000; ++i) {
     t.same(result, expected)
     t.end()
   })
+
+  test('password: ' + password.toString('base64') + ', salt: ' + salt.toString('base64') + ', no iv', function (t) {
+    var result = EVP_BytesToKey(password, salt, keyLen, 0)
+    var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv0(salt, password, 1)
+
+    console.log(result, expected)
+    t.same(result, expected)
+    t.end()
+  })
 }
 
 test('salt buffer length is 7', function (t) {
diff --git a/test/main.cc b/test/main.cc
index 9a7ba03..c58d434 100644
--- a/test/main.cc
+++ b/test/main.cc
@@ -30,8 +30,36 @@ NAN_METHOD(md5_key32_iv16) {
   info.GetReturnValue().Set(obj);
 }
 
+NAN_METHOD(md5_key32_iv0) {
+  Nan::HandleScope scope;
+
+  const EVP_CIPHER* cipher = EVP_get_cipherbyname("aes-256-cbc");
+
+  const unsigned char* salt = NULL;
+  if (!info[0]->IsNull()) {
+    salt = (const unsigned char*) node::Buffer::Data(info[0].As<v8::Object>());
+  }
+
+  v8::Local<v8::Object> data_buffer = info[1].As<v8::Object>();
+  const unsigned char* data = (const unsigned char*) node::Buffer::Data(data_buffer);
+
+  const int count = info[3].As<v8::Integer>()->Value();
+  unsigned char key[32] = {0};
+
+  int key_len = EVP_BytesToKey(cipher, EVP_md5(), salt, data, node::Buffer::Length(data_buffer), count, key, NULL);
+  if (key_len != 32) {
+    return Nan::ThrowRangeError("invalid key length returned");
+  }
+
+  v8::Local<v8::Object> obj = Nan::New<v8::Object>();
+  obj->Set(Nan::New<v8::String>("key").ToLocalChecked(), Nan::CopyBuffer((const char*) key, 32).ToLocalChecked());
+  obj->Set(Nan::New<v8::String>("iv").ToLocalChecked(), Nan::CopyBuffer((const char*) NULL, 0).ToLocalChecked());
+  info.GetReturnValue().Set(obj);
+}
+
 NAN_MODULE_INIT(Init) {
   Nan::Export(target, "md5_key32_iv16", md5_key32_iv16);
+  Nan::Export(target, "md5_key32_iv0", md5_key32_iv0);
 }
 
 NODE_MODULE(OpenSSL_EVP_BytesToKey, Init)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-evp-bytestokey.git



More information about the Pkg-javascript-commits mailing list