[][src]Function extendhash::sha256::extend_hash

pub fn extend_hash(
    hash: [u8; 32],
    length: usize,
    additional_input: &[u8]
) -> [u8; 32]

Calculate a SHA-256 hash extension.

Arguments

  • hash - The SHA-256 hash of some previous (unknown) data
  • length - The length of the unknown data (without any added padding)
  • additional_input - Additional input to be included in the new hash.

Returns

This function returns the SHA-256 hash of the concatenation of the original unknown data, its padding, and the additional_input. You can see the included (intermediate) padding by calling sha256::padding_for_length.

Example

let secret_data = "This is a secret!".as_bytes();
let hash = sha256::compute_hash(secret_data);
let secret_data_length = secret_data.len();

// Now we try computing a hash extension, assuming that
// `secret_data` is not available. We only need `hash`
// and `secret_data_length`.
let appended_message = "Appended message.".as_bytes();
let combined_hash = sha256::extend_hash(
    hash, secret_data_length, appended_message);

// Now we verify that `combined_hash` matches the
// concatenation (note the intermediate padding):
let mut combined_data = Vec::<u8>::new();
combined_data.extend_from_slice(secret_data);
let padding = sha256::padding_for_length(secret_data_length);
combined_data.extend_from_slice(padding.as_slice());
combined_data.extend_from_slice(appended_message);
assert_eq!(
    combined_hash,
    sha256::compute_hash(combined_data.as_slice()));