The best implementations of Google's two factor certified Python

Source: Internet
Author: User
Tags two factor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.