关于谷歌验证码的问题

来源:2-15 showapiRequest解决图片验证码识别

威哥V5

2019-02-20


import hashlib
import hmac
import time
import datetime

class GoogleKey:
	def byte_secret(self,secret):
	    missing_padding = len(secret) % 8
	    if missing_padding != 0:
	       secret += '=' * (8 - missing_padding)
	    return base64.b32decode(secret, casefold=True)


	def int_to_bytestring(self,i, padding=8):
	    result = bytearray()
	    while i != 0:
	        result.append(i & 0xFF)
	        i >>= 8
	    return bytes(bytearray(reversed(result)).rjust(padding, b''))


	# 根据约定的密钥计算当前动态密码
	def generate_otp(self,secret):
	    for_time = datetime.datetime.now()
	    i = time.mktime(for_time.timetuple())
	    input = int(i / 30)
	    digest = hashlib.sha1
	    digits = 6
	    if input < 0:
	        raise ValueError('input must be positive integer')
	    hasher = hmac.new(byte_secret(secret), int_to_bytestring(input), digest)
	    hmac_hash = bytearray(hasher.digest())
	    offset = hmac_hash[-1] & 0xf
	    code = ((hmac_hash[offset] & 0x7f) << 24 |
	            (hmac_hash[offset + 1] & 0xff) << 16 |
	            (hmac_hash[offset + 2] & 0xff) << 8 |
	            (hmac_hash[offset + 3] & 0xff))
	    str_code = str(code % 10 ** digits)
	    while len(str_code) < digits:
	        str_code = '0' + str_code
	    return str_code
	    
	if __name__ == '__main__'
	    gk = GoogleKey()
	    Key = gk.generate_otp('XPFBV5JAC3FS42FE')
	    print(key)

创建好了类,需要调用方法,我要用key 这个值 ,如何使用?

写回答

1回答

Mushishi

2019-02-20

你只能够其他地方调用这个generate_otp 这个方法。

0
2
威哥V5
非常感谢!
2019-02-20
共2条回复

Selenium3与Python3实战Web自动化测试框架

打破无项目经验、无框架经验瓶颈,实战Web自动化测试框架!

1886 学习 · 1093 问题

查看课程