Open In App

SHA-1 Hash

SHA-1 or Secure Hash Algorithm 1 is a cryptographic algorithm which takes an input and produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. This message digest is usually then rendered as a hexadecimal number which is 40 digits long. It is a U.S. Federal Information Processing Standard and was designed by the United States National Security Agency. SHA-1 is now considered insecure since 2005. Major tech giants browsers like Microsoft, Google, Apple and Mozilla have stopped accepting SHA-1 SSL certificates by 2017. To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.security. MessageDigest Class provides following cryptographic hash function to find hash value of a text as follows:

These algorithms are initialized in static method called getInstance(). After selecting the algorithm the message digest value is calculated and the results are returned as a byte array. BigInteger class is used, to convert the resultant byte array into its signum representation. This representation is then converted into a hexadecimal format to get the expected MessageDigest. Examples:



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

Example 1: Below program shows the implementation of SHA-1 hash in Java. 






// 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 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 32 bit
            while (hashtext.length() < 32) {
                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

Example 2: Below program shows the implementation of SHA-1 hash in PHP.




<?php
    echo "HashCode Generated by SHA-1 for:";
    echo ("<br>");
    $myString = "hello world";
    echo $myString." : ";
    echo sha1($myString);
    echo ("<br>");
    $myString2 = "GeeksForGeeks";
    echo $myString2." : ";
    echo sha1($myString2);
?>

Output:

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

Example 3: Below program shows the implementation of SHA-1 hash in JavaScript. 




<!DOCTYPE html>
<html>
<head>
    <title>sha1 Hash function</title>
    <script src=
     
    </script>
</head>
<body>
    <h1>GeeksforGeeks</h1>
  
    <h2>JavaScript sha1 Hash function</h2>
    
    <p id="pId"></p>
    <p id="pId2"></p>
  
    <!-- Script to return math property values -->
    <script>      
        var myString = "hello world";
        var text = sha1(myString);
        document.getElementById("pId").innerHTML = myString + " : " + text;
        var myString2 = "GeeksForGeeks";
        var text2 = sha1(myString2);
        document.getElementById("pId2").innerHTML = myString2 + " : " + text2;           
    </script>
</body>
</html>

Output:

GeeksforGeeks
JavaScript sha1 Hash function
hello world : 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
GeeksForGeeks : addf120b430021c36c232c99ef8d926aea2acd6b

Applications:


Article Tags :