subxt_signer/crypto/
seed_from_entropy.rs

1// Copyright 2019-2025 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5use alloc::string::String;
6use hmac::Hmac;
7use pbkdf2::pbkdf2;
8use sha2::Sha512;
9use zeroize::Zeroize;
10
11/// This is taken from `substrate-bip39` so that we can keep dependencies in line, and
12/// is the same logic that sp-core uses to go from mnemonic entropy to seed. Returns
13/// `None` if invalid length.
14#[allow(dead_code)]
15pub fn seed_from_entropy(entropy: &[u8], password: &str) -> Option<[u8; 64]> {
16    if entropy.len() < 16 || entropy.len() > 32 || entropy.len() % 4 != 0 {
17        return None;
18    }
19
20    let mut salt = String::with_capacity(8 + password.len());
21    salt.push_str("mnemonic");
22    salt.push_str(password);
23
24    let mut seed = [0u8; 64];
25
26    pbkdf2::<Hmac<Sha512>>(entropy, salt.as_bytes(), 2048, &mut seed).ok()?;
27
28    salt.zeroize();
29
30    Some(seed)
31}