やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

ワンタイムパスワード生成ライブラリの調査2

前々回のつづき。GitHubの二要素認証で使うワンタイムパスワードを生成するPythonライブラリを探す。

OTPライブラリ

Name LICENSE CopyRight
pyotp LICENSE Copyright (C) 2011-2016 Mark Percival <m@mdp.im>, Nathan Reynolds <email@nreynolds.co.uk>, and PyOTP contributors
onetimepass LICENSE.rst Copyright(C)2011-2013 Tomasz Jaskowski(https://github.com/tadeck)

今回はonetimepassを使ってみる。

インストール

C:\Python34\Scripts\pip.exe install onetimepass

Collecting onetimepass
  Downloading onetimepass-1.0.1.tar.gz
Requirement already satisfied: six in c:\python34\lib\site-packages (from onetim
epass)
Installing collected packages: onetimepass
  Running setup.py install for onetimepass ... done
Successfully installed onetimepass-1.0.1

実行

#!python3
#encoding:utf-8

import requests
import datetime
import time
import onetimepass

def get_datetime():
    r = requests.get('http://www.google.com')
    print(r.headers['Date'])
    # example: Mon, 09 Jan 2017 08:59:36 GMT
    format = '%a, %d %b %Y %H:%M:%S GMT'
    dt = datetime.datetime.strptime(r.headers['Date'], format)
    print(dt)
    return dt

def datetime_to_epoch(dt):
    return int(time.mktime(dt.timetuple()))

two_factor_secret = 'secret_code_value'
print(onetimepass.get_totp(two_factor_secret))
print(onetimepass.get_totp(two_factor_secret, clock=datetime_to_epoch(get_datetime())))

1つ目はローカル時刻、2つ目はGoogle.comサーバ時刻(UTC)でTOTPを出した。

結果

WinAuthで出力したOTPと異なる値が出た。

前々回のPyOTPライブラリと同じ結果になった。

所感

やはりWinAuthの代わりには使えない。

2つのライブラリとも同じ結果であることからも、私が何か勘違いしていると思われる。