Skip to content

Instantly share code, notes, and snippets.

@rndme
Last active July 6, 2016 02:18
Show Gist options
  • Save rndme/46aa044f58394485e397ed049e203fc8 to your computer and use it in GitHub Desktop.
Save rndme/46aa044f58394485e397ed049e203fc8 to your computer and use it in GitHub Desktop.
key derivation using sha3 and variable loop count
/* password derivation that forces a re-creation of this routine by making slight mods to a normal hash-based kdf:
requires https://github.com/emn178/js-sha3
Example timings on a i7 in chrome:
cost ms
1 - 108
2 - 408
3 - 1067
4 - 2351
5 - 3927
6 - 7113
7 - 9462
8 - 12055
9 - 16270
10 - 20796
*/
function derive(strKey, cost){
cost = +cost || 1;
strKey=sha3_512(String(strKey));
var rounds = (cost*1000) + // 1000 per cost, plus a pseudo-random number between ~ -50 to 1200 :
Math.floor((sha3_256( strKey+cost+strKey ).match(/\w{2}/g).map(a=>parseInt(a,16)).reduce(function(a,b){return a+Math.abs(b)})-2500)/5),
pad = Array(33).join(sha3_512( cost + strKey )); // create a 4kb pad to make derivation require more work
for(var i=0; i < rounds; i++) strKey= sha3_256( (cost+(i*rounds)) + pad.slice(-i).repeat(cost) + strKey + i); // make derivation do math and string work
return strKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment