From dfaaf41cefe004eeb35f362cee3ba145bf0b9d35 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 27 Nov 2023 16:14:33 +0200 Subject: [PATCH] refactor(nix-compat): use ed25519_dalek::SIGNATURE_LENGTH No need to hardcode magic numbers here, we have a constant for that. Change-Id: I67b671c0c4bb7c3bfb001e9c36499f31873ee717 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10145 Reviewed-by: edef Autosubmit: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/narinfo/signature.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tvix/nix-compat/src/narinfo/signature.rs b/tvix/nix-compat/src/narinfo/signature.rs index c7d2e1ecb..26e062a09 100644 --- a/tvix/nix-compat/src/narinfo/signature.rs +++ b/tvix/nix-compat/src/narinfo/signature.rs @@ -1,29 +1,30 @@ use std::fmt::{self, Display}; use data_encoding::BASE64; +use ed25519_dalek::SIGNATURE_LENGTH; #[derive(Debug)] pub struct Signature<'a> { /// TODO(edef): be stricter with signature names here, they especially shouldn't have newlines! name: &'a str, - bytes: [u8; 64], + bytes: [u8; SIGNATURE_LENGTH], } impl<'a> Signature<'a> { - pub fn new(name: &'a str, bytes: [u8; 64]) -> Self { + pub fn new(name: &'a str, bytes: [u8; SIGNATURE_LENGTH]) -> Self { Self { name, bytes } } - pub fn parse(input: &'a str) -> Result, SignatureError> { + pub fn parse(input: &'a str) -> Result { let (name, bytes64) = input .split_once(':') .ok_or(SignatureError::MissingSeparator)?; - let mut buf = [0; 66]; - let mut bytes = [0; 64]; + let mut buf = [0; SIGNATURE_LENGTH + 2]; + let mut bytes = [0; SIGNATURE_LENGTH]; match BASE64.decode_mut(bytes64.as_bytes(), &mut buf) { - Ok(64) => { - bytes.copy_from_slice(&buf[..64]); + Ok(SIGNATURE_LENGTH) => { + bytes.copy_from_slice(&buf[..SIGNATURE_LENGTH]); } Ok(n) => return Err(SignatureError::InvalidSignatureLen(n)), // keeping DecodePartial gets annoying lifetime-wise @@ -37,7 +38,7 @@ impl<'a> Signature<'a> { self.name } - pub fn bytes(&self) -> &[u8; 64] { + pub fn bytes(&self) -> &[u8; SIGNATURE_LENGTH] { &self.bytes }