about summary refs log tree commit diff
path: root/tvix/nix-compat/src/narinfo/signature.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-11-27T14·14+0200
committerclbot <clbot@tvl.fyi>2023-11-27T15·11+0000
commitdfaaf41cefe004eeb35f362cee3ba145bf0b9d35 (patch)
treecd3a3e9610338e803516eb1ab160954d7e8e552b /tvix/nix-compat/src/narinfo/signature.rs
parent5730742bdf996f0a780579068b33459a6e3139ef (diff)
refactor(nix-compat): use ed25519_dalek::SIGNATURE_LENGTH r/7073
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 <edef@edef.eu>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/narinfo/signature.rs')
-rw-r--r--tvix/nix-compat/src/narinfo/signature.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/tvix/nix-compat/src/narinfo/signature.rs b/tvix/nix-compat/src/narinfo/signature.rs
index c7d2e1ecbcf7..26e062a09eea 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<Signature<'a>, SignatureError> {
+    pub fn parse(input: &'a str) -> Result<Self, SignatureError> {
         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
     }