56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
use alloc::borrow::Cow;
|
|
|
|
use crate::error::{ParsingError, ParsingResult};
|
|
use core::fmt::{self, Write};
|
|
|
|
/// A signature of a nix narinfo file. Computed over the StorePath, NarHash, NarSize and References fields using the Ed25519 public-key signature system.
|
|
#[derive(Clone, Eq, PartialEq, Debug)]
|
|
pub struct Sig<'a> {
|
|
/// The name of the key, eg `cache.example.org-1` for cache.nixos.org
|
|
pub key_name: Cow<'a, str>,
|
|
/// The actual signature
|
|
pub sig: Cow<'a, str>,
|
|
}
|
|
|
|
impl<'a> TryFrom<&'a str> for Sig<'a> {
|
|
type Error = ParsingError<'a>;
|
|
|
|
fn try_from(value: &'a str) -> ParsingResult<Self> {
|
|
Sig::parse(value)
|
|
}
|
|
}
|
|
// Neither the parse nor the serializa method is public since
|
|
// it doesn't really make sense to de/serialize the
|
|
// sig into the narinfo format outside of de/serializing a whole narinfo
|
|
impl<'a> Sig<'a> {
|
|
pub(crate) fn parse(value: &'a str) -> ParsingResult<Self> {
|
|
match value.split_once(':') {
|
|
Some((key, sig)) => Ok(Sig {
|
|
key_name: key.into(),
|
|
sig: sig.into(),
|
|
}),
|
|
None => Err(ParsingError::InvalidSignature(value)),
|
|
}
|
|
}
|
|
pub(crate) fn serialize_into<T: Write>(&self, w: &mut T) -> fmt::Result {
|
|
write!(w, "{}:{}", self.key_name, self.sig)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use alloc::string::String;
|
|
|
|
use super::*;
|
|
|
|
const SAMPLE_SIG: &str = "cache.nixos.org-1:BJ5QGcOta2s76XC6sep9DbAv0x3TILh3hHSKyR+9rFWYuBDTWdHs1KHeUEpw2espE/zPPBp2yURO6/J4Dhf9DQ==";
|
|
|
|
#[test]
|
|
fn serialize_deserialize_eq() {
|
|
let sig = Sig::parse(SAMPLE_SIG).unwrap();
|
|
let mut serialized = String::new();
|
|
sig.serialize_into(&mut serialized).unwrap();
|
|
assert_eq!(serialized, SAMPLE_SIG);
|
|
}
|
|
}
|