読者です 読者をやめる 読者になる 読者になる

<Python, Werkzeug> ハッシュパスワードの生成と比較

ハッシュパスワードhash passwordの生成generateと比較check

In [46]: from werkzeug.security import *

In [47]: whos
Variable                    Type                          Data/Info
-------------------------------------------------------------------
DEFAULT_PBKDF2_ITERATIONS   int                           1000
PY2                         bool                          False
SALT_CHARS                  str                           abcdefghijklmnopqrstuvwxy<...>LMNOPQRSTUVWXYZ0123456789
Struct                      type                          <class 'Struct'>
SystemRandom                type                          <class 'random.SystemRandom'>
check_password_hash         function                      <function check_password_<...>sh at 0x000000000BEF6B70>
codecs                      module                        <module 'codecs' from 'C:<...>aconda3\\lib\\codecs.py'>
gen_salt                    function                      <function gen_salt at 0x000000000BEF69D8>
generate_password_hash      function                      <function generate_passwo<...>sh at 0x000000000BEF6AE8>
hashlib                     module                        <module 'hashlib' from 'C<...>conda3\\lib\\hashlib.py'>
hmac                        module                        <module 'hmac' from 'C:\\<...>Anaconda3\\lib\\hmac.py'>
izip                        type                          <class 'zip'>
os                          module                        <module 'os' from 'C:\\Anaconda3\\lib\\os.py'>
pbkdf2_bin                  function                      <function pbkdf2_bin at 0x000000000BEF68C8>
pbkdf2_hex                  function                      <function pbkdf2_hex at 0x000000000BEF6840>
posixpath                   module                        <module 'posixpath' from <...>nda3\\lib\\posixpath.py'>
range_type                  type                          <class 'range'>
safe_join                   function                      <function safe_join at 0x000000000BEF6BF8>
safe_str_cmp                function                      <function safe_str_cmp at 0x000000000BEF6950>
starmap                     type                          <class 'itertools.starmap'>
string_types                tuple                         n=1
text_type                   type                          <class 'str'>
to_bytes                    function                      <function to_bytes at 0x000000000BBD2158>
to_native                   function                      <function to_native at 0x000000000BBD21E0>
xor                         builtin_function_or_method    <built-in function xor>

で、生成とチェック。

In [48]: generate_password_hash('hage')
Out[48]: 'pbkdf2:sha1:1000$AKUZF9gh$b2fdfca27e13fabeebb0ed8f9ed46a393f2ede74'

In [49]: p = generate_password_hash('hage')

In [50]: check_password_hash(p, 'hage')
Out[50]: True

In [51]: check_password_hash(p, 'hagesugi')
Out[51]: False

マニュアル。

Welcome | Werkzeug (The Python WSGI Utility Library)