python 智能十六进制数生成器
python intelligent hexadecimal numbers generator
我希望能够生成 12 个字符的十六进制长链,但链中重复的相同数字不超过 2 个:00 而不是 000
因为,我知道如何生成所有可能性,包括 00000000000 到 FFFFFFFFFFF,但我知道我不会使用所有这些值,并且因为用所有可能性生成的文件的大小有很多 GB,所以我想减小大小通过避免无用的生成链。
所以我的目标是得到类似 00A300BF8911 而不是 000300BF8911 的结果
你能帮我做一下吗?
非常感谢!
您可以从每个十六进制数字两次的列表中提取一个随机序列:
digits = list('1234567890ABCDEF') * 2
random.shuffle(digits)
hex_number = ''.join(digits[:12])
如果您想允许更短的序列,您也可以将其随机化,然后用零填充空白。
import random
digits = list('1234567890ABCDEF') * 2
random.shuffle(digits)
num_digits = random.randrange(3, 13)
hex_number = ''.join(['0'] * (12-num_digits)) + ''.join(digits[:num_digits])
print(hex_number)
import string, random
source = string.hexdigits[:16]
result = ''
while len(result) < 12 :
idx = random.randint(0,len(source))
if len(result) < 3 or result[-1] != result[-2] or result[-1] != source[idx] :
result += source[idx]
您可以创建一个从 0 到 255 的数组,然后将 random.sample 与您的列表一起使用以获得您的列表
如果您两次选择了同一个,请将其从一轮的选择中删除:
import random
hex_digits = set('0123456789ABCDEF')
result = ""
pick_from = hex_digits
for digit in range(12):
cur_digit = random.sample(hex_digits, 1)[0]
result += cur_digit
if result[-1] == cur_digit:
pick_from = hex_digits - set(cur_digit)
else:
pick_from = hex_digits
print(result)
因为标题提到发电机。这是上面的生成器:
import random
hex_digits = set('0123456789ABCDEF')
def hexGen():
while True:
result = ""
pick_from = hex_digits
for digit in range(12):
cur_digit = random.sample(hex_digits, 1)[0]
result += cur_digit
if result[-1] == cur_digit:
pick_from = hex_digits - set(cur_digit)
else:
pick_from = hex_digits
yield result
my_hex_gen = hexGen()
counter = 0
for result in my_hex_gen:
print(result)
counter += 1
if counter > 10:
break
结果:
1ECC6A83EB14
D0897DE15E81
9C3E9028B0DE
CE74A2674AF0
9ECBD32C003D
0DF2E5DAC0FB
31C48E691C96
F33AAC2C2052
CD4CEDADD54D
40A329FF6E25
5F5D71F823A4
您还可以将 while true 循环更改为仅根据传入函数的数字生成一定数量的这些。
我将这个问题解释为,"I want to construct a rainbow table by iterating through all strings that have the following qualities. The string has a length of 12, contains only the characters 0-9 and A-F, and it never has the same character appearing three times in a row."
def iter_all_strings_without_triplicates(size, last_two_digits = (None, None)):
a,b = last_two_digits
if size == 0:
yield ""
else:
for c in "0123456789ABCDEF":
if a == b == c:
continue
else:
for rest in iter_all_strings_without_triplicates(size-1, (b,c)):
yield c + rest
for s in iter_all_strings_without_triplicates(12):
print(s)
结果:
001001001001
001001001002
001001001003
001001001004
001001001005
001001001006
001001001007
001001001008
001001001009
00100100100A
00100100100B
00100100100C
00100100100D
00100100100E
00100100100F
001001001010
001001001011
...
请注意,将输出价值数百 TB 的值,因此与仅保存每个字符串(无论是否一式三份)相比,您并没有节省太多空间。
您可以使用生成器对当前实现生成的字符串迭代 window。 ……像 (hex_str[i:i + 3] for i in range(len(hex_str) - window_size + 1))
使用 len
和 set
你可以计算切片中不同字符的数量。尽管在您的示例中,只比较所有 3 个字符可能更容易。
我希望能够生成 12 个字符的十六进制长链,但链中重复的相同数字不超过 2 个:00 而不是 000 因为,我知道如何生成所有可能性,包括 00000000000 到 FFFFFFFFFFF,但我知道我不会使用所有这些值,并且因为用所有可能性生成的文件的大小有很多 GB,所以我想减小大小通过避免无用的生成链。
所以我的目标是得到类似 00A300BF8911 而不是 000300BF8911 的结果
你能帮我做一下吗? 非常感谢!
您可以从每个十六进制数字两次的列表中提取一个随机序列:
digits = list('1234567890ABCDEF') * 2
random.shuffle(digits)
hex_number = ''.join(digits[:12])
如果您想允许更短的序列,您也可以将其随机化,然后用零填充空白。
import random
digits = list('1234567890ABCDEF') * 2
random.shuffle(digits)
num_digits = random.randrange(3, 13)
hex_number = ''.join(['0'] * (12-num_digits)) + ''.join(digits[:num_digits])
print(hex_number)
import string, random
source = string.hexdigits[:16]
result = ''
while len(result) < 12 :
idx = random.randint(0,len(source))
if len(result) < 3 or result[-1] != result[-2] or result[-1] != source[idx] :
result += source[idx]
您可以创建一个从 0 到 255 的数组,然后将 random.sample 与您的列表一起使用以获得您的列表
如果您两次选择了同一个,请将其从一轮的选择中删除:
import random
hex_digits = set('0123456789ABCDEF')
result = ""
pick_from = hex_digits
for digit in range(12):
cur_digit = random.sample(hex_digits, 1)[0]
result += cur_digit
if result[-1] == cur_digit:
pick_from = hex_digits - set(cur_digit)
else:
pick_from = hex_digits
print(result)
因为标题提到发电机。这是上面的生成器:
import random
hex_digits = set('0123456789ABCDEF')
def hexGen():
while True:
result = ""
pick_from = hex_digits
for digit in range(12):
cur_digit = random.sample(hex_digits, 1)[0]
result += cur_digit
if result[-1] == cur_digit:
pick_from = hex_digits - set(cur_digit)
else:
pick_from = hex_digits
yield result
my_hex_gen = hexGen()
counter = 0
for result in my_hex_gen:
print(result)
counter += 1
if counter > 10:
break
结果:
1ECC6A83EB14
D0897DE15E81
9C3E9028B0DE
CE74A2674AF0
9ECBD32C003D
0DF2E5DAC0FB
31C48E691C96
F33AAC2C2052
CD4CEDADD54D
40A329FF6E25
5F5D71F823A4
您还可以将 while true 循环更改为仅根据传入函数的数字生成一定数量的这些。
我将这个问题解释为,"I want to construct a rainbow table by iterating through all strings that have the following qualities. The string has a length of 12, contains only the characters 0-9 and A-F, and it never has the same character appearing three times in a row."
def iter_all_strings_without_triplicates(size, last_two_digits = (None, None)):
a,b = last_two_digits
if size == 0:
yield ""
else:
for c in "0123456789ABCDEF":
if a == b == c:
continue
else:
for rest in iter_all_strings_without_triplicates(size-1, (b,c)):
yield c + rest
for s in iter_all_strings_without_triplicates(12):
print(s)
结果:
001001001001
001001001002
001001001003
001001001004
001001001005
001001001006
001001001007
001001001008
001001001009
00100100100A
00100100100B
00100100100C
00100100100D
00100100100E
00100100100F
001001001010
001001001011
...
请注意,将输出价值数百 TB 的值,因此与仅保存每个字符串(无论是否一式三份)相比,您并没有节省太多空间。
您可以使用生成器对当前实现生成的字符串迭代 window。 ……像 (hex_str[i:i + 3] for i in range(len(hex_str) - window_size + 1))
使用 len
和 set
你可以计算切片中不同字符的数量。尽管在您的示例中,只比较所有 3 个字符可能更容易。