actually I am stuck with the following business case and do not have an idea how to solve it.
I have to create more than 5.000.000 unique alphanumeric codes.
The rules for the codes are:
length: 12
format: every 4 digits "-"
some letters should be excluded like: O or l
The codes should be "secure" (i.e totally random) and it should be possible to run the script multiple times in case the codes aren't enough and we have to create more codes.
e.g. ab4D-406a-BCh7-TEs3
I have to solve this in Python 3.
My first idea was to save the codes into an database and just create them with the random function ASCII-Code -> Letter but maybe the scirpt creates the same code twice so I habe to check every time if that code already exists in the database which will cause a lot of database traffic.
My second idea is to use a hash function, but I think the codes wouldn't be secure and there are no hashfunctions which pass my rules.
My third idea is to use somethink like a random module from python to create the code and write the codes into an file and check the file every time if the code is already inside. But that's also not good for performance but I think betther than using a database.
Anybody an idea how to solve that problem with high performance?
Greetings.
Edit:
I tried this but it takes hours to create the codes. Some tipps how to increase the performance?
import random
sequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
seq = list(sequence)
codelist = []
counter = 0
while len(codelist) < 5000000:
code = ""
counter = counter +1
print(counter)
while len(code) < 12:
code = code + str(random.choice(seq))
try:
codelist.index(code)
except ValueError:
codelist.append(code)
file = open('codefile.txt','w')
for item in codelist:
file.write("%s\n" % item)
答案 0 :(得分:1)
加密保证了唯一性。如果您加密数字0,1,2,... 5,000,000,您将获得5,000,001保证的唯一结果,前提是您不要更改密钥。
您的下一个问题是如何将生成的二进制数更改为所需的格式。完整的字母数字使用26 + 26 + 10 = 62个字符。您使用的是其子集,因此您将使用较少的字符,例如58个字符。这意味着您可以将输出视为12位数的基数58(或其他)数字。
基数58(或其他)中的12位数将允许您调整加密的二进制块的大小。查看Format Preserving Encryption以确保加密输出的大小符合您的要求。
答案 1 :(得分:0)
The easiest way to generate unique alphanumeric codes is to generate a uuid, but they dont match your 'rules' - they are longer:
>>> import uuid
>>> _id = uuid.uuid4()
>>> print (_id)
5d9efd48-661f-47f8-8886-13e93fd8b899
>>> print (len(str(_id)))
36
>>>
答案 2 :(得分:0)
from threading import Thread
UUIDs = []
for i in range(100):
t = Thread(target= generate_alphanum, args=(UUIDs,))
t.start()
def generate_alphanum(g_list):
while len(g_list) < 50000:
uid = ''.join(random.choice(string.ascii_letters + string.digits[2:]) for _ in range(12))
if uid not in g_list:
g_list(uid[:4] + '-' + uid[4:8] + '-' + uid[8:])
caution: this may not guarantee complete randomness, but gets the job done. sample output:
'FD58-KGIo-yBGL',
'q9jv-tDa4-K3ae',
'BrGr-AO9o-GkfN',
'VyKb-NHh2-HRHM',
'g3Eu-aPsv-2YgF',
'iPxB-p4GV-f5tM',
'jewn-NWnM-kUDw',
'gDWY-MZB4-OysT',
'Acbu-kpTG-TCMm',
'rHBz-yJca-s9aA',
'2nnH-WFgT-gQef',
'2qSz-kX8z-qDpi',
'FnjV-sgzj-gzWt',
'5uwW-jwM5-FxB6',