Devrandom
11 years ago
7 changed files with 100 additions and 50 deletions
@ -0,0 +1,41 @@ |
|||
// Crypto extensions
|
|||
//
|
|||
// PBKDF2 with SHA512 - browser version
|
|||
|
|||
var jssha = require('jssha') |
|||
|
|||
var pbkdf2_sha512 = function (password, salt, keylen, options) { |
|||
password = new Buffer(password); |
|||
salt = new Buffer(salt); |
|||
// Defaults
|
|||
var iterations = options && options.iterations || 1; |
|||
|
|||
// Pseudo-random function
|
|||
function PRF(password, salt) { |
|||
var j = new jssha(salt.toString('hex'), 'HEX'); |
|||
var hash = j.getHMAC(password.toString('hex'), "HEX", "SHA-512", "HEX"); |
|||
return new Buffer(hash, 'hex'); |
|||
} |
|||
|
|||
// Generate key
|
|||
var derivedKeyBytes = new Buffer([]), |
|||
blockindex = 1; |
|||
while (derivedKeyBytes.length < keylen) { |
|||
var block = PRF(password, salt.concat([0, 0, 0, blockindex])); |
|||
for (var u = block, i = 1; i < iterations; i++) { |
|||
u = PRF(password, u); |
|||
for (var j = 0; j < block.length; j++) block[j] ^= u[j]; |
|||
} |
|||
derivedKeyBytes = derivedKeyBytes.concat(block); |
|||
blockindex++; |
|||
} |
|||
|
|||
// Truncate excess bytes - TODO
|
|||
//derivedKeyBytes.length = keylen;
|
|||
|
|||
return new Buffer(derivedKeyBytes); |
|||
}; |
|||
|
|||
exports.pbkdf2Sync_sha512 = function(password, salt, iterations, keylen) { |
|||
return pbkdf2_sha512(password, salt, keylen, {iterations: iterations}); |
|||
}; |
@ -1,49 +1,5 @@ |
|||
// Crypto extensions
|
|||
//
|
|||
// PBKDF2 with SHA512
|
|||
|
|||
var binding = require('bindings')('cryptox'); |
|||
|
|||
exports.pbkdf2_sha512 = function(password, salt, iterations, keylen, callback) { |
|||
if (typeof callback !== 'function') |
|||
throw new Error('No callback provided to pbkdf2'); |
|||
|
|||
return pbkdf2_sha512(password, salt, iterations, keylen, callback); |
|||
}; |
|||
|
|||
|
|||
exports.pbkdf2Sync_sha512 = function(password, salt, iterations, keylen) { |
|||
return pbkdf2_sha512(password, salt, iterations, keylen); |
|||
}; |
|||
|
|||
function toBuf(str, encoding) { |
|||
encoding = encoding || 'binary'; |
|||
if (typeof str === 'string') { |
|||
if (encoding === 'buffer') |
|||
encoding = 'binary'; |
|||
str = new Buffer(str, encoding); |
|||
} |
|||
return str; |
|||
} |
|||
|
|||
function pbkdf2_sha512(password, salt, iterations, keylen, callback) { |
|||
password = toBuf(password); |
|||
salt = toBuf(salt); |
|||
|
|||
if (exports.DEFAULT_ENCODING === 'buffer') |
|||
return binding.PBKDF2(password, salt, iterations, keylen, callback); |
|||
|
|||
// at this point, we need to handle encodings.
|
|||
var encoding = exports.DEFAULT_ENCODING; |
|||
if (callback) { |
|||
binding.PBKDF2_sha512(password, salt, iterations, keylen, function(er, ret) { |
|||
if (ret) |
|||
ret = ret.toString(encoding); |
|||
callback(er, ret); |
|||
}); |
|||
} else { |
|||
var ret = binding.PBKDF2_sha512(password, salt, iterations, keylen); |
|||
//return ret.toString(encoding);
|
|||
return ret; |
|||
} |
|||
if (process.versions) { |
|||
module.exports = require('./node/cryptox'); |
|||
return; |
|||
} |
|||
module.exports = require('./browser/cryptox'); |
|||
|
@ -0,0 +1,49 @@ |
|||
// Crypto extensions
|
|||
//
|
|||
// PBKDF2 with SHA512
|
|||
|
|||
var binding = require('bindings')('cryptox'); |
|||
|
|||
exports.pbkdf2_sha512 = function(password, salt, iterations, keylen, callback) { |
|||
if (typeof callback !== 'function') |
|||
throw new Error('No callback provided to pbkdf2'); |
|||
|
|||
return pbkdf2_sha512(password, salt, iterations, keylen, callback); |
|||
}; |
|||
|
|||
|
|||
exports.pbkdf2Sync_sha512 = function(password, salt, iterations, keylen) { |
|||
return pbkdf2_sha512(password, salt, iterations, keylen); |
|||
}; |
|||
|
|||
function toBuf(str, encoding) { |
|||
encoding = encoding || 'binary'; |
|||
if (typeof str === 'string') { |
|||
if (encoding === 'buffer') |
|||
encoding = 'binary'; |
|||
str = new Buffer(str, encoding); |
|||
} |
|||
return str; |
|||
} |
|||
|
|||
function pbkdf2_sha512(password, salt, iterations, keylen, callback) { |
|||
password = toBuf(password); |
|||
salt = toBuf(salt); |
|||
|
|||
if (exports.DEFAULT_ENCODING === 'buffer') |
|||
return binding.PBKDF2(password, salt, iterations, keylen, callback); |
|||
|
|||
// at this point, we need to handle encodings.
|
|||
var encoding = exports.DEFAULT_ENCODING; |
|||
if (callback) { |
|||
binding.PBKDF2_sha512(password, salt, iterations, keylen, function(er, ret) { |
|||
if (ret) |
|||
ret = ret.toString(encoding); |
|||
callback(er, ret); |
|||
}); |
|||
} else { |
|||
var ret = binding.PBKDF2_sha512(password, salt, iterations, keylen); |
|||
//return ret.toString(encoding);
|
|||
return ret; |
|||
} |
|||
} |
Loading…
Reference in new issue