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 <edef@edef.eu>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2023-11-27 16:14:33 +02:00 committed by clbot
parent 5730742bdf
commit dfaaf41cef

View file

@ -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
}