This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checksum_12_words_min(data) { | |
let binstr = (s,l =8) => s.toString(2).padStart(l,'0') // convert to binary `0011...` string | |
let tohex = (bytes) => bytes.map( x => x.toString(16).padStart(2,0) ).join('') | |
let bytes = data.map( x => binstr(x - 1, 11)).join('').match(/.{1,8}/g).map( x => parseInt(x, 2)) | |
bytes.pop(); console.log("Entropy is :",tohex(bytes)) | |
window.crypto.subtle.digest("SHA-256", new Uint8Array(bytes).buffer).then( x => { | |
let hash = new Uint8Array(x) | |
let cs = binstr(hash[0]).match(/.{1,4}/g)[0] | |
let bits = [binstr(bytes[15]),cs].join('') | |
console.log("Your 12th word index is: " + (1+parseInt(bits.substr(1),2))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We expect array of 12 words as human readable indexes (1-2048) | |
function checksum_12_words(data) { | |
if (data.length != 12) return console.log("ERROR: Need 12 words numbers as input") | |
let binstr = (s,l =8) => s.toString(2).padStart(l,'0') // convert to binary `0011...` string | |
let tohex = (bytes) => bytes.map( x => x.toString(16).padStart(2,0) ).join('') | |
let bytes = data.map( x => binstr(x - 1, 11)) // convert to 0-index, then to binary `0011...` | |
.join('').match(/.{1,8}/g) // split 8 bits | |
.map( x => parseInt(x, 2)) // convert to UInt8 |