use std::io::Read;
use std::path::Path;
use std::str::FromStr;
use ed25519_dalek::Signer;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Manifest {
pub country: String,
pub format_version: u32,
pub profile: String,
pub profile_version: u32,
pub file: String,
pub size: u64,
pub sha256: String,
pub documents: usize,
pub built_at: String,
pub sources: Vec<SourceVersion>,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SourceVersion {
pub url: String,
pub last_modified: Option<String>,
pub etag: Option<String>,
}
#[derive(Serialize, Deserialize)]
struct Envelope {
manifest: String,
key: String,
signature: String,
}
#[derive(Debug, thiserror::Error)]
pub enum SignatureError {
#[error("malformed envelope: {0}")]
Envelope(#[from] serde_json::Error),
#[error("signed by an untrusted key {0}")]
UntrustedKey(String),
#[error("invalid key or signature encoding")]
Encoding,
#[error("bad signature")]
BadSignature,
}
pub struct SigningKey(ed25519_dalek::SigningKey);
#[derive(Clone, PartialEq, Eq)]
pub struct PublicKey(ed25519_dalek::VerifyingKey);
impl SigningKey {
pub fn from_seed(seed: [u8; 32]) -> Self {
Self(ed25519_dalek::SigningKey::from_bytes(&seed))
}
pub fn public_key(&self) -> PublicKey {
PublicKey(self.0.verifying_key())
}
pub fn to_hex(&self) -> String {
hex(&self.0.to_bytes())
}
}
impl FromStr for SigningKey {
type Err = SignatureError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from_seed(
unhex(s.trim()).ok_or(SignatureError::Encoding)?,
))
}
}
impl FromStr for PublicKey {
type Err = SignatureError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes: [u8; 32] = unhex(s.trim()).ok_or(SignatureError::Encoding)?;
ed25519_dalek::VerifyingKey::from_bytes(&bytes)
.map(Self)
.map_err(|_| SignatureError::Encoding)
}
}
impl std::fmt::Display for PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&hex(self.0.as_bytes()))
}
}
impl std::fmt::Debug for PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PublicKey({self})")
}
}
pub fn sign(manifest: &Manifest, key: &SigningKey) -> String {
let manifest = serde_json::to_string(manifest).expect("a manifest always serializes");
let signature = key.0.sign(manifest.as_bytes());
serde_json::to_string_pretty(&Envelope {
key: key.public_key().to_string(),
signature: hex(&signature.to_bytes()),
manifest,
})
.expect("an envelope always serializes")
}
pub fn verify(envelope: &[u8], trusted: &[PublicKey]) -> Result<Manifest, SignatureError> {
let envelope: Envelope = serde_json::from_slice(envelope)?;
let key: PublicKey = envelope.key.parse()?;
if !trusted.contains(&key) {
return Err(SignatureError::UntrustedKey(envelope.key));
}
let signature: [u8; 64] = unhex(&envelope.signature).ok_or(SignatureError::Encoding)?;
key.0
.verify_strict(
envelope.manifest.as_bytes(),
&ed25519_dalek::Signature::from_bytes(&signature),
)
.map_err(|_| SignatureError::BadSignature)?;
Ok(serde_json::from_str(&envelope.manifest)?)
}
#[derive(Default)]
pub struct Hasher(Sha256);
impl Hasher {
pub fn update(&mut self, bytes: &[u8]) {
self.0.update(bytes);
}
pub fn finish(self) -> String {
hex(&self.0.finalize())
}
}
pub fn sha256_file(path: &Path) -> std::io::Result<(String, u64)> {
let mut file = std::fs::File::open(path)?;
let mut hasher = Hasher::default();
let mut buffer = vec![0u8; 1 << 20];
let mut size = 0u64;
loop {
let n = file.read(&mut buffer)?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
size += n as u64;
}
Ok((hasher.finish(), size))
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn unhex<const N: usize>(s: &str) -> Option<[u8; N]> {
if s.len() != 2 * N || !s.is_ascii() {
return None;
}
let mut out = [0u8; N];
for (i, byte) in out.iter_mut().enumerate() {
*byte = u8::from_str_radix(&s[2 * i..2 * i + 2], 16).ok()?;
}
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
fn manifest() -> Manifest {
Manifest {
country: "lu".into(),
format_version: 2,
profile: "lu".into(),
profile_version: 1,
file: "lu-0123456789abcdef.bin".into(),
size: 42,
sha256: "00".repeat(32),
documents: 7,
built_at: "2026-11-15T22:00:00Z".into(),
sources: vec![SourceVersion {
url: "https://example.org/a.csv".into(),
last_modified: Some("Sat, 14 Nov 2026 10:00:00 GMT".into()),
etag: None,
}],
}
}
#[test]
fn round_trip() {
let key = SigningKey::from_seed([7; 32]);
let envelope = sign(&manifest(), &key);
assert_eq!(
verify(envelope.as_bytes(), &[key.public_key()]).unwrap(),
manifest()
);
let again: SigningKey = key.to_hex().parse().unwrap();
assert_eq!(again.public_key(), key.public_key());
assert_eq!(
key.public_key().to_string().parse::<PublicKey>().unwrap(),
key.public_key()
);
}
#[test]
fn rejects_other_keys_and_tampering() {
let key = SigningKey::from_seed([7; 32]);
let other = SigningKey::from_seed([8; 32]);
let envelope = sign(&manifest(), &key);
assert!(matches!(
verify(envelope.as_bytes(), &[other.public_key()]),
Err(SignatureError::UntrustedKey(_))
));
let mut e: Envelope = serde_json::from_str(&envelope).unwrap();
e.manifest = e.manifest.replace(&"00".repeat(32), &"11".repeat(32));
let tampered = serde_json::to_vec(&e).unwrap();
assert!(matches!(
verify(&tampered, &[key.public_key()]),
Err(SignatureError::BadSignature)
));
let mut e: Envelope = serde_json::from_str(&sign(&manifest(), &other)).unwrap();
e.key = key.public_key().to_string();
let forged = serde_json::to_vec(&e).unwrap();
assert!(matches!(
verify(&forged, &[key.public_key()]),
Err(SignatureError::BadSignature)
));
}
#[test]
fn hashes() {
let mut h = Hasher::default();
h.update(b"abc");
assert_eq!(
h.finish(),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
}