Use cases

  • Verifying if a string has been changed

Java version

  • openjdk8
  • oraclejdk9
  • openjdk9
  • oraclejdk11
  • openjdk11
  • oraclejdk13
  • openjdk13

Example Code for Java String Hashing using SHA-512, BASE64 and UTF-8 encoding

package com.cryptoexamples.java;


import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Example for hashing of a string in one method.
 * - SHA-512
 * - BASE64 encoding as representation for the byte-arrays
 * - UTF-8 encoding of String
 * - Exception handling
 */
public class ExampleHash {
  private static final Logger LOGGER = Logger.getLogger(ExampleHash.class.getName());

  /**
   * Demonstrational method that hashes the plainText.
   * @param plainText
   * @return true if hashing was successful, false otherwise
   */
  public static boolean demonstrateHash(String plainText) {
    try {
      // Get MessageDigest Instance
      MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");

      // CREATE HASH
      byte[] hashBytes = messageDigest.digest(plainText.getBytes(StandardCharsets.UTF_8));

      // CONVERT/ENCODE IN BASE64
      String hashString = Base64.getEncoder().encodeToString(hashBytes);

      LOGGER.log(Level.INFO, hashString);
      return true;
    } catch (NoSuchAlgorithmException e) {
      LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
      return false;
    }
  }

  public static void main(String[] args) {
    demonstrateHash("Text that should be authenticated by comparing the hash of it!");
  }
}

References

Authors

Kai Mindermann

Reviews

Tags: Java