Class Sha256OneTimeCodeHasher

java.lang.Object
com.irurueta.authserver.authorization.application.ott.Sha256OneTimeCodeHasher
All Implemented Interfaces:
OneTimeCodeHasher

public class Sha256OneTimeCodeHasher extends Object implements OneTimeCodeHasher
Hashes one-time codes with SHA-256 (lower-case hex) and compares them in constant time.

A plain hash of a six-digit code can be brute-forced in an instant if the table leaks. That is acceptable here only because a code is single use, lives for minutes and is burnt after a few wrong attempts, so a leaked row is useless by the time anyone could read it. Never use this hasher for a long-lived secret.

  • Field Details

  • Constructor Details

    • Sha256OneTimeCodeHasher

      public Sha256OneTimeCodeHasher()
  • Method Details

    • hash

      public String hash(String code)
      Hashes a code.
      Specified by:
      hash in interface OneTimeCodeHasher
      Parameters:
      code - the clear-text code.
      Returns:
      the hash as lower-case hex.
    • matches

      public boolean matches(String code, String hash)
      Tells whether a code produces a hash, in constant time.
      Specified by:
      matches in interface OneTimeCodeHasher
      Parameters:
      code - the clear-text code, possibly null.
      hash - the stored hash, possibly null.
      Returns:
      true when they correspond.
    • digest

      private static byte[] digest(String text)
      Computes the SHA-256 digest of a text.
      Parameters:
      text - the text.
      Returns:
      the digest bytes.