Class ScramEngine


  • public final class ScramEngine
    extends Object
    Core SCRAM-SHA-256 cryptographic operations (RFC 5802, RFC 7804). Thread-safe: all methods are stateless.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] computeClientKey​(byte[] saltedPassword)
      ClientKey := HMAC(SaltedPassword, "Client Key")
      static byte[] computeClientProof​(byte[] clientKey, byte[] clientSignature)
      ClientProof := ClientKey XOR ClientSignature
      static byte[] computeClientSignature​(byte[] storedKey, String authMessage)
      ClientSignature := HMAC(StoredKey, AuthMessage)
      static byte[] computeSaltedPassword​(String password, byte[] salt, int iterations)
      SaltedPassword := Hi(Normalize(password), salt, i)
      static byte[] computeServerKey​(byte[] saltedPassword)
      ServerKey := HMAC(SaltedPassword, "Server Key")
      static byte[] computeServerSignature​(byte[] serverKey, String authMessage)
      ServerSignature := HMAC(ServerKey, AuthMessage)
      static byte[] computeStoredKey​(byte[] clientKey)
      StoredKey := H(ClientKey)
      static String generateNonce​(int lengthBytes)
      Generate a cryptographically random nonce, base64-encoded.
      static byte[] hash​(byte[] data)
      Compute SHA-256 hash.
      static byte[] hi​(String normalizedPassword, byte[] salt, int iterations)
      PBKDF2 / Hi() computation as defined in RFC 5802.
      static byte[] hmac​(byte[] key, byte[] data)
      Compute HMAC-SHA-256.
      static String normalizePassword​(String password)
      Normalize password per RFC 7613 OpaqueString profile.
      static byte[] xor​(byte[] a, byte[] b)
      XOR two equal-length byte arrays.
      static void zeroBytes​(byte[] array)
      Zero out a byte array for security.
    • Method Detail

      • hi

        public static byte[] hi​(String normalizedPassword,
                                byte[] salt,
                                int iterations)
        PBKDF2 / Hi() computation as defined in RFC 5802. Hi(str, salt, i) = PBKDF2(str, salt, i, dkLen)
        Parameters:
        normalizedPassword - the normalized password (String, UTF-8 encoded internally)
        salt - the salt bytes
        iterations - the iteration count
        Returns:
        the derived key bytes
      • hmac

        public static byte[] hmac​(byte[] key,
                                  byte[] data)
        Compute HMAC-SHA-256.
      • hash

        public static byte[] hash​(byte[] data)
        Compute SHA-256 hash.
      • xor

        public static byte[] xor​(byte[] a,
                                 byte[] b)
        XOR two equal-length byte arrays. Mutates array a in-place.
      • generateNonce

        public static String generateNonce​(int lengthBytes)
        Generate a cryptographically random nonce, base64-encoded.
        Parameters:
        lengthBytes - number of random bytes (before base64 encoding)
        Returns:
        base64-encoded nonce string
      • normalizePassword

        public static String normalizePassword​(String password)
        Normalize password per RFC 7613 OpaqueString profile. Steps applied in order: 1) width mapping (preserve), 2) non-ASCII spaces to U+0020, 3) case preserved, 4) NFC normalization, 5) prohibited char check.
      • computeSaltedPassword

        public static byte[] computeSaltedPassword​(String password,
                                                   byte[] salt,
                                                   int iterations)
        SaltedPassword := Hi(Normalize(password), salt, i)
      • computeClientKey

        public static byte[] computeClientKey​(byte[] saltedPassword)
        ClientKey := HMAC(SaltedPassword, "Client Key")
      • computeStoredKey

        public static byte[] computeStoredKey​(byte[] clientKey)
        StoredKey := H(ClientKey)
      • computeServerKey

        public static byte[] computeServerKey​(byte[] saltedPassword)
        ServerKey := HMAC(SaltedPassword, "Server Key")
      • computeClientSignature

        public static byte[] computeClientSignature​(byte[] storedKey,
                                                    String authMessage)
        ClientSignature := HMAC(StoredKey, AuthMessage)
      • computeClientProof

        public static byte[] computeClientProof​(byte[] clientKey,
                                                byte[] clientSignature)
        ClientProof := ClientKey XOR ClientSignature
      • computeServerSignature

        public static byte[] computeServerSignature​(byte[] serverKey,
                                                    String authMessage)
        ServerSignature := HMAC(ServerKey, AuthMessage)
      • zeroBytes

        public static void zeroBytes​(byte[] array)
        Zero out a byte array for security.