SHA1 Hash (original) (raw)

SHA-1 Hash

Last Updated : 1 Jun, 2026

SHA-1 (Secure Hash Algorithm 1) is a cryptographic hashing algorithm that generates a fixed 160-bit hash value from input data, mainly used to verify data integrity. It is now considered insecure due to vulnerabilities and has been replaced by stronger hashing standards.

SHA-1 Hashing Algorithm Workflow

The diagram illustrates the SHA-1 hashing process, including message padding, word computation, initialization of hash variables (A, B, C, D, E), execution of 80 rounds in four stages (0–19, 20–39, 40–59, 60–79), and final hash value generation.

SHA_1_Algorithm_Block_Diagram

SHA-1 Hash

Working of SHA-1

The block diagram of the SHA-1 algorithm. Here’s a detailed description of each component and process in the diagram:

Process Flow

Cryptographic Hash Functions in Java

In Java, cryptographic hash values are generated using the MessageDigest class from the java.security package.

Execution Process

Example Inputs and Outputs

Example Implementations

**Example 1: Implementation of SHA-1 in Java

Java `

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 message digest of the input string
        // returned as array of byte
        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;
    }

    // For specifying wrong message digest algorithms
    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

**Explanation: This program uses the MessageDigest class to generate a SHA-1 hash for a given string. The resulting hash is converted into a 40-character hexadecimal value and displayed.

**Example 2: Implementation of SHA-1 in PHP

PHP `

"); $myString = "hello world"; echo $myString." : "; echo sha1($myString); echo ("
"); $myString2 = "GeeksForGeeks"; echo $myString2." : "; echo sha1($myString2); ?>

`

**Output:

HashCode Generated by SHA-1 for:
hello world : 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
GeeksForGeeks : addf120b430021c36c232c99ef8d926aea2acd6b

**Explanation: This program uses PHP's built-in sha1() function to generate the SHA-1 hash of a string. The generated hash value is then printed on the webpage.

**Example 3: Implementation of SHA-1 in JavaScript

HTML `

sha1 Hash function