cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

Add possibility to pass data to finalize() on hashes.Hash

Open loewexy opened this issue 2 weeks ago • 3 comments

It would be very helpful if the finalize() method of a hash would also accept data. This would make it very easy to calculate a hash of some known data in one statement.

Currently you have to use something like:

digester = hashes.Hash(hashes.SHA256())
digester.update(data)
hash = digester.finalize()

With that small change it could be reduced to:

hash = hashes.Hash(hashes.SHA256()).finalize(data)

This will be equally simple than the standard library hashlib:

hash = hashlib.sha256(data).digest()

Possible alternatives would be:

  • Adding a data parameter to the constructor like so: hash = hashes.Hash(hashes.SHA256(), data).finalize()
  • Adding a classmethod to the hashes.HashAlgorithm subclasses like so: hash = hashes.SHA256.digest(data)

From my POV all of them are equally acceptable. I can not evaluate which is the simplest to implement.

loewexy avatar Nov 28 '25 12:11 loewexy