(Dec-14-2018, 07:20 PM)Gribouillis Wrote: A small training session in my favorite language
#!/usr/bin/env python3 import itertools as itt import os def iranduint(): yield from itt.chain.from_iterable(os.urandom(1024) for k in itt.count()) iranduint = iranduint() def irandrange(n): sz = n.bit_length() if sz > 8: raise ValueError('Expected value < 256, got', n) m = 2 ** sz assert((m/2) <= n < m) for x in iranduint: x %= m if x < n: yield x def irandchoice(n): s = set() while True: x = next(irandrange(n)) if x in s: continue s.add(x) yield x def make_key(): rotor = [x for x in range(1, 9)] alpha = [x for x in "ABCDEFGHIJKL"] chx = irandchoice(len(alpha)) core = [alpha[next(chx)] for i in range(8)] g36 = irandrange(36) ringset = [1 + next(g36) for i in range(8)] g11 = irandchoice(11) notchring = [1 + next(g11) for i in range(7)] notchring.insert(3, '') beta = ("A, B, B+, C, D, E, E+, F, G, G+, " "H, I, J, J+, K, L, M, M+ N, O, O+, " "P, Q, R, R+, S, T, T+, U, V, W, W+, " "X, Y, Z, Z+").replace(",", "").split() val = irandrange(len(beta)) ringset = [beta[next(val)] for i in range(7)] ringset.insert(3, '') return rotor, core, ringset, notchring, ringset if __name__ == '__main__': for row in make_key(): print(''.join('{:<3}'.format(x) for x in row))Starting with 3.6, one can use the secrets module instead of os.urandom
Wooow! Thank you soooooooooooooooooooooooooooooooooooooo much, really, really made my day! I just posted the same request in a forum called Stackoverflow and was voted off topic in about 15 seconds, I was very, very disappointed and just came back here to have a look and found your solution! It works perfectly, could not even dream about a better implementation! Thank you for your time and effort, it's great to know there are still helpful people who don't just say "learn Python and make one yourself" - I truly appreciate your kindness!
One last question - is it possible to add some lines to the code that achieve the following:
When running the program, is it possible to get the program first ask HOW MANY KEYS DO YOU WANT TO GENERATE? The user then enters a number between let's say 1 and 31 and then the program generates that amount of keys. This way, a full, monthly key set can be generated in a few seconds. :-)