EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Cyber Security

Hash and MAC Algorithms

SHA-1, or Secure Hash Algorithm 1, is a cryptographic algorithm that generates a 160-bit (20-byte) hash value from an input. This hash value, often referred to as the message digest, is usually represented as a 40-character hexadecimal number. Initially designed by the United States National Security Agency (NSA), SHA-1 became a U.S. Federal Information Processing Standard. However, it has been considered insecure since 2005, with major tech companies like Microsoft, Google, Apple, and Mozilla ceasing to accept SHA-1 SSL certificates by 2017.

SHA-1 Hash

SHA-1 Algorithm Overview

The SHA-1 algorithm involves several key components and processes to generate a hash. Here’s a breakdown of each step involved:

Components and Process Flow:

  1. Message (M): The original input message that needs to be hashed.
  2. Message Padding: The message is padded to meet the length requirement, ensuring the message’s length is congruent to 448 modulo 512. This step prepares the message for processing in 512-bit blocks.
  3. Round Word Computation (WtW_tWt): After padding, the message is split into 512-bit blocks, which are then divided into 16 words of 32 bits. These words are expanded into 80 32-bit words, which are used in the rounds.
  4. Round Initialization (A, B, C, D, and E): Five working variables (A, B, C, D, and E) are initialized with specific constant values, which are used in iterative calculations.
  5. Round Constants (KtK_tKt): SHA-1 uses four constant values applied to different rounds:
    • K1 for rounds 0-19
    • K2 for rounds 20-39
    • K3 for rounds 40-59
    • K4 for rounds 60-79
  6. Rounds (0-79): The main processing loop consists of 80 rounds, divided into four stages, each using different constants. In each round, logical operations are performed on the working variables (A, B, C, D, and E) using the message words.
  7. Final Round Addition: After all 80 rounds, the final values of the working variables are added to the original hash values.
  8. MPX (Multiplexing): The results from the final addition are combined to form the final message digest.

Summary:

  • Input (Message M): The process starts with the input message.
  • Message Padding: The message is padded to meet the necessary length.
  • Word Computation: The padded message is split into blocks and further into words, which are then expanded.
  • Initialization: Initial hash values are set.
  • Round Processing: The 80 rounds of processing are performed using the words and constants.
  • Final Addition: The round results are added to the initial hash values.
  • Output (Hash Value): The final hash value is generated.

Cryptographic Hash Functions in Java

In Java, the MessageDigest class from the java.security package is used to calculate cryptographic hash values. The following hash functions are supported:

  • MD2
  • MD5
  • SHA-1
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512

These algorithms can be initialized using the static getInstance() method. After selecting the algorithm, the message digest is calculated and returned as a byte array. The BigInteger class can be used to convert the byte array to its signum representation, which is then converted into hexadecimal format to produce the final message digest.

3. Hash Function: A hash function is a mathematical process that compresses input data into a fixed-length numeric value. Regardless of the input length, the output remains consistent in size, known as the hash value or message digest.

Example of SHA-1 in Java

  1. Input: hello world
    Output:
     2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
  2. Input: GeeksForGeeks
    Output:
     addf120b430021c36c232c99ef8d926aea2acd6b

Java Program to Compute SHA-1 Hash

// Java program to calculate SHA-1 hash value
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class GFG {
    public static String encryptThisString(String input) {
        try {
            // getInstance() method is called with algorithm SHA-1
            MessageDigest md = MessageDigest.getInstance("SHA-1");

            // digest() method is called to calculate the message digest of the input string
            byte[] messageDigest = md.digest(input.getBytes());

            // Convert byte array into signum representation
            BigInteger no = new BigInteger(1, messageDigest);

            // Convert message digest into hex value
            String hashtext = no.toString(16);

            // Add preceding 0s to make it 40 digits long
            while (hashtext.length() < 40) {
                hashtext = "0" + hashtext;
            }

            // return the HashText
            return hashtext;
        }
        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    // Driver code
    public static void main(String args[]) throws NoSuchAlgorithmException {
        System.out.println("HashCode Generated by SHA-1 for:");

        String s1 = "GeeksForGeeks";
        System.out.println("\n" + s1 + " : " + encryptThisString(s1));

        String s2 = "hello world";
        System.out.println("\n" + s2 + " : " + encryptThisString(s2));
    }
}

Output:

HashCode Generated by SHA-1 for:

GeeksForGeeks : addf120b430021c36c232c99ef8d926aea2acd6b

hello world : 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
End of lesson.