SHA−512 (Secure Hash Algorithm 512-bit)



SHA-512, or Secure Hash method 512, is a hashing technique that converts text of arbitrary length into a fixed-size string. Each output has a SHA-512 length of 512 bits (64 bytes).

This algorithm is frequently used for email address hashing, password hashing, and digital record verification. SHA-512 is also used in blockchain technology, with the BitShares network becoming the most known example.

In this chapter we will look at the origins of SHA-512 and how it works with the application of this algorithm.

What is SHA-512?

SHA-512 generates a hash value of 512 bits (64 bytes), making it one of the largest hash functions in the SHA-2 family. SHA-512, like all cryptographic hash algorithms, has the following basic properties −

  • Deterministic − The same input will always get the same result.
  • Fast to compute − The hash for any given data can be calculated very quickly.
  • Irreversible − You can not determine the original input from its hash.
  • Collision-resistant − It is computationally challenging to discover two distinct inputs that generate the same hash.
  • Avalanche effect − A small change in input (even flipping a single bit) results in a significantly different hash.

How SHA-512 Works?

Without going too far into the mathematical concepts, SHA-512 operates as follows −

  • Initialization − It starts with eight hash values calculated from the square roots of the initial eight prime numbers.
  • Pre-processing − The input message is padded so that it is a multiple of the Block size. The original message's 128-bit length (before padding) is added to the very end of the padded message.
  • Parsing − The message is then separated into 1024-bit parts.
  • Main Loop − The main loop analyses each 1024-bit block in 80 rounds, manipulating the data via logical operations, bitwise shifts, and modular arithmetic.
  • Output − After all of the blocks have been processed, the resulting 512-bit message digest is output as the hash.

Algorithm

The SHA-512 algorithm consists of the following steps −

  • Message Padding − First, your message is padded to ensure that it is the correct size for the algorithm. This ensures that it can be broken down into blocks and processed.
  • Initial hash values − The algorithm starts with eight initial hash values. These set values serve as the basis for the hashing procedure.
  • Message processing − The padded message is divided into blocks. Each block progresses over a series of stages known as rounds. In each round, the block is mixed and adjusted using specific techniques.
  • Final hash value − After all blocks have been examined, the hash value is computed. This hash value serves as a unique fingerprint for the original message.
  • Output − The SHA-512 algorithm generates the final hash result, which is generally a string of hexadecimal integers. This is the value returned after hashing your original message.
SHA-512 Algorithm

Implementation of SHA-512

Now we will implement the SHA-512 algorithm using different programming language.

Using Python

As we have seen the implementation of SHA-1 and SHA-256 in the previous chapters just like those implementations we will implement this algorithm but there is a slight change in the function we have used to generate a new hash object. So we will use hashlib.sha512() in this code.

import hashlib

# generate a string to hash
my_string = "Hello, Tutorialspoint family!"

# chaneg the string to bytes 
my_bytes = my_string.encode('utf-8')

# generate a new SHA-512 hash object
hash_object = hashlib.sha512()

# Update the hash object
hash_object.update(my_bytes)

# get the hexadecimal representation
hash_hex = hash_object.hexdigest()

# print the SHA-512 hash
print("SHA-512 hash of", my_string, ":", hash_hex)

Output

SHA-512 hash of Hello, Tutorialspoint family! : 7adcec675a2bae322d62c0266d148fbd9fcd65a8d9707c2e44f2ebc8138d4facf97b1079db71d9ff9e484fdcd1dd869b1c24086f3e58c6697e9db88074a2f831

Using Java

Now we will implement SHA-512 using Java and its built-in class MessageDigest and NoSuchAlgorithmException. With MessageDigest class of java we can provide cryptographic functionalities and use 'computeSHA512()' method which takes a message as input and computes its SHA-512 hash value. And NoSuchAlgorithmException class is used to throw an exception if there no such algorithm is found. So the code for SHA-512 using Java is as follows −

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA512Example {
   // Function to compute SHA-512 hash
   public static String computeSHA512(String message) throws NoSuchAlgorithmException {
      // Create a MessageDigest object for SHA-512
      MessageDigest digest = MessageDigest.getInstance("SHA-512");

      // Update the digest with the message bytes
      byte[] hashedBytes = digest.digest(message.getBytes());

      // Convert hashed bytes to hexadecimal representation
      StringBuilder hexString = new StringBuilder();
      for (byte b : hashedBytes) {
         String hex = Integer.toHexString(0xff & b);
         if (hex.length() == 1) hexString.append('0');
         hexString.append(hex);
      }

      // Return the hexadecimal representation
      return hexString.toString();
   }

   public static void main(String[] args) {
      String message = "Namaste, Tutorialspoint Family!";
      try {
         String hashedMessage = computeSHA512(message);
         System.out.println("SHA-512 Hash of '" + message + "' is: " + hashedMessage);
      } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
      }
   }
}

Input/Output

SHA-512 Hash of 'Namaste, Tutorialspoint Family!' is: 140b8ea03714699c3738071bff5446587a8cf8ecfa092ed0f49115ee8ad9617055fcfc99a484e8e16987b9a7241a8314b1a85c8e49b7c8cb2de1e276cb2a3290

Applications

SHA-512 and its siblings from the SHA-2 family are commonly used in a number of security applications and protocols, including −

  • Digital signatures are used to validate the integrity of a message or document.
  • Certificate creation is a process used by Certificate Authorities (CAs) to assure the security of digital certificates.
  • Password hashing involves storing passwords in databases as hashes rather than plain text.
  • Blockchain and cryptocurrencies: Used to ensure data integrity and security.
Advertisements