Skip to content

Instantly share code, notes, and snippets.

@maxrt101
Last active August 10, 2022 00:18
Show Gist options
  • Save maxrt101/630419bc1657dfda2f1221e3b9cceddd to your computer and use it in GitHub Desktop.
Save maxrt101/630419bc1657dfda2f1221e3b9cceddd to your computer and use it in GitHub Desktop.
Generate password-like symbol combinations of arbitrary length
#!/usr/bin/env python3
# combination generator by maxrt101
import itertools
import hashlib
import sys
def generate(digits: int, src: list) -> list:
return list(itertools.product(*([src] * digits)))
def hash(hash_type: str, src: list) -> list:
if hash_type == 'none':
return src
elif hash_type == 'sha256':
return map(lambda s: hashlib.sha256(s.encode()).hexdigest(), src)
else:
print(f'Error: unknown hash type "{hash_type}"')
exit(1)
def main():
if len(sys.argv) > 1 and sys.argv[0] in ['h', 'help', '--help']:
print(f'Usage {sys.argv[0]} [n=4] [src=0123456789] [hash=sha256]')
exit(1)
config = {
'n': 4,
'src': '0123456789',
'hash': 'sha256'
}
for i in range(1, len(sys.argv)):
try:
tokens = sys.argv[i].split('=')
if len(tokens) != 2:
print(f'Error: unknown argument "{sys.argv[i]}"')
exit(1)
if tokens[0] == 'n':
config['n'] = int(tokens[1])
elif tokens[0] == 'src':
config['src'] = tokens[1]
elif tokens[0] == 'hash':
config['hash'] = tokens[1]
else:
print(f'Error: unknown argument "{sys.argv[i]}"')
exit(1)
except Exception as e:
print(f'Error: {e}')
exit(1)
result = generate(config['n'], config['src'])
hashed = hash(config['hash'], list(''.join(x) for x in result))
print('\n'.join(f'{"".join(i)} {j}' for i, j in zip(result, hashed)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment