创建一个永不重复的随机十六进制生成器 [英] Creating a never-repeating random hexadecimal generator

查看:119
本文介绍了创建一个永不重复的随机十六进制生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司要求我为我们拥有的一些安全硬件开发一个 4 字节随机十六进制生成器,但是我对这种事情很陌生.(澄清一下,每个代码由 4 个十六进制数字组成).

My company has asked me to develop a 4-byte random hexadecimal generator for some security hardware we have, however I am new to this sort of thing. (To clarify, each code consists of 4 hexadecimal digits).

我们目前有一个正在使用的 124 个十六进制的列表,我需要能够不重复这些十六进制以及将使用此程序生成的十六进制.我已经开发了一个 GUI 和一个生成器,我只需要帮助确保它永远不会重复.

We currently have a list of 124 hexadecimals that are in use and I need to be able to not repeat those hexadecimals as well as ones that will be generated with this program. I have developed a GUI and a generator, I just need help making sure it never repeats.

这是我目前所拥有的:

# random hexadecimal generator engine 
text = "" 

for i in range(4): 
    rand = random.choice('0123456789abcdef') 
    text = text + rand 
    print(text) 

# creating the window 
root = Tk() # GUI window 
root.title("Hex Generator") 
root.geometry("250x150") 
app = Frame(root) 
app.grid() 
label = Label(app, text="Click to generate a Hexadecimal") 
button1 = Button(app, text="Generate") 
label.grid() 
button1.grid() 
label1 = Label(app, text=text) 
label1.grid() 

# kicking off event loop 
root.mainloop()

推荐答案

您的问题询问了一个 4 字节随机十六进制生成器,但在评论中您澄清说您只想要 4 个十六进制数字,这意味着只有 2**16 组合.这使问题变得相当简单:我们只需创建一个包含所有 65,536 个组合的列表并对其进行打乱,然后我们可以简单地迭代该打乱的列表.为了节省一点时间&RAM 我创建了一个整数列表 &只需根据需要将整数转换为十六进制字符串.

Your question asks about a 4-byte random hexadecimal generator, but in the comments you clarify that you only want 4 hex digits, which means there are only 2**16 combinations. That makes the problem rather easy: we just create a list of all 65,536 combinations and shuffle it, and then we can simply iterate over that shuffled list. To save a little time & RAM I create a list of integers & just convert the integers to hex strings as needed.

事实上,你有一个包含 124 个已经在使用的代码的列表,这增加了一点复杂性,但我们可以通过将这些代码放入一个集合中来解决这个问题;我们可以轻松测试生成的代码,看看它们是否在 used 集合中.

The fact that you've got a list of 124 codes that are already in use adds a little bit of complexity, but we can handle that by putting those codes into a set; we can easily test the generated codes to see if they're in the used set.

显然,我们希望能够多次运行该程序,因此我们将洗牌列表的索引号保存到一个文本文件中.这比将我们生成的每个数字存储到 used 集合中更简单、更有效.

Obviously, we want to be able to run the program multiple times, so we save the index number of the shuffled list into a text file. This is simpler and more efficient than storing each number that we generate into the used set.

我们还需要随机化保持一致,因此我们需要为随机数生成器提供一个种子.

We also need the randomization to be consistent, so we need to supply a seed to the random number generator.

这是一些 Python 3 代码.它可以适用于在 Python 2 上运行(通过将 FileNotFoundError 更改为 IOError),但是您不能在运行之间切换版本,因为随机序列Python 2 生成的数字与 Python 3 生成的数字不同

Here's some Python 3 code. It can be adapted to run on Python 2 (by changing FileNotFoundError to IOError), but you cannot switch versions between runs because the sequence of random numbers generated by Python 2 will not be the same as that generated by Python 3

from random import seed, shuffle

passphrase = 'My secret passphrase'
seed(passphrase)

# Codes that were already in use. 
# This list could be read from a file instead of being embedded in the script 
used = '''\
2b2d
40a7
c257
d929
c252
5805
2936
8b20
'''

# Convert to a set of strings
used = set(used.splitlines())

# The index of the next number to generate is stored in this file
fname = 'nextindex.txt'

try:
    with open(fname) as f:
        idx = int(f.read())
except FileNotFoundError:
    idx = 0

print('Using index', idx)

allnums = list(range(0x10000))
shuffle(allnums)

#Search for the next code that's not in the `used` set
while True:
    hexcode = format(allnums[idx], '04x')
    idx += 1
    if hexcode not in used:
        print('Code:', hexcode)
        break

# Save the next index
with open(fname, 'w') as f:
    print('Saving index', idx)
    f.write(str(idx))

3 次运行的输出

Using index 0
Code: d0fc
Saving index 1

Using index 1
Code: d7e9
Saving index 3

Using index 3
Code: fc42
Saving index 4

如您所见,索引 2 被跳过,这是因为它对应于 used 集中的代码.

As you can see, index 2 gets skipped, that's because it corresponds to a code in the used set.

这篇关于创建一个永不重复的随机十六进制生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆