Skip to main content

nix_compat/narinfo/
fingerprint.rs

1use crate::{nixbase32, store_path::StorePathRef};
2
3/// Computes the fingerprint string for certain fields in a [super::NarInfo].
4/// This fingerprint is signed by an ed25519 key, and in the case of a Nix HTTP
5/// Binary cache, included in the NARInfo files served from there.
6pub fn fingerprint<'a, R: Iterator<Item = &'a StorePathRef<'a>>>(
7    store_path: &StorePathRef,
8    nar_sha256: &[u8; 32],
9    nar_size: u64,
10    references: R,
11) -> String {
12    // format everything except the references
13    let mut output = format!(
14        "1;{};sha256:{};{};",
15        store_path.as_absolute_path_fmt(),
16        nixbase32::encode(nar_sha256),
17        nar_size,
18    );
19
20    // append references, which are absolute paths, joined with `,`.
21    use std::fmt::Write;
22    let mut it = references.peekable();
23    while let Some(reference) = it.next() {
24        write!(&mut output, "{}", reference.as_absolute_path_fmt()).unwrap();
25        if it.peek().is_some() {
26            output.write_char(',').unwrap();
27        }
28    }
29
30    output
31}
32
33#[cfg(test)]
34mod tests {
35    use crate::narinfo::NarInfo;
36
37    const NARINFO_STR: &str = r#"StorePath: /nix/store/syd87l2rxw8cbsxmxl853h0r6pdwhwjr-curl-7.82.0-bin
38URL: nar/05ra3y72i3qjri7xskf9qj8kb29r6naqy1sqpbs3azi3xcigmj56.nar.xz
39Compression: xz
40FileHash: sha256:05ra3y72i3qjri7xskf9qj8kb29r6naqy1sqpbs3azi3xcigmj56
41FileSize: 68852
42NarHash: sha256:1b4sb93wp679q4zx9k1ignby1yna3z7c4c2ri3wphylbc2dwsys0
43NarSize: 196040
44References: 0jqd0rlxzra1rs38rdxl43yh6rxchgc6-curl-7.82.0 6w8g7njm4mck5dmjxws0z1xnrxvl81xa-glibc-2.34-115 j5jxw3iy7bbz4a57fh9g2xm2gxmyal8h-zlib-1.2.12 yxvjs9drzsphm9pcf42a4byzj1kb9m7k-openssl-1.1.1n
45Deriver: 5rwxzi7pal3qhpsyfc16gzkh939q1np6-curl-7.82.0.drv
46Sig: cache.nixos.org-1:TsTTb3WGTZKphvYdBHXwo6weVILmTytUjLB+vcX89fOjjRicCHmKA4RCPMVLkj6TMJ4GMX3HPVWRdD1hkeKZBQ==
47Sig: test1:519iiVLx/c4Rdt5DNt6Y2Jm6hcWE9+XY69ygiWSZCNGVcmOcyL64uVAJ3cV8vaTusIZdbTnYo9Y7vDNeTmmMBQ==
48"#;
49
50    #[test]
51    fn fingerprint() {
52        let parsed = NarInfo::parse(NARINFO_STR).expect("must parse");
53        assert_eq!(
54            "1;/nix/store/syd87l2rxw8cbsxmxl853h0r6pdwhwjr-curl-7.82.0-bin;sha256:1b4sb93wp679q4zx9k1ignby1yna3z7c4c2ri3wphylbc2dwsys0;196040;/nix/store/0jqd0rlxzra1rs38rdxl43yh6rxchgc6-curl-7.82.0,/nix/store/6w8g7njm4mck5dmjxws0z1xnrxvl81xa-glibc-2.34-115,/nix/store/j5jxw3iy7bbz4a57fh9g2xm2gxmyal8h-zlib-1.2.12,/nix/store/yxvjs9drzsphm9pcf42a4byzj1kb9m7k-openssl-1.1.1n",
55            parsed.fingerprint()
56        );
57    }
58}