Skip to content

Commit dc305fe

Browse files
Merge pull request #96 from theseus-rs/add-md5-hash
feat: update hasher registry to work with Maven central and add MD5 hash
2 parents 97a7447 + 71cd379 commit dc305fe

File tree

8 files changed

+48
-15
lines changed

8 files changed

+48
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ home = "0.5.9"
3232
http = "1.1.0"
3333
human_bytes = { version = "0.4.3", default-features = false }
3434
lazy_static = "1.5.0"
35+
md-5 = "0.10.6"
3536
num-format = "0.4.4"
3637
quick-xml = "0.35.0"
3738
rand = "0.8.5"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ In either case, PostgreSQL will run in a separate process space.
2525
- running PostgreSQL on ephemeral ports
2626
- async and blocking API
2727
- bundling the PostgreSQL archive in an executable
28-
- dynamic version resolution
28+
- semantic version resolution
29+
- support for custom PostgreSQL archives / binaries
2930
- ability to configure PostgreSQL startup options
3031
- URL based configuration
3132
- choice of native-tls vs rustls

postgresql_archive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ hex = { workspace = true }
1818
http = { workspace = true }
1919
human_bytes = { workspace = true, default-features = false }
2020
lazy_static = { workspace = true }
21+
md-5 = { workspace = true }
2122
num-format = { workspace = true }
2223
quick-xml = { workspace = true, features = ["serialize"] }
2324
regex = { workspace = true }

postgresql_archive/src/hasher/md5.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::Result;
2+
use md5::{Digest, Md5};
3+
4+
/// Hashes the data using MD5.
5+
///
6+
/// # Errors
7+
/// * If the data cannot be hashed.
8+
pub fn hash(data: &Vec<u8>) -> Result<String> {
9+
let mut hasher = Md5::new();
10+
hasher.update(data);
11+
let hash = hex::encode(hasher.finalize());
12+
Ok(hash)
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn test_hash() -> Result<()> {
21+
let data = vec![4, 2];
22+
let hash = hash(&data)?;
23+
assert_eq!("21fb3d1d1a91a7e80dff456205f3380b", hash);
24+
Ok(())
25+
}
26+
}

postgresql_archive/src/hasher/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod blake2b_512;
22
pub mod blake2s_256;
3+
pub mod md5;
34
pub mod registry;
45
pub mod sha1;
56
pub mod sha2_256;

postgresql_archive/src/hasher/registry.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::configuration::theseus;
2-
use crate::hasher::{sha1, sha2_256, sha2_512};
2+
use crate::hasher::{md5, sha1, sha2_256, sha2_512};
3+
use crate::repository::maven;
34
use crate::Error::{PoisonedLock, UnsupportedHasher};
45
use crate::Result;
56
use lazy_static::lazy_static;
@@ -70,21 +71,21 @@ impl Default for HasherRegistry {
7071
|url, extension| Ok(url.starts_with(theseus::URL) && extension == "sha256"),
7172
sha2_256::hash,
7273
);
73-
// The zonky maven central releases prior to version 13.2.0 only provide MD5/SHA-1 hashes.
74+
// Register the Maven hashers: https://maven.apache.org/resolver/about-checksums.html#implemented-checksum-algorithms
7475
registry.register(
75-
|url, extension| {
76-
Ok(url.contains("zonky")
77-
&& url.contains("embedded-postgres-binaries")
78-
&& extension == "sha1")
79-
},
76+
|url, extension| Ok(url.starts_with(maven::URL) && extension == "md5"),
77+
md5::hash,
78+
);
79+
registry.register(
80+
|url, extension| Ok(url.starts_with(maven::URL) && extension == "sha1"),
8081
sha1::hash,
8182
);
8283
registry.register(
83-
|url, extension| {
84-
Ok(url.contains("zonky")
85-
&& url.contains("embedded-postgres-binaries")
86-
&& extension == "sha512")
87-
},
84+
|url, extension| Ok(url.starts_with(maven::URL) && extension == "sha256"),
85+
sha2_256::hash,
86+
);
87+
registry.register(
88+
|url, extension| Ok(url.starts_with(maven::URL) && extension == "sha512"),
8889
sha2_512::hash,
8990
);
9091
registry
@@ -119,7 +120,6 @@ pub fn get<S: AsRef<str>>(url: S, extension: S) -> Result<HasherFn> {
119120
#[cfg(test)]
120121
mod tests {
121122
use super::*;
122-
use crate::configuration::zonky;
123123

124124
fn test_hasher(extension: &str, expected: &str) -> Result<()> {
125125
let hasher = get("https://foo.com", extension)?;
@@ -163,6 +163,6 @@ mod tests {
163163

164164
#[test]
165165
fn test_get_zonkyio_postgresql_binaries() {
166-
assert!(get(zonky::URL, "sha512").is_ok());
166+
assert!(get(maven::URL, "sha512").is_ok());
167167
}
168168
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pub(crate) mod models;
22
pub mod repository;
3+
4+
pub const URL: &str = "https://repo1.maven.org/maven2";

0 commit comments

Comments
 (0)