Skip to content

Commit c8b6506

Browse files
miss-islingtonMariatta
authored andcommitted
Improve code examples in hashlib cookie signing (GH-3562) (GH-3566)
The `blake2b` function does not take the `data` keyword argument. The hex digest returned by sign was a string, whereas compare_digest expects bytes-like objects. Typo fix: compare_digesty -> compare_digest (cherry picked from commit 312ffea)
1 parent 472cc9f commit c8b6506

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Doc/library/hashlib.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,9 @@ to users and later verify them to make sure they weren't tampered with::
506506
>>> AUTH_SIZE = 16
507507
>>>
508508
>>> def sign(cookie):
509-
... h = blake2b(data=cookie, digest_size=AUTH_SIZE, key=SECRET_KEY)
510-
... return h.hexdigest()
509+
... h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY)
510+
... h.update(cookie)
511+
... return h.hexdigest().encode('utf-8')
511512
>>>
512513
>>> cookie = b'user:vatrogasac'
513514
>>> sig = sign(cookie)
@@ -517,7 +518,7 @@ to users and later verify them to make sure they weren't tampered with::
517518
True
518519
>>> compare_digest(b'user:policajac', sig)
519520
False
520-
>>> compare_digesty(cookie, '0102030405060708090a0b0c0d0e0f00')
521+
>>> compare_digest(cookie, b'0102030405060708090a0b0c0d0e0f00')
521522
False
522523

523524
Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used

0 commit comments

Comments
 (0)