Class Sha2Crypt


  • public class Sha2Crypt
    extends java.lang.Object
    SHA2-based Unix crypt implementation.

    Based on the C implementation released into the Public Domain by Ulrich Drepper <drepper@redhat.com> http://www.akkadia.org/drepper/SHA-crypt.txt

    Conversion to Kotlin and from there to Java in 2012 by Christian Hammers <ch@lathspell.de> and likewise put into the Public Domain.

    This class is immutable and thread-safe.

    Since:
    1.7
    Version:
    $Id: Sha2Crypt.java 1744746 2016-05-20 14:19:43Z sebb $
    • Constructor Summary

      Constructors 
      Constructor Description
      Sha2Crypt()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String sha256Crypt​(byte[] keyBytes)
      Generates a libc crypt() compatible "$5$" hash value with random salt.
      static java.lang.String sha256Crypt​(byte[] keyBytes, java.lang.String salt)
      Generates a libc6 crypt() compatible "$5$" hash value.
      static java.lang.String sha512Crypt​(byte[] keyBytes)
      Generates a libc crypt() compatible "$6$" hash value with random salt.
      static java.lang.String sha512Crypt​(byte[] keyBytes, java.lang.String salt)
      Generates a libc6 crypt() compatible "$6$" hash value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Sha2Crypt

        public Sha2Crypt()
    • Method Detail

      • sha256Crypt

        public static java.lang.String sha256Crypt​(byte[] keyBytes)
        Generates a libc crypt() compatible "$5$" hash value with random salt.

        See Crypt.crypt(String, String) for details.

        Parameters:
        keyBytes - plaintext to hash
        Returns:
        complete hash value
        Throws:
        java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.
      • sha256Crypt

        public static java.lang.String sha256Crypt​(byte[] keyBytes,
                                                   java.lang.String salt)
        Generates a libc6 crypt() compatible "$5$" hash value.

        See Crypt.crypt(String, String) for details.

        Parameters:
        keyBytes - plaintext to hash
        salt - real salt value without prefix or "rounds="
        Returns:
        complete hash value including salt
        Throws:
        java.lang.IllegalArgumentException - if the salt does not match the allowed pattern
        java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.
      • sha512Crypt

        public static java.lang.String sha512Crypt​(byte[] keyBytes)
        Generates a libc crypt() compatible "$6$" hash value with random salt.

        See Crypt.crypt(String, String) for details.

        Parameters:
        keyBytes - plaintext to hash
        Returns:
        complete hash value
        Throws:
        java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.
      • sha512Crypt

        public static java.lang.String sha512Crypt​(byte[] keyBytes,
                                                   java.lang.String salt)
        Generates a libc6 crypt() compatible "$6$" hash value.

        See Crypt.crypt(String, String) for details.

        Parameters:
        keyBytes - plaintext to hash
        salt - real salt value without prefix or "rounds="
        Returns:
        complete hash value including salt
        Throws:
        java.lang.IllegalArgumentException - if the salt does not match the allowed pattern
        java.lang.RuntimeException - when a NoSuchAlgorithmException is caught.