iota/iota-util/src/crypto_helper.rs
2026-08-28 16:21:11 +02:00

34 lines
930 B
Rust

use base64::{Engine as _, engine::general_purpose::STANDARD};
use mtp::crypto::{Keyring, PublicKeyBundle};
pub fn generate_keyring() -> Keyring {
Keyring::generate()
}
pub fn keyring_to_base64(keyring: &Keyring) -> String {
keyring
.try_to_bytes()
.map(|bytes| STANDARD.encode(bytes))
.unwrap_or_default()
}
pub fn keyring_from_base64(s: &str) -> Option<Keyring> {
let bytes = STANDARD.decode(s).ok()?;
Keyring::from_bytes(&bytes).ok()
}
pub fn public_key_bundle_to_base64(bundle: &PublicKeyBundle) -> String {
bundle
.try_as_bytes()
.map(|bytes| STANDARD.encode(bytes))
.unwrap_or_default()
}
pub fn public_key_bundle_from_base64(s: &str) -> Option<PublicKeyBundle> {
let bytes = STANDARD.decode(s).ok()?;
PublicKeyBundle::from_bytes(&bytes).ok()
}
pub fn hex_hash(input: &str) -> String {
hex::encode(mtp::crypto::sha256(input.as_bytes()))
}