Sha256: 63da9aaefd647d966944cf464f3fd71c472a8bb29c204c9329340350db8b5043

Contents?: true

Size: 1.16 KB

Versions: 35

Compression:

Stored size: 1.16 KB

Contents

var Buffer = require('safe-buffer').Buffer
var MD5 = require('md5.js')

/* eslint-disable camelcase */
function EVP_BytesToKey (password, salt, keyBits, 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 keyLen = keyBits / 8
  var key = Buffer.alloc(keyLen)
  var iv = Buffer.alloc(ivLen || 0)
  var tmp = Buffer.alloc(0)

  while (keyLen > 0 || ivLen > 0) {
    var hash = new MD5()
    hash.update(tmp)
    hash.update(password)
    if (salt) hash.update(salt)
    tmp = hash.digest()

    var used = 0

    if (keyLen > 0) {
      var keyStart = key.length - keyLen
      used = Math.min(keyLen, tmp.length)
      tmp.copy(key, keyStart, 0, used)
      keyLen -= used
    }

    if (used < tmp.length && ivLen > 0) {
      var ivStart = iv.length - ivLen
      var length = Math.min(ivLen, tmp.length - used)
      tmp.copy(iv, ivStart, used, used + length)
      ivLen -= length
    }
  }

  tmp.fill(0)
  return { key: key, iv: iv }
}

module.exports = EVP_BytesToKey

Version data entries

35 entries across 34 versions & 12 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.18.0 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.18.2 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.16.1 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.15.2 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.18.4 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.18.1 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.14.0 test/dummy/node_modules/evp_bytestokey/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/evp_bytestokey/index.js
tang-0.2.1 spec/tang_app/node_modules/evp_bytestokey/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/evp_bytestokey/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/evp_bytestokey/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/evp_bytestokey/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/evp_bytestokey/index.js
tang-0.2.0 spec/tang_app/node_modules/evp_bytestokey/index.js
tang-0.1.0 spec/tang_app/node_modules/evp_bytestokey/index.js
tang-0.0.9 spec/tang_app/node_modules/evp_bytestokey/index.js
enju_library-0.3.8 spec/dummy/node_modules/evp_bytestokey/index.js
ilog-0.4.1 node_modules/evp_bytestokey/index.js