Skip to content

Instantly share code, notes, and snippets.

@darrinholst
Created May 14, 2010 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrinholst/401095 to your computer and use it in GitHub Desktop.
Save darrinholst/401095 to your computer and use it in GitHub Desktop.
function PBKDF2(password, salt, num_iterations, num_bytes) {
var m_bpassword = str2binb(password);
var m_salt = salt;
var m_total_iterations = num_iterations;
var m_iterations_in_chunk = 100;
var m_iterations_done = 0;
var m_key_length = num_bytes;
var m_hash_length = 20;
var m_total_blocks = Math.ceil(m_key_length / m_hash_length);
var m_current_block = 1;
var m_ipad = new Array(16);
var m_opad = new Array(16);
var m_buffer = new Array(0x0, 0x0, 0x0, 0x0, 0x0);
var m_key = "";
var m_result_func;
var m_status_func;
if(m_bpassword.length > 16) {
m_bpassword = core_sha1(m_bpassword, password.length * chrsz);
}
for(var i = 0; i < 16; ++i) {
m_ipad[i] = m_bpassword[i] ^ 0x36363636;
m_opad[i] = m_bpassword[i] ^ 0x5C5C5C5C;
}
this.deriveKey = function() {
return this.do_PBKDF2_iterations();
}
this.do_PBKDF2_iterations = function()
{
while(1) {
var iterations = m_total_iterations;
for(var i = 0; i < iterations; ++i) {
if(m_iterations_done == 0) {
var salt_block = m_salt +
String.fromCharCode(m_current_block >> 24 & 0xF) +
String.fromCharCode(m_current_block >> 16 & 0xF) +
String.fromCharCode(m_current_block >> 8 & 0xF) +
String.fromCharCode(m_current_block & 0xF);
m_hash = core_sha1(m_ipad.concat(str2binb(salt_block)), 512 + salt_block.length * 8);
m_hash = core_sha1(m_opad.concat(m_hash), 512 + 160);
}
else {
m_hash = core_sha1(m_ipad.concat(m_hash), 512 + m_hash.length * 32);
m_hash = core_sha1(m_opad.concat(m_hash), 512 + 160);
}
for(var j = 0; j < m_hash.length; ++j) {
m_buffer[j] ^= m_hash[j];
}
m_iterations_done++;
}
if(m_current_block < m_total_blocks) {
m_key += binb2hex(m_buffer);
m_current_block++;
m_buffer = new Array(0x0, 0x0, 0x0, 0x0, 0x0);
m_iterations_done = 0;
}
else {
var tmp = binb2hex(m_buffer);
m_key += tmp.substr(0, (m_key_length - (m_total_blocks - 1) * m_hash_length) * 2);
return GibberishAES.s2a(hex2bin(m_key));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment