LoginSignup
0
1

More than 5 years have passed since last update.

phpのpassword_hashで使えるハッシュをローカルのPythonで生成!

Last updated at Posted at 2018-08-11

テストアカウントとかで簡単に使えるPython製のパスワードジェネレータです。password_hashとかpassword_verifyで確認してみて下さい。

事前準備

pip install bcrypt

コード

passward_hash.py
import bcrypt


def check_password(pw: bytes, h_pw: bytes) -> bool:
    if bcrypt.checkpw(pw, h_pw):
        return True
    else:
        return False


def get_password(pw: bytes) -> bytes:
    salt = bcrypt.gensalt(rounds=10, prefix=b'2b')
    return bcrypt.hashpw(pw, salt=salt)


def run():
    pw = input("ハッシュ化以前のパスワードを入力して下さい。").encode('utf-8')
    hash_pw = get_password(pw=pw)
    result = check_password(pw=pw, h_pw=hash_pw)
    if result:
        print("検証完了!", end='')
        print(f"\npure: {pw}")
        print(f"hashed: {hash_pw}")
    else:
        print("検証失敗!")

while True:
    run()

実行結果

GIF.gif

何回か試しに入れてみると出力する結果が毎回違っているので、あれ、これアカンやん!って思ったのですが、なんかOKみたいです。よー分からん!!

参考記事:

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1