Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Pbkdf2

  1. Object
  2. Pbkdf2

PBKDF2 (RFC 8018) with HMAC-SHA-256 or HMAC-SHA-512: the password hash of key stores and vaults, and with SHA-512 and 2048 iterations the seed of a BIP-39 mnemonic.

A CryptoEngine that derives natively is used when one is installed (every client port installs one). Otherwise the derivation runs in Java, and still costs two hash compressions per iteration: the HMAC key’s inner and outer padded blocks are compressed once, up front, and each iteration resumes from those two states with the previous output as its only block. A straightforward HMAC loop re-hashes both padded keys every round, twice the work.

byte[] mnemonicUtf8 = Mnemonic.generate(128).getBytes("UTF-8");
byte[] saltUtf8 = "mnemonic".getBytes("UTF-8");    // "mnemonic" + passphrase
byte[] seed = Pbkdf2.hmacSha512(mnemonicUtf8, saltUtf8, 2048, 64);

Methods

public static byte[] hmacSha512(byte[] password, byte[] salt, int iterations, int length)PBKDF2 with HMAC-SHA-512.
public static byte[] hmacSha256(byte[] password, byte[] salt, int iterations, int length)PBKDF2 with HMAC-SHA-256; see hmacSha512 for the parameters.

Inherited methods

Method details

hmacSha512

public static byte[] hmacSha512(byte[] password, byte[] salt, int iterations, int length)
PBKDF2 with HMAC-SHA-512.

Parameters

password byte[]
the password bytes, used as they are (encode text as UTF-8, normalized as the protocol requires, before calling)
salt byte[]
the salt
iterations int
at least 1
length int
how many bytes to derive, at least 1

Throws

CryptoException
if an argument is null or out of range

hmacSha256

public static byte[] hmacSha256(byte[] password, byte[] salt, int iterations, int length)
PBKDF2 with HMAC-SHA-256; see hmacSha512 for the parameters.