Skip to content

Commit

Permalink
Remove duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Sep 15, 2023
1 parent 10a5f5f commit 3fc72e4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
14 changes: 2 additions & 12 deletions api/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -62,16 +62,6 @@ where
Ok(keys)
}

/// Hash an input to be signed later
pub fn prehashed_message<I>(input: &mut I) -> Result<Sha512, std::io::Error>
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 {
Expand Down
7 changes: 4 additions & 3 deletions api/src/sign/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()`]
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions api/src/sign/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)?;

Expand Down

0 comments on commit 3fc72e4

Please sign in to comment.