nettext/src/crypto/mod.rs

49 lines
1.4 KiB
Rust
Raw Normal View History

2022-11-17 21:53:36 +00:00
//! Helpers to use cryptographic data types in nettext
pub use dryoc::*;
2022-11-17 21:53:36 +00:00
use dryoc::types::{Bytes, StackByteArray};
2022-11-17 21:53:36 +00:00
use crate::enc;
2022-11-17 21:53:36 +00:00
pub type SigningKeyPair = sign::SigningKeyPair<sign::PublicKey, sign::SecretKey>;
2022-11-17 21:53:36 +00:00
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()))
}
2022-11-17 21:53:36 +00:00
}
2022-11-22 14:29:04 +00:00
// ---- 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()
2022-11-22 14:29:04 +00:00
}
/// 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
2022-11-22 14:29:04 +00:00
}
/// 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()
2022-11-22 14:29:04 +00:00
}