Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) BIP39 Compute Binary Seed from MnemonicSee more Encryption ExamplesCreates a binary seed from a mnemonic. Uses the PBKDF2 function with a mnemonic sentence (in UTF-8 NFKD) used as the password and the string "mnemonic" + passphrase (again in UTF-8 NFKD) used as the salt. The iteration count is set to 2048 and HMAC-SHA512 is used as the pseudo-random function. The length of the derived key is 512 bits (= 64 bytes).
#import <CkoCrypt2.h> #import <NSString.h> #import <CkoBinData.h> // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init]; // Test with the test vectors at https://github.com/trezor/python-mnemonic/blob/master/vectors.json // This is the 2nd test vector.. NSString *mnemonic = @"legal winner thank year wave sausage worth useful legal winner thank yellow"; NSString *passphrase = @"TREZOR"; NSString *expectedSeed = @"2e8905819b8723fe2c1d161860e5ee1830318dbf49a83bd451cfb8440c28bd6fa457fe1296106559a3c80937a1c1069be3a3a5bd381ee6260e8d9739fce1f607"; NSString *expectedMasterKey = @"xprv9s21ZrQH143K2gA81bYFHqU68xz1cX2APaSq5tt6MFSLeXnCKV1RVUJt9FWNTbrrryem4ZckN8k4Ls1H6nwdvDTvnV7zEXs2HgPezuVccsq"; // The mnemonic sentence (in UTF-8 NFKD) used as the password. // The string "mnemonic" + passphrase (again in UTF-8 NFKD) used as the salt. // The iteration count is set to 2048 and HMAC-SHA512 is used as the pseudo-random function. // The length of the derived key is 512 bits (= 64 bytes). // We want the computed seed to be lowercase hex, therefore our salt must also be hex. // The seed is the keyword "mnemonic" + passphrase (in this case is "TREZOR") converted to hex. CkoBinData *bdSalt = [[CkoBinData alloc] init]; [bdSalt AppendString: @"mnemonic" charset: @"utf-8"]; [bdSalt AppendString: passphrase charset: @"utf-8"]; NSString *computedSeed = [crypt Pbkdf2: mnemonic charset: @"utf-8" hashAlg: @"sha512" salt: [bdSalt GetEncoded: @"hex_lower"] iterationCount: [NSNumber numberWithInt: 2048] outputKeyBitLen: [NSNumber numberWithInt: 512] encoding: @"hex_lower"]; NSLog(@"%@%@",@"Expected: ",expectedSeed); NSLog(@"%@%@",@"Computed: ",computedSeed); // To compute the hd_master_key, duplicate this Python code: // def to_hd_master_key(seed: bytes, testnet: bool = False) -> str: // if len(seed) != 64: // raise ValueError("Provided seed should have length of 64") // // # Compute HMAC-SHA512 of seed // seed = hmac.new(b"Bitcoin seed", seed, digestmod=hashlib.sha512).digest() // // # Serialization format can be found at: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Serialization_format // xprv = b"\x04\x88\xad\xe4" # Version for private mainnet // if testnet: // xprv = b"\x04\x35\x83\x94" # Version for private testnet // xprv += b"\x00" * 9 # Depth, parent fingerprint, and child number // xprv += seed[32:] # Chain code // xprv += b"\x00" + seed[:32] # Master key // // # Double hash using SHA256 // hashed_xprv = hashlib.sha256(xprv).digest() // hashed_xprv = hashlib.sha256(hashed_xprv).digest() // // # Append 4 bytes of checksum // xprv += hashed_xprv[:4] // // # Return base58 // return b58encode(xprv) // First compute the HMAC-SHA512 of the computedSeed CkoBinData *bdSeed = [[CkoBinData alloc] init]; [bdSeed AppendEncoded: computedSeed encoding: @"hex_lower"]; crypt.EncodingMode = @"hex_lower"; crypt.HashAlgorithm = @"sha512"; [crypt SetMacKeyString: @"Bitcoin seed"]; NSString *hmacSha512_hex = [crypt MacBdENC: bdSeed]; CkoBinData *bdHmac = [[CkoBinData alloc] init]; [bdHmac AppendEncoded: hmacSha512_hex encoding: @"hex_lower"]; CkoBinData *bdXprv = [[CkoBinData alloc] init]; [bdXprv AppendEncoded: @"0488ade4" encoding: @"hex_lower"]; [bdXprv AppendEncoded: @"000000000000000000" encoding: @"hex_lower"]; [bdXprv AppendEncoded: [bdHmac GetEncodedChunk: [NSNumber numberWithInt: 32] numBytes: [NSNumber numberWithInt: 32] encoding: @"hex_lower"] encoding: @"hex_lower"]; [bdXprv AppendByte: [NSNumber numberWithInt: 0]]; [bdXprv AppendEncoded: [bdHmac GetEncodedChunk: [NSNumber numberWithInt: 0] numBytes: [NSNumber numberWithInt: 32] encoding: @"hex_lower"] encoding: @"hex_lower"]; // Double hash using SHA256 crypt.EncodingMode = @"hex_lower"; crypt.HashAlgorithm = @"sha256"; CkoBinData *bdHash = [[CkoBinData alloc] init]; [bdHash AppendEncoded: [crypt HashBdENC: bdXprv] encoding: @"hex_lower"]; NSString *secondHash = [crypt HashBdENC: bdHash]; [bdHash Clear]; [bdHash AppendEncoded: secondHash encoding: @"hex_lower"]; // Append the 1st 4 bytes of the bdHash to bdXprv. [bdXprv AppendEncoded: [bdHash GetEncodedChunk: [NSNumber numberWithInt: 0] numBytes: [NSNumber numberWithInt: 4] encoding: @"hex_lower"] encoding: @"hex_lower"]; // Base58 encode bdXprv NSString *computedMasterKey = [bdXprv GetEncoded: @"base58"]; NSLog(@"%@%@",@"Expected Master Key: ",expectedMasterKey); NSLog(@"%@%@",@"Computed Master Key: ",computedMasterKey); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.