# Hash Functions

By [Bless Hukporti](https://paragraph.com/@bless-hukporti) · 2024-07-10

---

Hash functions are very important in cryptography, in this article were going to take a deep look on hash functions, how to build one, their importance, how they work and their practical use cases.

### What is a Hash Function?

A hash function is a mathematical algorithm that takes an input (or 'message') and returns a fixed-size string of bytes ( an output). The output, typically a hash value or digest, appears random and is unique to the input. Hash functions are designed to be fast and efficient, producing the hash value quickly, regardless of the size of the input data.

Now that we know what a hash function is, it’s important we take a look of it’s properties.

### Properties of Hash Functions

For a hash function to be considered cryptographically secure, it must satisfy several key properties:

1.  **Deterministic**: The same input will always produce the same output.
    
2.  **Quick Computation**: The hash function should be able to process data quickly.
    
3.  **Preimage Resistance**: Given a hash value, it should be computationally impractical to find the original input.
    
4.  **Second Preimage Resistance**: It should be computationally impractical to find a different input with the same hash value as a given input.
    
5.  **Collision Resistance**: It should be computationally impractical to find two different inputs that produce the same hash value.
    
6.  **Avalanche Effect**: A small change in the input should produce a significantly different hash value.
    

### How Hash Functions Work

Let's take an example of how a hash function works in practice:

1.  **Input**: A message or data input.
    
2.  **Processing**: The hash function processes the input through a series of complex mathematical operations.
    
3.  **Output**: A fixed-size hash value is produced.
    

For instance, using SHA-256:

*   Input: "Hello, World!"
    
*   Output: "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B9DAA41A3D99C0DFB"
    

**BUILDING A SECURE HASH FUNCTION IN RUST**
-------------------------------------------

#### **Step 1: Setting Up Your Rust Project**

First, ensure you have Rust installed on your system. If you haven't installed Rust yet, you can do so by following the instructions on the official Rust website: [https://www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install).

Once Rust is installed, you can create a new Rust project by running the following command in your terminal:

    cargo new secure_hash
    cd secure_hash
    

This will create a new directory named `secure_hash` with a basic Rust project structure.

#### **Step 2: Adding Dependencies**

To use SHA-256 in Rust, you'll need to add the `sha2` crate to your project. Open the `Cargo.toml` file in your project directory and add the `sha2` dependency under `[dependencies]`:

    [dependencies]
    sha2 = "0.9.8"
    

This tells Cargo, Rust's package manager, to download and compile the `sha2` crate when you build your project.

#### **Step 3: Implementing the Secure Hash Function**

Now, let's implement the secure hash function in Rust. Open the `src/main.rs` file in your project directory and replace its contents with the following code:

    /**
     * @title Secure Hash
     * @author Bless Hukporti
     * @notice Impliments Hash Function Implimentation that meets the requirements of collision resistance and avalanche effect.  
     * @dev Implementation of a secure hash function in Rust that uses the SHA-256 algorithm
     */
    use sha2::{Digest, Sha256};
    
    fn secure_hash(input: &str) -> String {
        // This creates a new Sha256 hasher
        let mut hasher = Sha256::new();
    
        hasher.update(input.as_bytes());
    
        let result = hasher.finalize();
        let hash_hex = format!("{:x}", result);
    
        hash_hex
    }
    
    fn main() {
        let input_data = "Bless, Hukporti";
        let hashed_result = secure_hash(input_data);
        println!("input: {}", input_data);
        println!("Hashes_Result: {}", hashed_result);
    }
    
    /*
    input: hash, algorithm
    Hashes_Result: b71505e25a00aa3287d76ef0417b02c1dc289b0e214fac78d85e683aa170e4f4 */
    
    /*
    input: Bless, Hukporti
    Hashes_Result: 570412ec3f38069ae3caf4c266383ecd6cb726b82a5beee5b5491ba52baee465 */
    

This code defines a function `secure_hash` that takes a string slice (`&str`) as input, hashes it using SHA-256, and returns the hash as a hexadecimal string. The `main` function demonstrates how to use `secure_hash` to hash a sample string and print the result.

#### **Step 4: Building and Running Your Project**

With your code written, you can now build and run your Rust project. In your terminal, navigate to your project directory and run:

    cargo build
    
    cargo run
    

This command compiles your project and runs the resulting binary. You should see the SHA-256 hash of the string "Hello, World!" printed to the console.

**Implementation Details**
--------------------------

The `secure_hash` function takes a string as input and returns a hexadecimal string representing the hash of the input. The function uses the `Sha256` hasher from the `sha2` crate to compute the hash.

The `main` function demonstrates how to use the `secure_hash` function. It computes the hash of the string `"Bless, Hukporti"` and prints the result to the console.

**Step-by-Step Explanation**
----------------------------

Here is a step-by-step explanation of the code:

1.  The `secure_hash` function is defined. It takes a string as input and returns a hexadecimal string representing the hash of the input.
    
2.  The function creates a new `Sha256` hasher.
    
3.  The hasher is updated with the bytes of the input string.
    
4.  The hasher is finalized and the result is stored in a variable.
    
5.  The result is formatted as a hexadecimal string and returned.
    
6.  The `main` function is defined. It demonstrates how to use the `secure_hash` function.
    
7.  The `main` function computes the hash of the string `"Bless, Hukporti"` and prints the result to the console.
    

Adjust "Bless, Hukporti" in the main function as needed.

---

*Originally published on [Bless Hukporti](https://paragraph.com/@bless-hukporti/hash-functions)*
