I.T. Security and Linux Administration

Sep 30 2013   4:14PM GMT

Create Your Own Two-Factor Authentication System: Generating Tokens

Eric Hansen Eric Hansen Profile: Eric Hansen

This will be a small blurb compared to the rest but this is rather essential. This isn’t the best way to generate tokens and in no way do I recommend you use this in a practical case. Here is the code, however (place it just below the app = … line):

def generate_token(number):
    from time import time
    charlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+/="
    num = str(number+time()).split(".")[0]
    info = [num[i:i+2] for i in range(0, len(num), 2)]
    token = ""
    for i in info:
        index = int(i[0])+int(i[1])
        token = "%s%s" % (token, str(charlist[index]))
    return token

Basically what this does is add the current time to the number provided (and since time() returns a decimal we only want the whole number). Then we split the number into multiple pairs and add both digits in the pairs, and that is our place in the token list/charlist. It’s pretty simplistic but it does our job just fine.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: