armorybrainwallet / brain2bip

Brain wallet for BIP39 mnemonic wallets
6 stars 6 forks source link

Words generated from hash are not correct #1

Open Gerschel opened 4 years ago

Gerschel commented 4 years ago

If I put in the word "hello", the hash output is correct.
I looked at this in the 24 word setting. Looking at the first byte, we have 2cf2, which results in the bits '0010110011110010'. Grabbing the first eleven bits produces the int 359.
Starting from first indice being 0, the 359th word in the list is 'coconut'.
Yours outputs 'stuff', which is the 1724 indice from the english wordlist.

steff517 commented 1 month ago

You're right. I also gave this to ChatGPT 40 for confirmation. It's astonishing, that nobody even repied after 4 years. This issue is very dangerous as the recovery only works using this exact project.

ChatGPTs Analysis: Calculate the SHA-256 hash of the word "hello": The SHA-256 hash of "hello" is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824.

Convert the hash to a binary string: The binary form of the hash is:

00101100 11110010 01001101 10111010 01011111 10110000 10100011 00001110 00100110 11101000 00111011 00101010 11000101 10111001 11100010 10011110 00011011 00010110 00011110 01011100 00011111 10100111 01000010 01011110 01110011 00000100 00110011 01100010 10011000 10111000 00100100

Extract the first 11 bits: The first 11 bits of the binary string are 00101100111.

Convert the first 11 bits to a decimal number:

00101100111 (binary) = 359 (decimal)

Determine the position of the first word in the BIP-39 word list:
The word at position 359 (starting from 0) in the English BIP-39 word list is coconut.

This demonstrates that for the input word "hello," the first hash value 2cf2... (in binary 00101100111...) gives the decimal number 359, which corresponds to the word coconut in the BIP-39 word list.

If you want to check this in code, here is a simple JavaScript implementation:

// Example word list (BIP-39 English word list)
var wordlist = ["abandon", "ability", ... , "zoo"]; // Insert the complete word list here

// Function to compute SHA-256 hash
function sha256(input) {
    return sjcl.hash.sha256.hash(input);
}

// Function to convert bits to word list
function toMnemonic(entropy) {
    var bits = entropy.map(byte => byte.toString(2).padStart(8, '0')).join('');
    var chunks = [];
    for (var i = 0; i < bits.length; i += 11) {
        var chunk = bits.substring(i, i + 11);
        chunks.push(chunk);
    }
    var words = chunks.map(chunk => {
        var index = parseInt(chunk, 2);
        return wordlist[index];
    });
    return words.join(' ');
}

// Main function to compute the first word from "hello"
function getFirstWordFromHello() {
    var hash = sha256("hello");
    var bits = sjcl.codec.hex.fromBits(hash).split('').map(n => parseInt(n, 16).toString(2).padStart(4, '0')).join('');
    var first11Bits = bits.substring(0, 11);
    var index = parseInt(first11Bits, 2);
    var firstWord = wordlist[index];
    return firstWord;
}

console.log(getFirstWordFromHello()); // Output: coconut