Skip to content

Commit ce629a5

Browse files
Merge pull request #88 from theseus-rs/add-blake2-and-sha3-hash-support
feat: add blake2 and sha3 hash support
2 parents c30c6bd + f546cdd commit ce629a5

File tree

9 files changed

+235
-1
lines changed

9 files changed

+235
-1
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ version = "0.12.0"
2424
[workspace.dependencies]
2525
anyhow = "1.0.86"
2626
async-trait = "0.1.80"
27+
blake2 = "0.10.6"
2728
bytes = "1.6.0"
2829
criterion = "0.5.1"
2930
flate2 = "1.0.30"
@@ -43,6 +44,7 @@ semver = "1.0.23"
4344
serde = "1.0.203"
4445
serde_json = "1.0.118"
4546
sha2 = "0.10.8"
47+
sha3 = "0.10.8"
4648
tar = "0.4.41"
4749
target-triple = "0.1.3"
4850
test-log = "0.2.16"

postgresql_archive/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ version.workspace = true
1212
[dependencies]
1313
anyhow = { workspace = true }
1414
async-trait = { workspace = true }
15+
blake2 = { workspace = true }
1516
bytes = { workspace = true }
1617
flate2 = { workspace = true }
1718
hex = { workspace = true }
@@ -28,6 +29,7 @@ semver = { workspace = true }
2829
serde = { workspace = true, features = ["derive"] }
2930
serde_json = { workspace = true }
3031
sha2 = { workspace = true }
32+
sha3 = { workspace = true }
3133
tar = { workspace = true }
3234
target-triple = { workspace = true }
3335
tempfile = { workspace = true }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::Result;
2+
use blake2::{Blake2b512, Digest};
3+
4+
/// Hashes the data using blake2b-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 = Blake2b512::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+
"e487ff1a06742b6054c76387d7a0bf9e0f62964358b850d80d9f88071508ef855e745a8ba67617f850cf563b20f4ec0d5bd8233b2e85eb0ba4f31a14075fb3d9",
31+
hash
32+
);
33+
Ok(())
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::Result;
2+
use blake2::{Blake2s256, Digest};
3+
4+
/// Hashes the data using blake2s-256.
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 = Blake2s256::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+
"d2e78507a899636d20314690e1683fd2116dd438b502645a8aa8f79fd579fb70",
31+
hash
32+
);
33+
Ok(())
34+
}
35+
}

postgresql_archive/src/hasher/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
pub mod blake2b_512;
2+
pub mod blake2s_256;
13
pub mod registry;
24
pub mod sha2_256;
35
pub mod sha2_512;
6+
pub mod sha3_256;
7+
pub mod sha3_512;

postgresql_archive/src/hasher/registry.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hasher::{sha2_256, sha2_512};
1+
use crate::hasher::{blake2b_512, blake2s_256, sha2_256, sha2_512, sha3_256, sha3_512};
22
use crate::Result;
33
use lazy_static::lazy_static;
44
use std::collections::HashMap;
@@ -59,8 +59,12 @@ impl HasherRegistry {
5959
impl Default for HasherRegistry {
6060
fn default() -> Self {
6161
let mut registry = Self::new();
62+
registry.register("blake2s", blake2s_256::hash);
63+
registry.register("blake2b", blake2b_512::hash);
6264
registry.register("sha256", sha2_256::hash);
6365
registry.register("sha512", sha2_512::hash);
66+
registry.register("sha3-256", sha3_256::hash);
67+
registry.register("sha3-512", sha3_512::hash);
6468
registry
6569
}
6670
}
@@ -145,4 +149,56 @@ mod tests {
145149
);
146150
Ok(())
147151
}
152+
153+
#[test]
154+
fn test_sha3_256() -> Result<()> {
155+
let hasher = get("sha3-256").unwrap();
156+
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
157+
let hash = hasher(&data)?;
158+
159+
assert_eq!(
160+
"c0188232190e0427fc9cc78597221c76c799528660889bd6ce1f3563148ff84d",
161+
hash
162+
);
163+
Ok(())
164+
}
165+
166+
#[test]
167+
fn test_sha3_512() -> Result<()> {
168+
let hasher = get("sha3-512").unwrap();
169+
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
170+
let hash = hasher(&data)?;
171+
172+
assert_eq!(
173+
"9429fc1f9772cc1d8039fe75cc1b033cd60f0ec4face0f8a514d25b0649ba8a5954b6c7a41cc3697a56db3ff321475be1fa14b70c7eb78fec6ce62dbfc54c9d3",
174+
hash
175+
);
176+
Ok(())
177+
}
178+
179+
#[test]
180+
fn test_blake2s_256() -> Result<()> {
181+
let hasher = get("blake2s").unwrap();
182+
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
183+
let hash = hasher(&data)?;
184+
185+
assert_eq!(
186+
"7125921e06071710350390fe902856dbea366a5d6f5ee26c18e741143ac80061",
187+
hash
188+
);
189+
Ok(())
190+
}
191+
192+
#[test]
193+
fn test_blake2s_512() -> Result<()> {
194+
let hasher = get("blake2s").unwrap();
195+
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
196+
let hash = hasher(&data)?;
197+
198+
assert_eq!(
199+
"7125921e06071710350390fe902856dbea366a5d6f5ee26c18e741143ac80061",
200+
hash
201+
);
202+
Ok(())
203+
}
148204
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::Result;
2+
use sha3::{Digest, Sha3_256};
3+
4+
/// Hashes the data using SHA3-256.
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 = Sha3_256::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+
"10a0812b3335c2f6de6dd195c77950e20dbd2e87ee95086db4e2fd42f1a78eed",
31+
hash
32+
);
33+
Ok(())
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::Result;
2+
use sha3::{Digest, Sha3_512};
3+
4+
/// Hashes the data using SHA3-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 = Sha3_512::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+
"4bcb3a87557684ff56272f6bc7f542d728d1b953d8b0beb94ffdd97d9ba872550629c9eb98357060c7dce1786f91e6af948eb1ae21ec304f558a4651ff2b134f",
31+
hash
32+
);
33+
Ok(())
34+
}
35+
}

0 commit comments

Comments
 (0)