This version should be the best implementation, add four time points on this, you can use in the way to judge to avoid errors.
The @ code comment is actually the best explanation
Class _googletwosetpauth (object): ' Google token two authentication related ' ' Def _get_hotp_token (self, Secret, intervals_no): ' Get Htop_token Internal method algorithm:p Aram secret encoding before two authentication:p Aram intervals_no time interval ' key = Base64.b32decode ( Secret, True) msg = Struct.pack (">q", intervals_no) H = hmac.new (Key, MSG, HASHLIB.SHA1). Digest () o = Ord (h[19]) & h = (Struct.unpack (">i", H[o:o+4]) [0] & 0x7fffffff)% 1000000 return h def Get_totp_token (self, secret): "Get Totop:p Aram secret two times before the authentication code, call this method ' ' # Time sample T = Time.time () t_30 = t + t_30_ = t-30 t_60 = t-60 # 90s time, the general client difference will not exceed this, more than this does not provide Method Oold = Self._get_hotp_token (Secret, Intervals_no=int (T_60)//) Old = Self._get_hotp_token (Secret, inte Rvals_no=int (t_30_)//30) now = Self._get_hotp_token (Secret, Intervals_no=int (t)//30) New = Self._get_hotp_to Ken (Secret, Intervals_no=int (t_30)//30) return oold, old, now, new Def shutff_str (self): ' Generate random string characters 16 bits ' ' src = ' abcdefg HIJKLMNOPQRSTUVWXYZ ". Upper () secret=" ". Join (Random.sample (src,16)). Replace (', ') return secret
Google two factor certified Python best implementations