Last active
October 10, 2024 14:55
-
-
Save mbreese/19e1e2009f5e51d327a1077cf78c8441 to your computer and use it in GitHub Desktop.
bcrypt
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
#!/home/mbreese/.local/src/bcrypt-venv/bin/python3 | |
import sys | |
import bcrypt | |
def usage(): | |
sys.stderr.write('''\ | |
Encrypting text | |
Usage: bcrypt {-r} [text] | |
You can also pipe raw plaintext through stdin (will be stripped unless -r is set!) | |
Checking text | |
Usage: bcrypt -c {-r} hashtext [plaintext] | |
You can also pipe raw plaintext through stdin (will be stripped unless -r is set!) | |
Also, be sure to single quote the hashtext, if being set in a shell! | |
''') | |
sys.exit(1) | |
def encrypt(plainbytes): | |
salt = bcrypt.gensalt() | |
print("%s" % bcrypt.hashpw(plainbytes, salt).decode(('utf-8'))) | |
def checkhash(hashed, plainbytes): | |
if bcrypt.checkpw(plainbytes, hashed): | |
sys.stdout.write("OK\n") | |
else: | |
sys.stdout.write("INVALID\n") | |
if len(sys.argv) == 1: | |
plain = sys.stdin.read().strip().encode('utf-8') | |
encrypt(plain) | |
sys.exit() | |
elif len(sys.argv) == 2: | |
if sys.argv[1] == '-h' or sys.argv[1] == '--help': | |
usage() | |
elif sys.argv[1] == '-r': | |
plain = sys.stdin.buffer.read() | |
encrypt(plain) | |
sys.exit() | |
else: | |
plain = sys.argv[1].encode('utf-8') | |
encrypt(plain) | |
sys.exit() | |
elif len(sys.argv) > 2 and sys.argv[1] == '-c': | |
if len(sys.argv) == 4 and sys.argv[2] == '-r': | |
plain = sys.stdin.buffer.read() | |
hashtext = sys.argv[3].encode('utf-8') | |
checkhash(hashtext, plain) | |
sys.exit() | |
elif len(sys.argv) == 4: | |
plain = sys.argv[3].encode('utf-8') | |
hashtext = sys.argv[2].encode('utf-8') | |
checkhash(hashtext, plain) | |
sys.exit() | |
elif len(sys.argv) == 3: | |
plain = sys.stdin.read().strip().encode('utf-8') | |
hashtext = sys.argv[2].encode('utf-8') | |
checkhash(hashtext, plain) | |
sys.exit() | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a command line program that calculates and verifies bcrypt passwords. It expects for there to be a venv set (see the first line) with the python library bcrypt installed w/ pip.