Promotion code generation using python

Using python, what would be a smart / efficient way to generate promotional codes. As for creating special numbers for discount coupons. e.g. 1027828-1

thank

+3
source share
4 answers

1027828-1 is very small. An attacker can make about a million assumptions using just a few lines of code, and possibly a few days.

Python, Linux Windows. base64 , , , urllib.urlencode(), base10, .

import os
import base64

def secure_rand(len=8):
    token=os.urandom(len)
    return base64.b64encode(token)

print(secure_rand())

, , base256. 256 ^ 8 - 18446744073709551616, .

, base64 . , url-safe base64 , , humanhash, .

+5

pythonic , :

 import random
 def get_promo_code(num_chars):
     code_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     code = ''
     for i in range(0, num_chars):
         slice_start = random.randint(0, len(code_chars) - 1)
         code += code_chars[slice_start: slice_start + 1]
     return code
+7

6- #, , :

import random
print str(random.randint(100000, 999999))

...

+1

, , , .

  • .
  • ; [10, 20].

, :

def code(seed = None):
    if (not seed) or (type(seed) != str) or (len(seed) < 10):
        seed = str(uuid.uuid4())[:10]

    code = ""
    for character in seed:
        value = str(ord(character))
        code += value

    return code[:20]

. ASCII, .

: '97534957569756524557' . ...

code("pcperini answer") == '11299112101114105110'
code(str(time.time())) == '49524956514950505257'
0

Source: https://habr.com/ru/post/1729449/


All Articles