MnemonicUtils

BIP-39 mnemonic utility functions for generating, validating, and converting mnemonic phrases.

This object provides the core operations for BIP-39 mnemonic handling as specified by the BIP-39 specification and SEP-5.

Security Considerations

  • CSPRNG: Uses cryptographically secure random number generation for entropy

  • O(1) Word Lookup: Word list lookups use HashMap for constant-time access to mitigate timing attacks

  • Constant-Time Comparison: Checksum validation uses constant-time comparison to prevent timing attacks

  • NFKD Normalization: Both mnemonic and passphrase are NFKD-normalized before use per BIP-39 specification

  • Memory Cleanup: Generated entropy is zeroed after use for defense in depth

Usage Example

// Generate a 24-word mnemonic (256 bits of entropy)
val mnemonic = MnemonicUtils.generateMnemonic(MnemonicStrength.BITS_256)

// Validate a mnemonic
if (MnemonicUtils.validateMnemonic(mnemonic)) {
println("Mnemonic is valid")
}

// Convert mnemonic to BIP-39 seed
val seed = MnemonicUtils.mnemonicToSeed(mnemonic, passphrase = "optional passphrase")

See also

for entropy strength options

for supported word list languages

Functions

Link copied to clipboard

Detects the language of a mnemonic phrase.

Link copied to clipboard
suspend fun entropyToMnemonic(entropy: ByteArray, language: MnemonicLanguage = MnemonicLanguage.ENGLISH): String

Converts entropy bytes to a BIP-39 mnemonic phrase.

Link copied to clipboard
suspend fun generateMnemonic(strength: MnemonicStrength = MnemonicStrength.BITS_256, language: MnemonicLanguage = MnemonicLanguage.ENGLISH): String

Generates a BIP-39 mnemonic phrase with the specified strength.

Link copied to clipboard

Gets the strength level of a mnemonic phrase based on its word count.

Link copied to clipboard
suspend fun mnemonicToEntropy(mnemonic: String, language: MnemonicLanguage = MnemonicLanguage.ENGLISH): ByteArray

Converts a BIP-39 mnemonic phrase back to its original entropy.

Link copied to clipboard
suspend fun mnemonicToSeed(mnemonic: String, passphrase: String = ""): ByteArray

Converts a BIP-39 mnemonic phrase to a 64-byte seed.

Link copied to clipboard
suspend fun mnemonicToSeedHex(mnemonic: String, passphrase: String = ""): String

Converts a BIP-39 mnemonic phrase to a hex-encoded 64-byte seed.

Link copied to clipboard
suspend fun validateMnemonic(mnemonic: String, language: MnemonicLanguage = MnemonicLanguage.ENGLISH): Boolean

Validates a BIP-39 mnemonic phrase.