Skip to content

Instantly share code, notes, and snippets.

@dbwodlf3
Last active August 11, 2020 13:34
Show Gist options
  • Select an option

  • Save dbwodlf3/6c0dda5ae82f07ad18665850c3977c1d to your computer and use it in GitHub Desktop.

Select an option

Save dbwodlf3/6c0dda5ae82f07ad18665850c3977c1d to your computer and use it in GitHub Desktop.
JavaScript Code creating ID from time stamp with sha256
async function makeId(number){
let bitMask1 = 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000;
let bitMask2 = 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff;
let bitArray = new Uint32Array(2);
bitArray[0] = bitMask1^number;
bitArray[1] = bitMask2^number;
let hashBuffer = await crypto.subtle.digest('sha-256',bitArray);
let hashArray = Array.from(new Uint8Array(hashBuffer));
let id = hashArray.map(b => b.toString(16)).join('');
return id;
}
makeId(Date.now()).then((id)=>{console.log(id);});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment