Created
May 20, 2020 03:52
-
-
Save btbytes/87e10bc7768b67cd1435eefa332ef971 to your computer and use it in GitHub Desktop.
Shell Safe API Key / Password generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
apikeygen.py | |
Generate a shell safe API key (or password) | |
Generated string should not have any characters that need to be escaped in | |
the shell. The following characters have special meaning in some shell contexts | |
! # $ ' ( ) * , ; . < = > ? [ ] ^ { } | ~ - . : \ | |
Arguments: | |
-l --length length of the API key to be generated. Default is 32. | |
-s --source a string of characters to use | |
Revision: | |
2020-05-19: Initial revision | |
Reference: | |
https://unix.stackexchange.com/a/357932 | |
""" | |
import random | |
def apikeygen(length, source): | |
return ''.join(random.sample(source, length)) | |
if __name__ == '__main__': | |
import argparse | |
import string | |
source = string.ascii_lowercase\ | |
+ string.ascii_uppercase\ | |
+ string.digits\ | |
+ '@%_/+' | |
dirty = '@%' | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'-l', | |
'--length', | |
type=int, | |
help='length of the API key to be generated.', | |
default=32) | |
parser.add_argument( | |
'-s', | |
'--source', | |
help='a string of characters to use.', | |
default=source) | |
parser.add_argument( | |
'-c', | |
'--clickable', | |
help='make the string clickable.', | |
action='store_true', | |
default=source) | |
args = parser.parse_args() | |
source = args.source | |
if args.clickable: | |
source = ''.join([c for c in source if c not in dirty]) | |
print(apikeygen(args.length, source)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment