<p>I am trying to go from a BIP39 phrase to Seed to xprv root key. I have the following BIP39 phrase:</p>
<p>&quot;prefer offer puppy imitate pink banana because whale letter thought artwork loud&quot;</p>
<p>which according to the Ian Coleman tool returns a Seed of</p>
<pre><code>af0dc2290ec62618dd11686d3fb7201dc3a57dcc3129713272cd1fe75002d963db048cfda84da6cc1762c027b49b1a76e2d3e408f589c7acfca681e506a03aff
</code></pre>
<p>and a xprv of</p>
<pre><code>xprv9s21ZrQH143K2Xszf61JSrkqT2cgXVbhKcu3YcfKmL5gYquAsMTFTwZ6NDqjMzPkCk4k9MkqwQKqyfxH7K3RCXdRZ7poUFZw3h5mEu9kCEW
</code></pre>
<p>I use the following code to try to emulate this:</p>
<pre><code>from hashlib import pbkdf2_hmac #import for going BIP39 words to key

    def BIP39_to_hash():
    
        mnemonictohash = 'prefer offer puppy imitate pink banana because whale letter thought artwork loud'
        a512string = pbkdf2_hmac('sha512',bytes(mnemonictohash,'ascii'), bytes.fromhex(''), 2048, dklen=512) #bytesfromhex is the salt (blank), 2048 is iterations
        print(binascii.hexlify(a512string)) #as Byte String converted to hex
    
    BIP39_to_hash
</code></pre>
<p>however this returns</p>
<pre><code>b'1acefb42123af6019fe4487aef8095b3879a028f313bae10b83fd72f8972fed7cc2502a8a6f0c1b99c2fb55efebcbd79258808c0c026dbfa282fa0b62db827da5fcea3cb6d71a31c41b6324e2b00cdc4e270572da5683d05201e89413fa1aded294be883635e6de3c84c87617971c84a17364c727109d6de3e82b31bc0a49be3e93f40fc50cd2b8121589ddfd44c338b98ef76b1408a74e2f4a521087e8dfb3112ffb12fa2f29cd7c6abc311b25c3d0278c836249d01525ecf39faa3ca6a4fc2513cb3ae6af70552179aee8f755ed6be00afce1303452dd8008bfdcd289849df2ce304807a9f7bfc1040927b3aca60390c05132cb12c00dd9b33e165f1ced6157eeb60253979af0922a1794427efbf27d8810e11bf31b5a41bdad73dbd85e200ef982ff0ce2157f0cdcd0b35e069c41a850459f04752654119590f86b528544b616b24798e1a179853530491b27b21d8bfde33575679ba16afde0b40b95a6ab112fbe68a8df11ba1b8d419251b5c85121a0f2deeea43b767d720df730018f4fd8616d3f787b196cc1cd61f7a0eef0149453a599193bdf51af65c3ac5b7b469bb7ab9fb8d837a727979b6d6de079e1db7eb02eee03b19e719615b54e5bf0be5dade7fd022aff5fd0389ce37b5ca69e8172c2420cb9b1869d4eb3c083a06096bd3d334ab223efd3f1eb8d301ce89a5685bbbc3fa5cba4a26c696eb55e660367c39'
</code></pre>
<p>what am I missing?</p>

CC BY-SA 4.0
2024-07-18T09:46:49.680Z
I have just noticed that I should not be using a blank salt, but the word "mnemonic" itself, byte converted and joined with a passphrase (blank, '') which returns a string of length 1024. Do I take the first 128 chars of this string as my seed? I think that must be the case.

Source: Answer to this question https://bitcoin.stackexchange.com/a/114285
0xCas
CC BY-SA 4.0
2024-07-19T04:20:35.740Z
`dklen` is in bytes so use `dklen=64` and as you noted `salt=b'mnemonic'`
dave_thompson_085
<python><mnemonic-seed>
python "pbkdf2_hmac" function doesn't return same as Ian Coleman BIP39 tool. Why?
0xCas
# python "pbkdf2_hmac" function doesn't return same as Ian Coleman BIP39 tool. Why?

(Asked by: username_0 on 2024-07-18)

I am trying to go from a BIP39 phrase to Seed to xprv root key. I have the following BIP39 phrase:
"prefer offer puppy imitate pink banana because whale letter thought artwork loud"
which according to the Ian Coleman tool returns a Seed of
af0dc2290ec62618dd11686d3fb7201dc3a57dcc3129713272cd1fe75002d963db048cfda84da6cc1762c027b49b1a76e2d3e408f589c7acfca681e506a03aff

and a xprv of
xprv9s21ZrQH143K2Xszf61JSrkqT2cgXVbhKcu3YcfKmL5gYquAsMTFTwZ6NDqjMzPkCk4k9MkqwQKqyfxH7K3RCXdRZ7poUFZw3h5mEu9kCEW

I use the following code to try to emulate this:
from hashlib import pbkdf2_hmac #import for going BIP39 words to key

    def BIP39_to_hash():
    
        mnemonictohash = 'prefer offer puppy imitate pink banana because whale letter thought artwork loud'
        a512string = pbkdf2_hmac('sha512',bytes(mnemonictohash,'ascii'), bytes.fromhex(''), 2048, dklen=512) #bytesfromhex is the salt (blank), 2048 is iterations
        print(binascii.hexlify(a512string)) #as Byte String converted to hex
    
    BIP39_to_hash

however this returns
b'1acefb42123af6019fe4487aef8095b3879a028f313bae10b83fd72f8972fed7cc2502a8a6f0c1b99c2fb55efebcbd79258808c0c026dbfa282fa0b62db827da5fcea3cb6d71a31c41b6324e2b00cdc4e270572da5683d05201e89413fa1aded294be883635e6de3c84c87617971c84a17364c727109d6de3e82b31bc0a49be3e93f40fc50cd2b8121589ddfd44c338b98ef76b1408a74e2f4a521087e8dfb3112ffb12fa2f29cd7c6abc311b25c3d0278c836249d01525ecf39faa3ca6a4fc2513cb3ae6af70552179aee8f755ed6be00afce1303452dd8008bfdcd289849df2ce304807a9f7bfc1040927b3aca60390c05132cb12c00dd9b33e165f1ced6157eeb60253979af0922a1794427efbf27d8810e11bf31b5a41bdad73dbd85e200ef982ff0ce2157f0cdcd0b35e069c41a850459f04752654119590f86b528544b616b24798e1a179853530491b27b21d8bfde33575679ba16afde0b40b95a6ab112fbe68a8df11ba1b8d419251b5c85121a0f2deeea43b767d720df730018f4fd8616d3f787b196cc1cd61f7a0eef0149453a599193bdf51af65c3ac5b7b469bb7ab9fb8d837a727979b6d6de079e1db7eb02eee03b19e719615b54e5bf0be5dade7fd022aff5fd0389ce37b5ca69e8172c2420cb9b1869d4eb3c083a06096bd3d334ab223efd3f1eb8d301ce89a5685bbbc3fa5cba4a26c696eb55e660367c39'

what am I missing?

**Tags:** python, mnemonic-seed

> **Comments:**
> **username_0**: I have just noticed that I should not be using a blank salt, but the word "mnemonic" itself, byte converted and joined with a passphrase (blank, '') which returns a string of length 1024. Do I take the first 128 chars of this string as my seed? I think that must be the case.  Source: Answer to this question https://bitcoin.stackexchange.com/a/114285  
> **username_1**: `dklen` is in bytes so use `dklen=64` and as you noted `salt=b'mnemonic'`  

---

## 0 answers

