Skip to content

Files

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Latest commit

f1a57df · Oct 21, 2024

History

History
55 lines (41 loc) · 4.21 KB

File metadata and controls

55 lines (41 loc) · 4.21 KB

SHA-1 Algorithm

The SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that produces a 160-bit (20-byte) hash value, which is often represented as a 40-digit hexadecimal number. It was developed by the National Security Agency (NSA) and published by the National Institute of Standards and Technology (NIST) in 1993 as a part of the Digital Signature Algorithm.

How SHA-1 Works

SHA-1 takes an input message of any length and generates a fixed-size 160-bit (20-byte) hash value. The process involves several steps:

  1. Preprocessing

    • Padding: The original message is padded with a '1' bit followed by enough '0' bits to make the message length congruent to 448 modulo 512. This ensures the length of the message is a multiple of 512 bits (64 bytes).
    • Length Append: The original length of the message (before padding) is represented as a 64-bit binary number and appended to the end of the padded message.
  2. Initialize Buffers

    • Five 32-bit buffers are used, denoted as H0, H1, H2, H3, and H4, which are initialized to specific constants derived from the square roots of prime numbers:
      • H0 = 0x67452301
      • H1 = 0xEFCDAB89
      • H2 = 0x98BADCFE
      • H3 = 0x10325476
      • H4 = 0xC3D2E1F0
  3. Processing the Message in 512-bit Chunks

    • The padded message is divided into blocks of 512 bits (64 bytes). Each block is processed through 80 rounds, utilizing bitwise operations, modular addition, and logical functions.
    • Message Schedule Creation: The 512-bit block is divided into 16 words of 32 bits each, which are then expanded into an 80-word schedule using bitwise operations.
    • Main Loop: During each round, a different logical function is applied, and the five buffers are updated using the message schedule. This process involves mixing the buffers in a complex way to produce the hash.
  4. Updating the Buffers

    • After processing each block, the intermediate hash values are added to the existing buffer values. This process ensures that each block of the message contributes to the final hash.
  5. Producing the Final Hash

    • Once all blocks have been processed, the final hash value is generated by concatenating the five buffers (H0, H1, H2, H3, H4). This results in a 160-bit (20-byte) output, typically represented as a 40-digit hexadecimal number.

Applications of SHA-1

SHA-1 is widely used in various applications where data integrity verification is essential:

  • Digital Signatures: It is used in digital signature algorithms to verify the authenticity and integrity of digital documents.
  • Secure Communication Protocols: SHA-1 is integrated into protocols such as TLS/SSL for ensuring secure communication over networks.
  • Checksum and Data Integrity Verification: It can be used to detect alterations or corruption in data files.
  • Version Control Systems: Systems like Git use SHA-1 for identifying commits uniquely.

How to Implement SHA-1

Implementing SHA-1 involves following the step-by-step process outlined above. Libraries in various programming languages (JavaScript, Python, C++, etc.) provide built-in support for computing SHA-1 hashes, making it easier to integrate into projects.

For example, in JavaScript, a basic implementation could involve creating functions for padding the input, initializing buffers, processing blocks, and producing the final hash. Using these functions, the SHA-1 algorithm can compute the hash of a given input string.

How to Use This Code

  • First, import the sha1 from the directory where it is exported as a constant.

  • The sha1 object contains two methods:

    • hash: Takes a string as input and returns its corresponding SHA-1 hash value.
    • compare: Takes a string and a SHA-1 hash string, and returns true if the hash value of the input string matches the given SHA-1 hash; otherwise, it returns false.

References