Docs: correct hashlib.blake2 keyed hashing example (bpo-31560)

This commit is contained in:
Dmitry Chestnykh 2017-09-23 19:18:40 +02:00 committed by Benjamin Peterson
parent c8a6e5b18d
commit aecc08ac3a
2 changed files with 9 additions and 8 deletions

View File

@ -510,15 +510,19 @@ to users and later verify them to make sure they weren't tampered with::
... h.update(cookie)
... return h.hexdigest().encode('utf-8')
>>>
>>> cookie = b'user:vatrogasac'
>>> def verify(cookie, sig):
... good_sig = sign(cookie)
... return compare_digest(good_sig, sig)
>>>
>>> cookie = b'user-alice'
>>> sig = sign(cookie)
>>> print("{0},{1}".format(cookie.decode('utf-8'), sig))
user:vatrogasac,349cf904533767ed2d755279a8df84d0
>>> compare_digest(cookie, sig)
user-alice,b'43b3c982cf697e0c5ab22172d1ca7421'
>>> verify(cookie, sig)
True
>>> compare_digest(b'user:policajac', sig)
>>> verify(b'user-bob', sig)
False
>>> compare_digest(cookie, b'0102030405060708090a0b0c0d0e0f00')
>>> verify(cookie, b'0102030405060708090a0b0c0d0e0f00')
False
Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used

View File

@ -130,9 +130,6 @@ library/exceptions,,:err,err.object[err.start:err.end]
library/functions,,:step,a[start:stop:step]
library/functions,,:stop,"a[start:stop, i]"
library/functions,,:stop,a[start:stop:step]
library/hashlib,,:vatrogasac,>>> cookie = b'user:vatrogasac'
library/hashlib,,:vatrogasac,"user:vatrogasac,349cf904533767ed2d755279a8df84d0"
library/hashlib,,:policajac,">>> compare_digest(b'user:policajac', sig)"
library/hashlib,,:LEAF,"h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,"
library/http.client,,:port,host:port
library/http.cookies,,`,!#$%&'*+-.^_`|~:

1 c-api/arg :ref PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
130 library/functions :step a[start:stop:step]
131 library/functions :stop a[start:stop, i]
132 library/functions :stop a[start:stop:step]
library/hashlib :vatrogasac >>> cookie = b'user:vatrogasac'
library/hashlib :vatrogasac user:vatrogasac,349cf904533767ed2d755279a8df84d0
library/hashlib :policajac >>> compare_digest(b'user:policajac', sig)
133 library/hashlib :LEAF h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,
134 library/http.client :port host:port
135 library/http.cookies ` !#$%&'*+-.^_`|~: