[Pkg-javascript-commits] [node-evp-bytestokey] 13/29: restore v1.0.0 API

Bastien Roucariès rouca at moszumanska.debian.org
Fri Sep 8 09:56:50 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 67bedf1e894f0e8c50335d24c1ccd7382a3e64e3
Author: Daniel Cousens <github at dcousens.com>
Date:   Mon Aug 21 12:57:53 2017 +1000

    restore v1.0.0 API
---
 index.js      | 35 +++++++++++++++++------------------
 package.json  |  1 +
 test/index.js | 36 ++++++++++++++----------------------
 3 files changed, 32 insertions(+), 40 deletions(-)

diff --git a/index.js b/index.js
index 3137a65..dfb1896 100644
--- a/index.js
+++ b/index.js
@@ -1,44 +1,43 @@
 var Buffer = require('safe-buffer').Buffer
+var MD5 = require('md5.js')
 
 /* eslint-disable camelcase */
-function EVP_BytesToKey (createHash, salt, data, count, keyLen, ivLen) {
-  if (Buffer.isBuffer(salt) && salt.length !== 8) {
-    throw new RangeError('salt should be Buffer with 8 byte length')
+function EVP_BytesToKey (password, salt, keyLen, ivLen) {
+  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')
   }
 
   var key = Buffer.alloc(keyLen)
   var iv = Buffer.alloc(ivLen)
-  var mdBuf = Buffer.alloc(0)
+  var tmp = Buffer.alloc(0)
 
   while (keyLen > 0 || ivLen > 0) {
-    var hash = createHash()
-    hash.update(mdBuf)
-    hash.update(data)
+    var hash = new MD5()
+    hash.update(tmp)
+    hash.update(password)
     if (salt) hash.update(salt)
+    tmp = hash.digest()
 
-    for (var i = 1; i < count; ++i) {
-      mdBuf = createHash().update(mdBuf).digest()
-    }
-
-    mdBuf = hash.digest()
     var used = 0
 
     if (keyLen > 0) {
       var keyStart = key.length - keyLen
-      used = Math.min(keyLen, mdBuf.length)
-      mdBuf.copy(key, keyStart, 0, used)
+      used = Math.min(keyLen, tmp.length)
+      tmp.copy(key, keyStart, 0, used)
       keyLen -= used
     }
 
-    if (used < mdBuf.length && ivLen > 0) {
+    if (used < tmp.length && ivLen > 0) {
       var ivStart = iv.length - ivLen
-      var length = Math.min(ivLen, mdBuf.length - used)
-      mdBuf.copy(iv, ivStart, used, used + length)
+      var length = Math.min(ivLen, tmp.length - used)
+      tmp.copy(iv, ivStart, used, used + length)
       ivLen -= length
     }
   }
 
-  mdBuf.fill(0)
+  tmp.fill(0)
   return { key: key, iv: iv }
 }
 
diff --git a/package.json b/package.json
index 62eb8bf..c6cbc5d 100644
--- a/package.json
+++ b/package.json
@@ -39,6 +39,7 @@
   },
   "gypfile": true,
   "dependencies": {
+    "md5.js": "^1.3.4",
     "safe-buffer": "^5.1.1"
   }
 }
diff --git a/test/index.js b/test/index.js
index 31e8cdd..d78d99c 100644
--- a/test/index.js
+++ b/test/index.js
@@ -12,39 +12,31 @@ var crypto = require('crypto')
 var test = require('tape')
 var EVP_BytesToKey = require('../')
 
-function createHashMD5 () { return crypto.createHash('md5') }
 var keyLen = 32
 var ivLen = 16
 
-var counts = [1, 2, 100]
-for (var i = 0; i < counts.length; ++i) {
-  var count = counts[i]
+for (var i = 0; i < 1000; ++i) {
+  var password = crypto.randomBytes(10 + Math.round(Math.random() * 100))
+  var salt = crypto.randomBytes(8)
 
-  test('count = ' + count, function (t) {
-    var salt = crypto.randomBytes(8)
-    var data = crypto.randomBytes(10 + Math.round(Math.random() * 100))
-
-    t.test('salt is null', function (t) {
-      var result = EVP_BytesToKey(createHashMD5, null, data, count, keyLen, ivLen)
-      var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv16(null, data, count)
-      t.same(result, expected)
-      t.end()
-    })
-
-    t.test('salt is not null', function (t) {
-      var result = EVP_BytesToKey(createHashMD5, salt, data, count, keyLen, ivLen)
-      var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv16(salt, data, count)
-      t.same(result, expected)
-      t.end()
-    })
+  test('salt is 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()
+  })
 
+  test('salt is not null', function (t) {
+    var result = EVP_BytesToKey(password, salt, keyLen, ivLen)
+    var expected = OpenSSL_EVP_BytesToKey.md5_key32_iv16(salt, password, 1)
+    t.same(result, expected)
     t.end()
   })
 }
 
 test('salt buffer length is 7', function (t) {
   t.throws(function () {
-    EVP_BytesToKey(createHashMD5, Buffer.alloc(7))
+    EVP_BytesToKey(Buffer.alloc(5), Buffer.alloc(7))
   }, /^RangeError: salt should be Buffer with 8 byte length$/)
   t.end()
 })

-- 
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