From 3fc72e4a3827244082b76e327ad3332557352bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Fri, 15 Sep 2023 04:32:09 +0200 Subject: [PATCH] Remove duplicated code --- api/src/sign/mod.rs | 14 ++------------ api/src/sign/tar.rs | 7 ++++--- api/src/sign/zip.rs | 6 +++--- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/api/src/sign/mod.rs b/api/src/sign/mod.rs index 7c5a887..994e6f4 100644 --- a/api/src/sign/mod.rs +++ b/api/src/sign/mod.rs @@ -7,14 +7,14 @@ mod tar; #[cfg(feature = "sign-zip")] mod zip; -use std::io::{copy, Read}; +use std::io::Read; #[cfg(feature = "sign-tar")] pub use self::tar::{copy_and_sign_tar, SignTarError}; #[cfg(feature = "sign-zip")] pub use self::zip::{copy_and_sign_zip, SignZipError}; use crate::{ - Digest, Sha512, SignatureCountLeInt, SignatureError, SigningKey, BUF_LIMIT, HEADER_SIZE, + Sha512, SignatureCountLeInt, SignatureError, SigningKey, BUF_LIMIT, HEADER_SIZE, KEYPAIR_LENGTH, MAGIC_HEADER, SIGNATURE_LENGTH, }; @@ -62,16 +62,6 @@ where Ok(keys) } -/// Hash an input to be signed later -pub fn prehashed_message(input: &mut I) -> Result -where - I: ?Sized + Read, -{ - let mut prehashed_message = Sha512::new(); - let _: u64 = copy(input, &mut prehashed_message)?; - Ok(prehashed_message) -} - /// An error returned by [`gather_signature_data()`] #[derive(Debug, thiserror::Error)] pub enum GatherSignatureDataError { diff --git a/api/src/sign/tar.rs b/api/src/sign/tar.rs index 75d43e3..33790ed 100644 --- a/api/src/sign/tar.rs +++ b/api/src/sign/tar.rs @@ -6,9 +6,10 @@ use base64::prelude::BASE64_STANDARD; use base64::Engine; use ed25519_dalek::SIGNATURE_LENGTH; -use super::{gather_signature_data, prehashed_message, GatherSignatureDataError}; +use super::{gather_signature_data, GatherSignatureDataError}; use crate::{ - SignatureCountLeInt, SigningKey, BUF_LIMIT, GZIP_END, GZIP_EXTRA, GZIP_START, HEADER_SIZE, + prehash, SignatureCountLeInt, SigningKey, BUF_LIMIT, GZIP_END, GZIP_EXTRA, GZIP_START, + HEADER_SIZE, }; /// An error returned by [`copy_and_sign_tar()`] @@ -57,7 +58,7 @@ where } // gather signature - let prehashed_message = prehashed_message(input).map_err(SignTarError::InputRead)?; + let prehashed_message = prehash(input).map_err(SignTarError::InputRead)?; let buf = gather_signature_data(keys, &prehashed_message, context).map_err(SignTarError::Sign)?; let buf = BASE64_STANDARD.encode(buf); diff --git a/api/src/sign/zip.rs b/api/src/sign/zip.rs index 15c23d3..8b975a1 100644 --- a/api/src/sign/zip.rs +++ b/api/src/sign/zip.rs @@ -5,8 +5,8 @@ use std::io::{BufReader, BufWriter, IoSlice, Read, Seek, SeekFrom, Write}; use zip::result::ZipError; use zip::{ZipArchive, ZipWriter}; -use super::{gather_signature_data, prehashed_message, GatherSignatureDataError}; -use crate::{SignatureCountLeInt, SigningKey, BUF_LIMIT, HEADER_SIZE, SIGNATURE_LENGTH}; +use super::{gather_signature_data, GatherSignatureDataError}; +use crate::{prehash, SignatureCountLeInt, SigningKey, BUF_LIMIT, HEADER_SIZE, SIGNATURE_LENGTH}; /// An error returned by [`copy_and_sign_zip()`] #[derive(Debug, thiserror::Error)] @@ -70,7 +70,7 @@ where let _ = output .seek(SeekFrom::Start(signature_bytes.try_into().unwrap())) .map_err(SignZipError::OutputSeek)?; - let prehashed_message = prehashed_message(output).map_err(SignZipError::OutputRead)?; + let prehashed_message = prehash(output).map_err(SignZipError::OutputRead)?; let buf = gather_signature_data(keys, &prehashed_message, context).map_err(SignZipError::Sign)?;