pub(crate) fn hash_or_randomize_secret<T: ByteConvertible<T> + RandomGenerator<T>>(
    secret: Option<&String>
) -> T
Expand description

Hashes the provided secret string or generates a random value.

This function takes an optional secret string and performs one of two actions:

  • If a secret string is provided, it hashes the string using SHA-512 and then converts the hash to the specified type T.
  • If no secret is provided (i.e., None), it generates a random value of type T.

Type Parameters

  • T: The target type for the hashed or random value. The type must implement ByteConvertible to allow conversion from bytes to T, and RandomGenerator to allow generation of a random value of type T.

Parameters

  • secret: An Option<&String> representing the secret string to hash.
    • Some(&String): The string to hash.
    • None: Indicates that a random value should be generated instead of hashing.

Returns

Returns a value of type T. The value is either:

  • The hash of the provided secret string, converted to type T, or
  • A randomly generated value of type T, if no secret string was provided.

Panics

This function may panic in the following cases:

  • If conversion from the hash bytes to the target type T fails.
  • If random value generation for the target type T fails.

Examples

let secret = Some(String::from("my_secret"));
let hashed_secret: [u8; 64] = hash_or_randomize_secret(secret.as_ref());
// hashed_secret is now the SHA-512 hash of "my_secret", as an array of bytes.

let random_secret: [u8; 64] = hash_or_randomize_secret(None);
// random_secret is now a randomly generated array of bytes.