toncenter / tonweb-mnemonic

Mnemonic code for generating deterministic keys for TON (The Open Network)
GNU General Public License v3.0
49 stars 19 forks source link

Tryin to build java and swift versions of this lib. PBKDF2WithHmacSHA512 value doesn't match. #22

Open sench93 opened 1 year ago

sench93 commented 1 year ago

I am trying to create java and swift versions of this library so mobile based developer can use it too.

The problem is that PBKDF2 SHA-512 returns different values on your lib and java.

async function pbkdf2Sha512(key: ArrayBuffer, salt: string, iterations: number): Promise<Uint8Array> {
    const saltBuffer = stringToIntArray(salt).buffer;
    const pbkdf2_key = await crypto.subtle.importKey(
        'raw',
        key,
        {name: 'PBKDF2'},
        false,
        ['deriveBits']
    );
    const derivedBits = await crypto.subtle.deriveBits(
        {name: 'PBKDF2', hash: 'SHA-512', salt: saltBuffer, iterations: iterations},
        pbkdf2_key,
        512
    );
    console.log("PBKDF2: " +Buffer.from(derivedBits).toString("hex"))

    return new Uint8Array(derivedBits);
}

This is code from your library.

public  static byte[] pbkdf2HmacSha512(final char[] password, final byte[] salt) {
        try {
            SecretKeyFactory skf = null;
            try {
                skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            PBEKeySpec spec = new PBEKeySpec(password, salt, 390, 512);
            SecretKey key = skf.generateSecret(spec);
            System.out.println(Hex.toHexString(key.getEncoded()));
            return key.getEncoded();
        } catch (Exception  e) {
            throw new RuntimeException(e);
        }
    }

This is one is from java.

Seed phrase is

bless case length avocado panic save inspire movie sadness pizza moon knee gift gorilla same glide bulb capital basket clinic base bachelor civil crunch

Iterations on both places are 390.

Salt is

TON seed version

Hmac(entropy) calculation are same in both places.

6798ceeb23ed6203da80c9dec99a2df64827e64884ab17a8bc36e34750aeca48e536bc9b9460a89a38b734d177711e27edd6139016244d2dcb630d1a1b97cd4a

I would be very thankful if someone helps me to overcome this issue .