48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
//! Helpers to use cryptographic data types in nettext
|
|
|
|
pub use dryoc::*;
|
|
|
|
use dryoc::types::{Bytes, StackByteArray};
|
|
|
|
use crate::enc;
|
|
|
|
pub type SigningKeyPair = sign::SigningKeyPair<sign::PublicKey, sign::SecretKey>;
|
|
|
|
impl<const N: usize> enc::Encode for StackByteArray<N> {
|
|
fn term(&self) -> enc::Result<'_> {
|
|
Ok(enc::bytes(self.as_slice()))
|
|
}
|
|
}
|
|
|
|
impl enc::Encode for sign::SigningKeyPair<sign::PublicKey, sign::SecretKey> {
|
|
fn term(&self) -> enc::Result<'_> {
|
|
Ok(enc::bytes(self.secret_key.as_slice()))
|
|
}
|
|
}
|
|
|
|
// ---- helpers ----
|
|
|
|
/// Compute the hash of a payload with default dryoc parameters and optionnal key
|
|
pub fn compute_hash(bytes: &[u8], key: Option<&[u8; 32]>) -> generichash::Hash {
|
|
generichash::GenericHash::hash_with_defaults(bytes, key).unwrap()
|
|
}
|
|
|
|
/// Compute the ed25519 signature of a message using a secret key
|
|
pub fn compute_signature(message: &[u8], secret_key: &sign::SecretKey) -> sign::Signature {
|
|
SigningKeyPair::from_secret_key(secret_key.clone())
|
|
.sign_with_defaults(message)
|
|
.unwrap()
|
|
.into_parts()
|
|
.0
|
|
}
|
|
|
|
/// Verify the ed25519 signature of a message using a public key
|
|
pub fn verify_signature(
|
|
signature: &sign::Signature,
|
|
message: &[u8],
|
|
public_key: &sign::PublicKey,
|
|
) -> bool {
|
|
sign::SignedMessage::from_parts(signature.clone(), message)
|
|
.verify(public_key)
|
|
.is_ok()
|
|
}
|