detect_hash_algorithm

Function detect_hash_algorithm 

Source
pub fn detect_hash_algorithm(hash: &str) -> HashAlgorithm
Expand description

Detect hash algorithm based on hash length

This function infers the hash algorithm from the hexadecimal hash string length.

§Arguments

  • hash - Hexadecimal hash string

§Returns

The detected HashAlgorithm:

  • 64 characters → SHA-256
  • 96 characters → SHA-384
  • 128 characters → SHA-512
  • Other lengths → SHA-384 (default)

§Examples

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

let sha256_hash = "a".repeat(64);
let sha384_hash = "b".repeat(96);
let sha512_hash = "c".repeat(128);

assert!(matches!(detect_hash_algorithm(&sha256_hash), HashAlgorithm::Sha256));
assert!(matches!(detect_hash_algorithm(&sha384_hash), HashAlgorithm::Sha384));
assert!(matches!(detect_hash_algorithm(&sha512_hash), HashAlgorithm::Sha512));