Skip to content

Instantly share code, notes, and snippets.

@mykiimike
Last active October 26, 2018 13:20
Show Gist options
  • Save mykiimike/c81700721eee4cb528a4921b0d9ed382 to your computer and use it in GitHub Desktop.
Save mykiimike/c81700721eee4cb528a4921b0d9ed382 to your computer and use it in GitHub Desktop.
posp test
const crypto = require('crypto');
function proofOfSpace(password, salt) {
var iterate = 500000; // gives ~30Mo on sha256
var series = [];
var spaceSize = 0;
console.log("Generating POS password");
var last = '';
for(var a=0; a<iterate; a++) {
var hash = crypto.createHash('sha256');
hash.update(last);
hash.update(salt);
hash.update(password);
var res = hash.digest('hex');
series.push(res);
last = res;
spaceSize += res.length;
}
function showMeYouHaveTheRest(block, pos) {
var hash = crypto.createHash('sha256');
var last = block;
for(var a=pos; a<series.length; a++) {
hash.update(last);
hash.update(series[pos]);
hash.update(salt);
hash.update(password);
last = hash.digest('hex');
}
return(last);
}
for(var a=iterate-1; a>=0; a--) {
var pos = series[a];
if(!pos) {
console.log("Error there");
process.exit(0);
}
var hash = crypto.createHash('sha256');
hash.update(last);
hash.update(pos);
hash.update(salt);
hash.update(password);
hash.update(showMeYouHaveTheRest(last, pos));
var res = hash.digest('hex');
series.push(res);
last = res;
}
console.log("POS password computation done gives "+last+" and spending "+(spaceSize/1024/1024)+'MB');
return("$POSPv1:"+last);
}
proofOfSpace('test', 'test');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment