Skip to main content

pharos_runtime_core/
support.rs

1/// Performs streq.
2///
3/// # Parameters
4///
5/// - `left`: Input consumed by [`crate::support::streq`].
6/// - `right`: Input consumed by [`crate::support::streq`].
7///
8/// # Returns
9///
10/// Returns `true` when the condition checked by [`crate::support::streq`] holds and `false` otherwise.
11///
12/// # Examples
13///
14/// ```rust,ignore
15/// let _result = pharos_runtime_core::support::streq(todo!(), todo!());
16/// ```
17///
18/// # Related
19///
20/// - [`crate::support`] for adjacent helpers that share the same runtime surface.
21pub fn streq(left: &str, right: &str) -> bool {
22    left == right
23}
24
25/// Encodes bytes as lowercase hexadecimal text.
26pub fn hex_digest(bytes: &[u8]) -> String {
27    const HEX: &[u8; 16] = b"0123456789abcdef";
28    let mut out = String::with_capacity(bytes.len() * 2);
29    for byte in bytes {
30        out.push(HEX[(byte >> 4) as usize] as char);
31        out.push(HEX[(byte & 0x0f) as usize] as char);
32    }
33    out
34}
35
36/// Computes a lowercase SHA-256 digest for arbitrary bytes.
37pub fn sha256_hex(bytes: &[u8]) -> String {
38    use sha2::{Digest, Sha256};
39
40    hex_digest(&Sha256::digest(bytes))
41}
42
43#[cfg(test)]
44mod tests {
45    use super::{hex_digest, sha256_hex};
46
47    #[test]
48    fn encodes_hex_digest() {
49        assert_eq!(hex_digest(&[0x00, 0x0f, 0xa5, 0xff]), "000fa5ff");
50    }
51
52    #[test]
53    fn computes_sha256_hex_digest() {
54        assert_eq!(
55            sha256_hex(b"pharos"),
56            "8653057a4b57183ce71278ca80dbd82a61196fa182652f4cba355614b768d063"
57        );
58    }
59}