2024/09/21

PythonによるHOTPとTOTPの実装

Pythonによる,HOTPとTOTP及びTOTP Encodingの実装例を書いていく.

HOTP(HMAC-based One-Time Password)ということから分かる通り,HMACを使用したOTPである.HMACに入力として,キーとカウンターを与え,結果をワンタイムパスワードとする.

TOTP(Time-based One-Time Password)はHOTPをベースとしたOTPであり,HOTPのカウンターとして時刻を与える.キーと時刻(カウンター)を入力し,結果をワンタイムパスワードとする.

現在時刻を30秒間隔で区切ることが多く,つまり30秒間隔でワンタイムパスワードが変更されることになる.時間区切り(タイムステップ)ではUNIX時間が使われる.

HOTPの実装

'''
MIT License

Copyright (c) 2023 flucium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import hmac
import hashlib

key = b"secret key"

counter = 0

hmac_result = hmac.new(key,counter.to_bytes(8,"big"),hashlib.sha1).digest()

offset = hmac_result[19] & 0x0f

bin_code = bin( (hmac_result[offset]  & 0x7f) << 24 | (hmac_result[offset+1] & 0xff) << 16 | (hmac_result[offset+2] & 0xff) <<  8 | (hmac_result[offset+3] & 0xff))

snum = int(bin_code,2)

otp = snum % 10**6

print(otp)

TOTPの実装

'''
MIT License

Copyright (c) 2023 flucium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import hmac
import hashlib
import datetime
import base64
    
# HOTP
def hotp(secret,counter):
        
    hmac_result = hmac.new(secret,counter.to_bytes(8,"big"),hashlib.sha1).digest()

    offset = hmac_result[19] & 0x0f

    bin_code = bin( (hmac_result[offset]  & 0x7f) << 24 | (hmac_result[offset+1] & 0xff) << 16 | (hmac_result[offset+2] & 0xff) <<  8 | (hmac_result[offset+3] & 0xff))

    snum = int(bin_code,2)
        
    return snum % 10**6



# TOTP
x = 30

t = (datetime.datetime.utcnow() - datetime.datetime(1970,1,1,0,0)).total_seconds()

t = int(t / x)

print("TOTP : ",hotp(b"secret key",t))

TOTP encoding

1PasswordやGoogle Authenticatorなどで使用するために(対応させるために),エンコードをする必要がある.

'''
MIT License

Copyright (c) 2023 flucium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import hmac
import hashlib
import datetime
import base64

provided = "EXAMPLE"

user = "user@example.com"

secret = base64.b32encode(b"secret key").upper().decode()

issuer = "ISSUER"

algorithm = "SHA1"

digits = 6

period = 30

uri = "otpauth://totp/{}:{}?secret={}&issuer={}&algorithm={}&digits={}&period={}".format(provided,user,secret,issuer,algorithm,digits,period)

print(uri)

RFC