Skip to content

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

@loewexy

Description

@loewexy

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions