VOOZH about

URL: https://www.geeksforgeeks.org/java/sha-1-hash-in-java/

⇱ SHA-1 Hash - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

  • Produces a 160-bit (40-character hexadecimal) hash output
  • Formerly used as a Federal Information Processing Standard (FIPS)
  • Broken by collision attacks, making it unsafe for modern security use

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

  • Message (M): The original input data that needs to be hashed.
  • Message Padding: The message is padded so its length becomes 448 modulo 512, making it ready for block processing.
  • Word Computation: The padded message is divided into 512-bit blocks, each split into 16 words, which are then expanded into 80 words.
  • Initialization: Five working variables A, B, C, D, and E are initialized with predefined constant values.
  • Round Constants: Four constants (K1 to K4) are used across different round ranges (0–19, 20–39, 40–59, 60–79).
  • 80 Rounds Processing: The algorithm performs 80 iterations, applying logical operations and transformations on A–E using the expanded words and constants.
  • Final Addition: The results of the rounds are added to the initial values of A–E to form the intermediate hash.
  • Message Digest (MPX): All values are combined to produce the final 160-bit hash output.

Cryptographic Hash Functions in Java

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

  • Supported Algorithms: Java supports multiple hashing algorithms, including MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.

Execution Process

  • Select algorithm using MessageDigest.getInstance()
  • Input data is processed into a byte array
  • Byte array is converted using BigInteger
  • Final output is displayed as a hexadecimal string

Example Inputs and Outputs

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

Example Implementations

Example 1: Implementation of SHA-1 in Java


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

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

Output:

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

Example: This example uses the js-sha1 library to calculate SHA-1 hashes. The generated hash values are displayed dynamically in the browser using JavaScript.

Applications of SHA-1

SHA-1 is widely used in various domains to ensure data security, integrity, and authenticity.

  • Cryptography: Generates a fixed, irreversible hash to verify data authenticity during transmission.
  • Data Integrity: Detects any data modification by comparing original and current hash values.
  • Digital Signatures: Ensures authenticity by hashing data and encrypting it with a private key.
  • Digital Forensics: Verifies that digital evidence has not been altered during investigation.
  • Password Storage: Stores passwords securely as hash values instead of plain text.
  • Software Updates: Confirms file integrity by matching downloaded file hash with the original.
Comment
Article Tags:
Article Tags: