[php] TOTPのワンタイムパスワードを生成する
問題
TOTPのワンタイムパスワードをphpで生成したいです。
答え
例
// ライブラリを利用するか、関数を自作してbase32_decodeしてください
$binary_secret = Base32::decode($secret);
$digits = 6;
$period = 30;
$now = time();
$current_step = ($now - $now % $period) / $period;
$binary_timestamp = pack('N*', 0) . pack('N*', $current_step);
$hash = hash_hmac('sha1', $binary_timestamp, $binary_secret, true);
$offset = ord($hash[19]) & 0xF;
$otp = (
((ord($hash[$offset + 0]) & 0x7F) << 24) |
((ord($hash[$offset + 1]) & 0xFF) << 16) |
((ord($hash[$offset + 2]) & 0xFF) << 8) |
(ord($hash[$offset + 3]) & 0xFF)
) % pow(10, $digits);
$otp = str_pad($otp, $digits, '0', STR_PAD_LEFT);
【php】どうやってデバッグしますか? at softelメモ 2010年3月11日 10:57
[...] または、古いphpでは error_log() [...]