Skip to content

Commit 6d2e060

Browse files
committed
feat: add sha2-512 support
1 parent 54c4118 commit 6d2e060

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod registry;
22
pub mod sha2_256;
3+
pub mod sha2_512;

postgresql_archive/src/hasher/registry.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hasher::sha2_256;
1+
use crate::hasher::{sha2_256, sha2_512};
22
use crate::Result;
33
use lazy_static::lazy_static;
44
use std::collections::HashMap;
@@ -60,6 +60,7 @@ impl Default for HasherRegistry {
6060
fn default() -> Self {
6161
let mut registry = Self::new();
6262
registry.register("sha256", sha2_256::hash);
63+
registry.register("sha512", sha2_512::hash);
6364
registry
6465
}
6566
}
@@ -131,4 +132,17 @@ mod tests {
131132
);
132133
Ok(())
133134
}
135+
136+
#[test]
137+
fn test_sha2_512() -> Result<()> {
138+
let hasher = get("sha512").unwrap();
139+
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
140+
let hash = hasher(&data)?;
141+
142+
assert_eq!(
143+
"3ad3f36979450d4f53366244ecf1010f4f9121d6888285ff14104fd5aded85d48aa171bf1e33a112602f92b7a7088b298789012fb87b9056321241a19fb74e0b",
144+
hash
145+
);
146+
Ok(())
147+
}
134148
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::Result;
2+
use sha2::{Digest, Sha512};
3+
4+
/// Hashes the data using SHA2-512.
5+
///
6+
/// # Arguments
7+
/// * `data` - The data to hash.
8+
///
9+
/// # Returns
10+
/// * The hash of the data.
11+
///
12+
/// # Errors
13+
/// * If the data cannot be hashed.
14+
pub fn hash(data: &Vec<u8>) -> Result<String> {
15+
let mut hasher = Sha512::new();
16+
hasher.update(data);
17+
let hash = hex::encode(hasher.finalize());
18+
Ok(hash)
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn test_hash() -> Result<()> {
27+
let data = vec![4, 2];
28+
let hash = hash(&data)?;
29+
assert_eq!(
30+
"7df6418d1791a6fe80e726319f16f107534a663346f99e0d155e359a54f6c74391e2f3be19c995c3c903926d348bd86c339bd982e10f09aa776e4ff85d36387a",
31+
hash
32+
);
33+
Ok(())
34+
}
35+
}

postgresql_archive/src/repository/github/repository.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,20 @@ impl Repository for GitHub {
276276
);
277277

278278
if let Some(asset_hash) = asset_hash {
279+
let archive_hash = match asset_hasher_fn {
280+
Some(hasher_fn) => hasher_fn(&bytes)?,
281+
None => return Err(AssetHashNotFound(asset.name))?,
282+
};
283+
let hash_len = archive_hash.len();
284+
279285
debug!(
280286
"Downloading archive hash {}",
281287
asset_hash.browser_download_url
282288
);
283289
let request = client.get(&asset_hash.browser_download_url);
284290
let response = request.send().await?.error_for_status()?;
285291
let text = response.text().await?;
286-
let re = Regex::new(r"[0-9a-f]{64}")?;
292+
let re = Regex::new(&format!(r"[0-9a-f]{{{hash_len}}}"))?;
287293
let hash = match re.find(&text) {
288294
Some(hash) => hash.as_str().to_string(),
289295
None => return Err(AssetHashNotFound(asset.name)),
@@ -294,11 +300,6 @@ impl Repository for GitHub {
294300
human_bytes(text.len() as f64)
295301
);
296302

297-
let archive_hash = match asset_hasher_fn {
298-
Some(hasher_fn) => hasher_fn(&bytes)?,
299-
None => String::new(),
300-
};
301-
302303
if archive_hash != hash {
303304
return Err(ArchiveHashMismatch { archive_hash, hash });
304305
}

0 commit comments

Comments
 (0)