Skip to content

Instantly share code, notes, and snippets.

@bmatusiak
Created February 1, 2020 22:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmatusiak/4de001f19ed796185ce95887f17f7e28 to your computer and use it in GitHub Desktop.
Save bmatusiak/4de001f19ed796185ce95887f17f7e28 to your computer and use it in GitHub Desktop.
var bitHex = 8;
//idea is that when a pub key gets created, the user stores this int32 in its profile.
//then when you go to fetch the list of pubkeys, hash the key and also fetch the int32 from the profile
//compair the alias + int32 and show the user profile(s) to the requesting user for decision
//when user has decided save the pubkey and any other meta data you wish
var sha256_toint_test = "something like a publickey that has a reference of the output int32";
console.log(sha256_toint_test);
sha256(sha256_toint_test).then(function(data){
console.log(data);// <-- hash buffer
console.log(buf2hex1(data));// <-- hash hex
console.log(toInt(buf2hex1(data)));// <-- int32
});
function buf2hex1(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
function toInt(hash){
return parseInt(hash.slice(0,bitHex),16);
}
function sha256(data){
return window.crypto.subtle.digest('SHA-256', new window.TextEncoder("utf-8").encode(data)); //<--- must use https://
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment