Commit 20e34a14 authored by Éloïs's avatar Éloïs

[feat] crypto: add seed step

parent ece0a503
use dup_crypto::bases::*;
use dup_crypto::hashs::*;
use dup_crypto::keys::*;
use wasm_bindgen::prelude::*;
......@@ -30,3 +32,54 @@ pub fn generate_ed25519_pubkey(salt: &str, password: &str) -> String {
format!("{}", keypair.pubkey)
}
#[wasm_bindgen]
pub fn generate_seed_from_salted_password(salt: &str, password: &str, base: usize) -> String {
if base != 16 {
return "Error : invalid param base : accepted values are : 16.".to_owned();
}
let seed = ed25519::KeyPairFromSaltedPasswordGenerator::with_default_parameters()
.generate_seed(salt.as_bytes(), password.as_bytes());
match base {
16 => Hash(seed).to_hex(),
_ => unreachable!(),
}
}
#[wasm_bindgen]
pub fn generate_ed25519_keypair_from_seed(seed: &str, base: usize) -> String {
match parse_seed(seed, base) {
Ok(seed) => {
let keypair = ed25519::KeyPairFromSeedGenerator::generate(&seed);
format!("{}:{}", keypair.pubkey, keypair.privkey)
}
Err(error_message) => error_message,
}
}
#[wasm_bindgen]
pub fn generate_ed25519_pubkey_from_seed(seed: &str, base: usize) -> String {
match parse_seed(seed, base) {
Ok(seed) => {
let keypair = ed25519::KeyPairFromSeedGenerator::generate(&seed);
format!("{}", keypair.pubkey)
}
Err(error_message) => error_message,
}
}
fn parse_seed(seed: &str, base: usize) -> Result<[u8; 32], String> {
match base {
16 => match b16::str_hex_to_32bytes(seed) {
Ok(seed) => Ok(seed),
Err(err) => Err(format!("{}", err)),
},
58 => match b58::str_base58_to_32bytes(seed) {
Ok(seed) => Ok(seed),
Err(err) => Err(format!("{}", err)),
},
_ => Err("Error : invalid param base : accepted values are : 16, 58.".to_owned()),
}
}
......@@ -28,6 +28,22 @@ fn test_generate_ed25519_keypair() {
);
}
#[wasm_bindgen_test]
fn test_generate_ed25519_keypair_from_seed() {
let seed = generate_seed_from_salted_password(
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV",
"JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV_",
16,
);
let pubkey = generate_ed25519_pubkey_from_seed(&seed, 16);
assert_eq!(
pubkey.as_str(),
"7iMV3b6j2hSj5WtrfchfvxivS9swN3opDgxudeHq64fb"
);
}
#[wasm_bindgen_test]
fn test_parse_and_verify_example_idty_v10() {
let doc = example_doc(DocumentType::IdentityV10);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment