Skip to main content

calculate_hash_with_algorithm

Function calculate_hash_with_algorithm 

Source
pub fn calculate_hash_with_algorithm(
    data: &[u8],
    algorithm: &HashAlgorithm,
) -> String
Expand description

Calculate hash of data using the specified algorithm

§Arguments

  • data - The byte slice to hash
  • algorithm - The hash algorithm to use (SHA-256, SHA-384, or SHA-512)

§Returns

A hexadecimal string representation of the hash:

  • SHA-256: 64 characters
  • SHA-384: 96 characters
  • SHA-512: 128 characters

§Examples

use atlas_cli::hash::calculate_hash_with_algorithm;
use atlas_c2pa_lib::cose::HashAlgorithm;

let data = b"Hello, World!";

// SHA-256
let hash256 = calculate_hash_with_algorithm(data, &HashAlgorithm::Sha256);
assert_eq!(hash256.len(), 64);

// SHA-384
let hash384 = calculate_hash_with_algorithm(data, &HashAlgorithm::Sha384);
assert_eq!(hash384.len(), 96);

// SHA-512
let hash512 = calculate_hash_with_algorithm(data, &HashAlgorithm::Sha512);
assert_eq!(hash512.len(), 128);

// Different algorithms produce different hashes
assert_ne!(hash256, hash384);
assert_ne!(hash384, hash512);