<s> from base64 import b32encode <EOL> from binascii import hexlify <EOL> from urllib import urlencode <EOL> from django_twofactor . encutil import encrypt , decrypt , _gen_salt <EOL> from oath import accept_totp <EOL> from django . conf import settings <EOL> import random <EOL> try : <EOL> random = random . SystemRandom ( ) <EOL> except : <EOL> pass <EOL> TOTP_OPTIONS = getattr ( settings , "<STR_LIT>" , { } ) <EOL> PERIOD = TOTP_OPTIONS . get ( '<STR_LIT>' , <NUM_LIT:30> ) <EOL> FORWARD_DRIFT = TOTP_OPTIONS . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> BACKWARD_DRIFT = TOTP_OPTIONS . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> DEFAULT_TOKEN_TYPE = TOTP_OPTIONS . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> ENCRYPTION_KEY = getattr ( settings , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def random_seed ( rawsize = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( [ chr ( random . randint ( <NUM_LIT:0> , <NUM_LIT:255> ) ) for i in range ( rawsize ) ] ) <EOL> def encrypt_value ( raw_value ) : <EOL> salt = _gen_salt ( ) <EOL> return "<STR_LIT>" % ( salt , encrypt ( raw_value , ENCRYPTION_KEY + salt ) ) <EOL> def decrypt_value ( salted_value ) : <EOL> salt , encrypted_value = salted_value . split ( "<STR_LIT:$>" , <NUM_LIT:1> ) <EOL> return decrypt ( encrypted_value , ENCRYPTION_KEY + salt ) <EOL> def check_raw_seed ( raw_seed , auth_code , token_type = None ) : <EOL> """<STR_LIT>""" <EOL> if not token_type : <EOL> token_type = DEFAULT_TOKEN_TYPE <EOL> return accept_totp ( <EOL> auth_code , <EOL> hexlify ( raw_seed ) , <EOL> token_type , <EOL> period = PERIOD , <EOL> forward_drift = FORWARD_DRIFT , <EOL> backward_drift = BACKWARD_DRIFT <EOL> ) [ <NUM_LIT:0> ] <EOL> def get_google_url ( raw_seed , hostname = None ) : <EOL> b32secret = b32encode ( raw_seed ) <EOL> if not hostname : <EOL> from socket import gethostname <EOL> hostname = gethostname ( ) <EOL> data = "<STR_LIT>" % { <EOL> "<STR_LIT>" : hostname , <EOL> "<STR_LIT>" : b32secret , <EOL> } <EOL> url = "<STR_LIT>" + urlencode ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : data <EOL> } ) <EOL> return url </s>
