Skip to content

hashlib-digest-hex (FURB181)

Derived from the refurb linter.

Fix is sometimes available.

What it does

Checks for the use of .digest().hex() on a hashlib hash, like sha512.

Why is this bad?

When generating a hex digest from a hash, it's preferable to use the .hexdigest() method, rather than calling .digest() and then .hex(), as the former is more concise and readable.

Example

from hashlib import sha512

hashed = sha512(b"some data").digest().hex()

Use instead:

from hashlib import sha512

hashed = sha512(b"some data").hexdigest()

Fix safety

This rule's fix is marked as unsafe, as the target of the .digest() call could be a user-defined class that implements a .hex() method, rather than a hashlib hash object.

References