The Wayback Machine - https://web.archive.org/web/20240629112501/https://www.geeksforgeeks.org/sha-1-hash-in-java/
Open In App

SHA-1 Hash

Last Updated : 09 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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

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




// 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




<?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. 

HTML




<!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:

  • Cryptography: The main application of SHA1 is to protect communications from being intercepted by outside parties. From a given data input, SHA1 generates a fixed-size, singular, and irreversible hash value. The integrity of the data can then be confirmed by comparing this hash value to the original hash value. This makes it possible to confirm that the data was not changed or tampered with in any manner during transmission.
  • Data Integrity: In many industries, such as finance, healthcare, and government, data integrity is a major concern. Data integrity in a system is checked using the SHA1 algorithm. A fingerprint of the original data is created using a hash value produced by the SHA1 algorithm.  If the data changes in any way, the hash value will also change, indicating that the data has been tampered with. 
  • Digital Signatures: Digital signatures are used to confirm the legitimacy of digital documents and messages. The digital document or communication is hashed using the SHA1 technique, and its hash value is subsequently encrypted with the sender’s private key. Using the sender’s public key to decode the message, the recipient can then compare the hash value to the original value.
  • Digital Forensics: In digital forensics, a hash of a file containing digital evidence can be produced using the SHA1 algorithm. To ensure that the evidence hasn’t been altered with during the investigation, utilize this hash value as proof. It gives proof that the file has not been altered if the hash values of the original file and the evidence file match. 
  • Password Storage: SHA1 can be used to save passwords. A hash of the password is generated using SHA1 when a user creates a password. The password itself is then substituted in a database for the hash value. The user’s password is hashed with SHA1 when they attempt to log in, and the resulting hash is compared to a previously generated hash.
  • Software Updates: The integrity of software updates can be guaranteed using SHA1. The SHA1 hash of the update file can be made public on the software vendor’s website when an update is made available. By comparing the hash of the downloaded file with the published hash, users can download the update and ensure its integrity.


Previous Article
Next Article

Similar Reads

SHA-256 and SHA-3
Both the SHA-256 and SHA-3 are hashing algorithms, Both of these algorithms are very widely known and popularly used in the security as well as the cryptography of modern computers these algorithms are used in order to protect sensitive data as well as maintain the security of the systems so in this article, we will learn what SHA-256 and SHA-3 alg
6 min read
SHA-384 Hash In Java
The SHA-2 family of cryptographic hash functions consists of six hash functions. These are: SHA-224, with 224 bit hash values SHA-256, with 256 bit hash values SHA-384, with 384 bit hash values SHA-512, with 512 bit hash values SHA-512/224, with 512 bit hash values SHA-512/256, with 512 bit hash values Among these, SHA-256 and SHA-512 are the most
3 min read
SHA-224 Hash In Java
The SHA-2 family of cryptographic hash functions consists of six hash functions. These are: SHA-224, with 224 bit hash values SHA-256, with 256 bit hash values SHA-384, with 384 bit hash values SHA-512, with 512 bit hash values SHA-512/224, with 512 bit hash values SHA-512/256, with 512 bit hash values Among these, SHA-256 and SHA-512 are the most
2 min read
SHA-256 Hash in Java
Definition: In Cryptography, SHA is cryptographic hash function which takes input as 20 Bytes and rendered the hash value in hexadecimal number, 40 digits long approx.Message Digest Class: To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java.security.MessagDigest Class provides following cryptographi
2 min read
SHA-512 Hash In Java
The SHA-2 family of cryptographic hash functions consists of six hash functions. These are: SHA-224, with 224-bit hash valuesSHA-256, with 256-bit hash valuesSHA-384, with 384-bit hash valuesSHA-512, with 512-bit hash valuesSHA-512/224, with 512-bit hash valuesSHA-512/256, with 512-bit hash valuesAmong these, SHA-256 and SHA-512 are the most common
3 min read
What are Hash Functions and How to choose a good Hash Function?
Prerequisite: Hashing | Set 1 (Introduction) What is a Hash Function? A function that converts a given big phone number to a small practical integer value. The mapped integer value is used as an index in the hash table. In simple terms, a hash function maps a big number or string to a small integer that can be used as the index in the hash table. W
5 min read
Hash Functions and Types of Hash functions
Hash functions are a fundamental concept in computer science and play a crucial role in various applications such as data storage, retrieval, and cryptography. In data structures and algorithms (DSA), hash functions are primarily used in hash tables, which are essential for efficient data management. This article delves into the intricacies of hash
4 min read
Implement Secure Hashing Algorithm - 512 ( SHA-512 ) as Functional Programming Paradigm
Given a string S of length N, the task is to find the SHA-512 Hash Value of the given string S. Examples: Input: S = "GeeksforGeeks"Output: acc10c4e0b38617f59e88e49215e2e894afaee5ec948c2af6f44039f03c9fe47a9210e01d5cd926c142bdc9179c2ad30f927a8faf69421ff60a5eaddcf8cb9c Input: S = "hello world"Output:309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bc
15 min read
Data Structures | Hash | Question 1
A hash table of length 10 uses open addressing with hash function h(k)=k mod 10, and linear probing. After inserting 6 values into an empty hash table, the table is as shown below. Which one of the following choices gives a possible order in which the key values could have been inserted in the table? (A) 46, 42, 34, 52, 23, 33 (B) 34, 42, 23, 52, 3
2 min read
Data Structures | Hash | Question 2
How many different insertion sequences of the key values using the hash function h(k) = k mod 10 and linear probing will result in the hash table shown below? (A) 10 (B) 20 (C) 30 (D) 40 Answer: (C) Explanation: In a valid insertion sequence, the elements 42, 23 and 34 must appear before 52 and 33, and 46 must appear before 33. Total number of diff
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg