0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonでワンタイムパスワード:TOTP

0
Posted at

ワンタイムパスワードのPythonでの実装をまとめます。

インストール

pip install pyotp qrcode[pil]

QRコード生成

ここで作成するQRコードをGoogle Authenticateなどのワンタイムパスワードアプリで読み込みます。

import pyotp
import qrcode

# 1. シークレットを作成
secret = pyotp.random_base32()

# 2. TOTPオブジェクト作成
totp = pyotp.TOTP(secret)

# 3. Google Authenticator用の登録URLを作成
#    name: ユーザー名やメールアドレス
#    issuer_name: サービス名
otp_uri = totp.provisioning_uri(
    name="nozaki@sankosc.co.jp",
    issuer_name="SankoSC"
)

print("secret =", secret)
print("otp_uri =", otp_uri)

# 4. QRコード画像を作成
img = qrcode.make(otp_uri)

# 5. 保存
img.save("totp_qr.png")

認証

import pyotp

secret = "シークレット"
code = input("Enter the OTP code: ")
if pyotp.TOTP(secret).verify(code):
	print("OK")
else:
	print("NG")
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?