def OTP_synchronous_generator(license_id, downloader, username, phone, expDate, random_code): # Static random (6 digits) lay tu database static_random = 'asfb86' expDate = expDate.strftime('%Y%m%d%H%M%S%f') hash_value = hash_generator(license_id + downloader, username + phone, expDate + static_random) hash_value_base32 = b32encode(hash_value.encode('ascii')) hotp = HOTP(hash_value_base32) OTP_value = hotp.at(int(random_code)) return OTP_value
class TwoFactorCode: def __init__(self, name, issuer, codetype, codestr, counter, pos, ui, imagedata): # generate the uuid used as the dict key later # we want this to be a string self.uuid = uuid4().__str__() # grab values from call self.name = name self.issuer = issuer self.codetype = codetype self.codestr = codestr self.ui = ui self.counter = counter self.imagedata = imagedata self.pos = pos # check code type, make code object for type, # or throw exception if not recognized if codetype == 'totp': self.code = TOTP(self.codestr) self.curcode = self.code.now() elif codetype == 'hotp': self.code = HOTP(self.codestr) self.curcode = self.code.at(self.counter) else: raise TypeError('codetype was not hotp or totp') def set_counter(self, value): # set the counter to a specified value if self.counter is not None: self.counter = value.__int__() def get_current_code(self): # return current code (plaintext) if self.codetype == 'totp': return self.code.now().__str__() elif self.codetype == 'hotp': return self.code.at(self.counter).__str__()
def OTP_generator(): # Lay sessionID cua user session_id = 'abacavasdf' base32secret = random_base32() hotp = HOTP(base32secret) counter = random_digit() OTP_value = hotp.at(counter) time_create = datetime.now().strftime('%Y%m%d%H%M%S%f') # Tao data cho file json dict = { session_id: counter, 'base32secret': base32secret, 'time_create': time_create } # Neu file ton tai va khac rong if not path.isfile('./OTP.json') or read_file('./OTP.json') == b'': generate_json_file('./OTP.json', 'OTP', dict) else: append_json_file('./OTP.json', 'OTP', dict, session_id) print(OTP_value) return OTP_value
def OTP_generator(request): # Lay sessionID cua user # a = ShareFile.objects.latest('date_create') a = request.session session_id = a.session_key base32secret = random_base32() hotp = HOTP(base32secret) counter = random_digit() OTP_value = hotp.at(counter) time_create = datetime.now().strftime('%Y%m%d%H%M%S%f') # Tao data cho file json dict = { session_id: counter, 'base32secret': base32secret, 'time_create': time_create } # Neu file ton tai va khac rong if file_is_not_existed('./OTP.json'): generate_json_file('./OTP.json', 'OTP', dict) else: append_json_file('./OTP.json', 'OTP', dict, session_id) return OTP_value