Module hash

Module hash 

Source
Expand description

§Hash Module

This module provides cryptographic hash functions for the Atlas CLI, supporting SHA-256, SHA-384, and SHA-512 algorithms. It integrates with the atlas-c2pa-lib to use consistent hash algorithm types throughout the codebase.

§Features

  • Calculate hashes of byte data with configurable algorithms
  • Calculate file hashes efficiently using streaming
  • Combine multiple hashes into a single hash
  • Verify data integrity by comparing hashes
  • Automatic algorithm detection based on hash length

§Algorithm Support

The module supports the following hash algorithms:

  • SHA-256: 256-bit hash (64 hex characters) - Default for backward compatibility
  • SHA-384: 384-bit hash (96 hex characters) - Default for new manifests
  • SHA-512: 512-bit hash (128 hex characters) - Maximum security

§Examples

§Basic hashing with default algorithm (SHA-384)

use atlas_cli::hash::calculate_hash;

let data = b"Hello, World!";
let hash = calculate_hash(data);
assert_eq!(hash.len(), 96); // SHA-384 produces 96 hex characters

§Hashing with specific algorithm

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

let data = b"Hello, World!";
let hash = calculate_hash_with_algorithm(data, &HashAlgorithm::Sha512);
assert_eq!(hash.len(), 128); // SHA-512 produces 128 hex characters

§File hashing

use atlas_cli::hash::calculate_file_hash_with_algorithm;
use atlas_c2pa_lib::cose::HashAlgorithm;
use std::path::Path;

let path = Path::new("large_file.bin");
let hash = calculate_file_hash_with_algorithm(path, &HashAlgorithm::Sha384).unwrap();
assert_eq!(hash.len(), 96); // SHA-384 produces 96 hex characters

Functions§

algorithm_to_string
Get the algorithm name as used in manifests
calculate_file_hash
Calculate SHA-256 hash of a file
calculate_file_hash_with_algorithm
Calculate hash of a file using the specified algorithm
calculate_hash
Calculate SHA-384 hash of the given data
calculate_hash_with_algorithm
Calculate hash of data using the specified algorithm
combine_hashes
This function concatenates the decoded bytes of multiple hashes and produces a new SHA-384 hash. This is useful for creating a single hash that represents multiple components.
detect_hash_algorithm
Detect hash algorithm based on hash length
get_hash_length
Get the expected hash length for an algorithm
parse_algorithm
Parse algorithm from string
verify_hash
Verify that data matches the expected hash
verify_hash_with_algorithm
Verify hash with an explicitly specified algorithm