Record Class Hash

java.lang.Object
java.lang.Record
org.htmlunit.csp.value.Hash
Record Components:
algorithm - the hash algorithm (SHA-256, SHA-384, or SHA-512)
base64ValuePart - the Base64-encoded hash value (case-sensitive)

public record Hash(Hash.Algorithm algorithm, String base64ValuePart) extends Record
Represents a CSP hash-source value, e.g. 'sha256-abc123...'.

A hash-source pairs a hash Hash.Algorithm with a Base64-encoded digest value. The algorithm prefix (sha256, sha384, sha512) is parsed case-insensitively, but the Base64 digest itself is case-sensitive.

Author:
Salvation Team
See Also:
  • Constructor Details

    • Hash

      public Hash(Hash.Algorithm algorithm, String base64ValuePart)
      Creates an instance of a Hash record class.
      Parameters:
      algorithm - the value for the algorithm record component
      base64ValuePart - the value for the base64ValuePart record component
  • Method Details

    • getAlgorithm

      public Hash.Algorithm getAlgorithm()
      Returns the hash algorithm.
      Returns:
      the Hash.Algorithm for this hash-source
    • getBase64ValuePart

      public String getBase64ValuePart()
      Returns the Base64-encoded hash value.

      This value is case-sensitive and may use standard Base64 or Base64url encoding.

      Returns:
      the Base64-encoded digest string
    • parseHash

      public static Optional<Hash> parseHash(String value)
      Parses a hash-source from its CSP string representation.

      The input must be a single-quoted string of the form 'sha256-<base64>', 'sha384-<base64>', or 'sha512-<base64>'. The algorithm prefix is matched case-insensitively; the Base64 portion is preserved as-is.

      Parameters:
      value - the CSP hash-source token (e.g. 'sha256-abc123...')
      Returns:
      an Optional containing the parsed Hash, or empty if the value does not match the hash-source grammar
    • toString

      public String toString()
      Returns the CSP string representation of this hash-source, e.g. 'sha256-abc123...'.
      Specified by:
      toString in class Record
      Returns:
      the single-quoted hash-source string
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • algorithm

      public Hash.Algorithm algorithm()
      Returns the value of the algorithm record component.
      Returns:
      the value of the algorithm record component
    • base64ValuePart

      public String base64ValuePart()
      Returns the value of the base64ValuePart record component.
      Returns:
      the value of the base64ValuePart record component