Skip to content

Commit

Permalink
Add hex-encoding examples based on base16ct (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Jun 21, 2024
1 parent 0b79171 commit 8998905
Show file tree
Hide file tree
Showing 44 changed files with 159 additions and 19 deletions.
41 changes: 34 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ascon-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ascon = { version = "0.4", default-features = false }
spectral = { version = "0.6", default-features = false }
hex = "0.4"
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["std"]
Expand Down
4 changes: 4 additions & 0 deletions ascon-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ hasher.update(b"some bytes");
let hash = hasher.finalize();

assert_eq!(hash, hex!("b742ca75e57038757059cccc6874714f9dbd7fc5924a7df4e316594fd1426ca8"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "b742ca75e57038757059cccc6874714f9dbd7fc5924a7df4e316594fd1426ca8");
```

XOF hashing:
Expand Down
1 change: 1 addition & 0 deletions belt-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ belt-block = { version = "0.1.1", default-features = false }
[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["oid", "std"]
Expand Down
4 changes: 4 additions & 0 deletions belt-hash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ hasher.update(b"hello world");
let hash = hasher.finalize();

assert_eq!(hash, hex!("afb175816416fbadad4629ecbd78e1887789881f2d2e5b80c22a746b7ac7ba88"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "afb175816416fbadad4629ecbd78e1887789881f2d2e5b80c22a746b7ac7ba88");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
1 change: 1 addition & 0 deletions blake2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ digest = { version = "=0.11.0-pre.8", features = ["mac"] }
[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["std"]
Expand Down
12 changes: 8 additions & 4 deletions blake2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ let mut hasher = Blake2b512::new();
hasher.update(b"hello world");

// read hash digest and consume hasher
let res = hasher.finalize();
assert_eq!(res, hex!(
let hash = hasher.finalize();
assert_eq!(hash, hex!(
"021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbc"
"c05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0"
));

// same example for Blake2s256:
let mut hasher = Blake2s256::new();
hasher.update(b"hello world");
let res = hasher.finalize();
assert_eq!(res, hex!("9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b"));
let hash = hasher.finalize();
assert_eq!(hash, hex!("9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
1 change: 1 addition & 0 deletions fsb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ whirlpool = { version = "=0.11.0-pre.2", path = "../whirlpool", default-features
[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["std"]
Expand Down
4 changes: 4 additions & 0 deletions fsb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ hasher.update(b"hello");
let hash = hasher.finalize();

assert_eq!(hash, hex!("0f036dc3761aed2cba9de586a85976eedde6fa8f115c0190763decc02f28edbc"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "0f036dc3761aed2cba9de586a85976eedde6fa8f115c0190763decc02f28edbc");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
1 change: 1 addition & 0 deletions gost94/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ digest = "=0.11.0-pre.8"
[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["oid", "std"]
Expand Down
4 changes: 4 additions & 0 deletions gost94/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ hasher.update("The quick brown fox jumps over the lazy dog");
let hash = hasher.finalize();

assert_eq!(hash, hex!("9004294a361a508c586fe53d1f1b02746765e71b765472786e4770d565830a76"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "9004294a361a508c586fe53d1f1b02746765e71b765472786e4770d565830a76");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
1 change: 1 addition & 0 deletions groestl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ digest = "=0.11.0-pre.8"
[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["std"]
Expand Down
8 changes: 6 additions & 2 deletions groestl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ use hex_literal::hex;

let mut hasher = Groestl256::default();
hasher.update(b"my message");
let result = hasher.finalize();
let hash = hasher.finalize();

assert_eq!(result, hex!("dc0283ca481efa76b7c19dd5a0b763dff0e867451bd9488a9c59f6c8b8047a86"));
assert_eq!(hash, hex!("dc0283ca481efa76b7c19dd5a0b763dff0e867451bd9488a9c59f6c8b8047a86"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "dc0283ca481efa76b7c19dd5a0b763dff0e867451bd9488a9c59f6c8b8047a86");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
1 change: 1 addition & 0 deletions jh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ simd = { package = "ppv-lite86", version = "0.2.6" }

[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
base16ct = { version = "0.2", features = ["alloc"] }

[features]
zeroize = ["digest/zeroize"]
Expand Down
8 changes: 6 additions & 2 deletions jh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ use hex_literal::hex;

let mut hasher = Jh256::new();
hasher.update(b"hello");
let result = hasher.finalize();
let hash = hasher.finalize();

assert_eq!(result, hex!("94fd3f4c564957c6754265676bf8b244c707d3ffb294e18af1f2e4f9b8306089"));
assert_eq!(hash, hex!("94fd3f4c564957c6754265676bf8b244c707d3ffb294e18af1f2e4f9b8306089"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "94fd3f4c564957c6754265676bf8b244c707d3ffb294e18af1f2e4f9b8306089");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
1 change: 1 addition & 0 deletions md2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ digest = "=0.11.0-pre.8"
[dev-dependencies]
digest = { version = "=0.11.0-pre.8", features = ["dev"] }
hex-literal = "0.4"
base16ct = { version = "0.2", features = ["alloc"] }

[features]
default = ["oid", "std"]
Expand Down
4 changes: 4 additions & 0 deletions md2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ hasher.update(b"hello world");
let hash = hasher.finalize();

assert_eq!(hash, hex!("d9cce882ee690a5c1ce70beff3a78c77"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "d9cce882ee690a5c1ce70beff3a78c77");
```

Also, see the [examples section] in the RustCrypto/hashes readme.
Expand Down
Loading

0 comments on commit 8998905

Please sign in to comment.