Commits

Anonymous committed 913b7d2

gen_randoam_string(), gen_nonce()

Comments (0)

Files changed (1)

src/accounts/utils.py

     else:
         redirect_to = fallback_url
     return redirect_to
+
+#
+
+def gen_random_string(length, chrs=None):
+    ''' generate random string
+
+        :param length: length of generated string 
+        :param chrs:  character restriction
+    '''
+
+    import random ,os
+
+    if chrs is None:
+        return os.urandom(length) 
+    else:
+        return ''.join([chrs[ random.SystemRandom().randrange(len(chrs))] 
+                    for _ in xrange(length)])
+
+def gen_nonce(when=None):
+    ''' generate nonce 
+
+        :param when: datetime
+    '''
+    import string
+    from datetime import datetime 
+    NONCE_CHARS = string.ascii_letters + string.digits
+    salt = gen_random_string(6, NONCE_CHARS)
+        
+    when = when if when else datetime.now() 
+    return  when.strftime('%Y%m%d%H%M%S%f'+salt)
+