Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace ring with sha2 #67

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ futures = "0.3"
futures-util = "0.3"
hex = "0.4.3"
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "stream"] }
ring = "0.16.20"
semver = { version = "1.0.17", features = ["std", "serde"] }
serde = { version = "1.0", features = [ "derive" ] }
serde_derive = "1.0"
serde_json = "1.0"
sha2 = "0.10.8"
slog = "2.7"
tar = "0.4"
thiserror = "1.0"
Expand Down
10 changes: 5 additions & 5 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use camino::{Utf8Path, Utf8PathBuf};
use chrono::{DateTime, FixedOffset, Utc};
use futures_util::StreamExt;
use reqwest::header::{CONTENT_LENGTH, LAST_MODIFIED};
use ring::digest::{Context as DigestContext, Digest, SHA256};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::str::FromStr;
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};

Expand Down Expand Up @@ -173,13 +173,13 @@ pub async fn download(
Ok(())
}

async fn get_sha256_digest(path: &Utf8Path) -> Result<Digest> {
async fn get_sha256_digest(path: &Utf8Path) -> Result<[u8; 32]> {
let mut reader = BufReader::new(
tokio::fs::File::open(path)
.await
.with_context(|| format!("could not open {path:?}"))?,
);
let mut context = DigestContext::new(&SHA256);
let mut hasher = Sha256::new();
let mut buffer = [0; 1024];

loop {
Expand All @@ -190,10 +190,10 @@ async fn get_sha256_digest(path: &Utf8Path) -> Result<Digest> {
if count == 0 {
break;
} else {
context.update(&buffer[..count]);
hasher.update(&buffer[..count]);
}
}
Ok(context.finish())
Ok(hasher.finalize().into())
}

#[test]
Expand Down
12 changes: 7 additions & 5 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use async_trait::async_trait;
use blake3::{Hash as BlakeDigest, Hasher as BlakeHasher};
use camino::Utf8Path;
use hex::ToHex;
use ring::digest::{Context as DigestContext, Digest as ShaDigest, SHA256};
use serde::{Deserialize, Serialize};
use sha2::{Digest as _, Sha256};
use tokio::io::{AsyncReadExt, BufReader};

// The buffer size used to hash smaller files.
Expand All @@ -22,6 +22,8 @@ const HASH_BUFFER_SIZE: usize = 16 * (1 << 10);
// NOTE: This is currently only blake3-specific.
const LARGE_HASH_SIZE: usize = 1 << 20;

struct ShaDigest([u8; 32]);

/// Implemented by algorithms which can take digests of files.
#[async_trait]
pub trait FileDigester {
Expand All @@ -36,7 +38,7 @@ impl FileDigester for ShaDigest {
.await
.with_context(|| format!("could not open {path:?}"))?,
);
let mut context = DigestContext::new(&SHA256);
let mut hasher = Sha256::new();
let mut buffer = [0; HASH_BUFFER_SIZE];
loop {
let count = reader
Expand All @@ -46,10 +48,10 @@ impl FileDigester for ShaDigest {
if count == 0 {
break;
} else {
context.update(&buffer[..count]);
hasher.update(&buffer[..count]);
}
}
let digest = context.finish().into();
let digest = ShaDigest(hasher.finalize().into()).into();

Ok(digest)
}
Expand Down Expand Up @@ -106,7 +108,7 @@ pub enum Digest {

impl From<ShaDigest> for Digest {
fn from(digest: ShaDigest) -> Self {
Self::Sha2(digest.as_ref().encode_hex::<String>())
Self::Sha2(digest.0.as_ref().encode_hex::<String>())
}
}

Expand Down
Loading