mirror of
https://github.com/python/cpython.git
synced 2025-09-30 20:31:52 +00:00
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 312ffead1e
)
This commit is contained in:
parent
472cc9f366
commit
c8b6506404
1 changed files with 4 additions and 3 deletions
|
@ -506,8 +506,9 @@ to users and later verify them to make sure they weren't tampered with::
|
||||||
>>> AUTH_SIZE = 16
|
>>> AUTH_SIZE = 16
|
||||||
>>>
|
>>>
|
||||||
>>> def sign(cookie):
|
>>> def sign(cookie):
|
||||||
... h = blake2b(data=cookie, digest_size=AUTH_SIZE, key=SECRET_KEY)
|
... h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY)
|
||||||
... return h.hexdigest()
|
... h.update(cookie)
|
||||||
|
... return h.hexdigest().encode('utf-8')
|
||||||
>>>
|
>>>
|
||||||
>>> cookie = b'user:vatrogasac'
|
>>> cookie = b'user:vatrogasac'
|
||||||
>>> sig = sign(cookie)
|
>>> sig = sign(cookie)
|
||||||
|
@ -517,7 +518,7 @@ to users and later verify them to make sure they weren't tampered with::
|
||||||
True
|
True
|
||||||
>>> compare_digest(b'user:policajac', sig)
|
>>> compare_digest(b'user:policajac', sig)
|
||||||
False
|
False
|
||||||
>>> compare_digesty(cookie, '0102030405060708090a0b0c0d0e0f00')
|
>>> compare_digest(cookie, b'0102030405060708090a0b0c0d0e0f00')
|
||||||
False
|
False
|
||||||
|
|
||||||
Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used
|
Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue