Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hashing to support HMACs, update names in fs2.hashing to be clearer, introduce Digest type #3465

Merged
merged 12 commits into from
Aug 26, 2024

Conversation

mpilquist
Copy link
Member

@mpilquist mpilquist commented Aug 19, 2024

This PR adds a few features:

  • support for computing HMACs
  • SHA3 and various other modern codecs

This PR makes a number of name changes including:

  • replacing addChunk with update
  • replacing computeAndReset with hash
  • replacing hash with drain
  • renaming Hash[F] to Hasher[F]
  • renaming Hashing.create to Hashing.hasher

Additionally:

  • fs2.hashing.Hash type was introduced, wrapping a Chunk[Byte] and providing a constant time equality check
  • pipes were refactored to use Hash as the output type when appropriate

@mpilquist mpilquist changed the title Update names in fs2.hashing to be clearer, introduce Digest type Update Hashing to support HMACs, update names in fs2.hashing to be clearer, introduce Digest type Aug 24, 2024

/** Creates a new MD-5 hash. */
def md5: Resource[F, Hash[F]] = create(HashAlgorithm.MD5)
def md5: Resource[F, Hash[F]] = hash(HashAlgorithm.MD5)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undecided if we should have all these aliases. Options:

  • No aliases -- use Hashing[F].hash(HashAlgorithm.SHA256) instead
  • Aliases for most popular hashes only -- SHA1, SHA256, SHA3_256?
  • Aliases for all built-in hashes (that's current state of this PR)

We don't have aliases for HMACs (though we could) -- e.g., def hmacSha256(key: Chunk[Byte]) = hmac(hash(HashAlgorithm.SHA256), key)

Thoughts @armanbilge?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Aliases for all built-in hashes

It's not really possible to have this in general. By "built-in" we mean hashes that we have a case for. Because what's actually built-in on the runtime system can be totally variable depending on the build of the JDK/Node.js/OpenSSL. In truth, any of these methods could raise an error. Maybe no aliases would be more honest to that, although I can see how that's annoying from a usability perspective.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some docs to make the possibility of failure clear and removed all aliases.

Comment on lines 27 to 40
object HashAlgorithm {
case object MD5 extends HashAlgorithm
case object SHA1 extends HashAlgorithm
case object SHA224 extends HashAlgorithm
case object SHA256 extends HashAlgorithm
case object SHA384 extends HashAlgorithm
case object SHA512 extends HashAlgorithm
case object SHA512_224 extends HashAlgorithm
case object SHA512_256 extends HashAlgorithm
case object SHA3_224 extends HashAlgorithm
case object SHA3_256 extends HashAlgorithm
case object SHA3_384 extends HashAlgorithm
case object SHA3_512 extends HashAlgorithm
final case class Named(name: String) extends HashAlgorithm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If HashAlgorithm is sealed then we technically can't add new cases in the future. There are various annoying workarounds, we could unseal it but make the ctor private for example ... or just ignore it and break compatibility in this relatively minor way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I made it unsealed with private constructor.

@mpilquist mpilquist merged commit 333b753 into typelevel:main Aug 26, 2024
15 checks passed
@mpilquist mpilquist deleted the topic/digest branch August 26, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants