reinventing the wheel again

I knew this script looked familiar, I just wrote the same script as I wrote almost a year ago. I guess I didn't do a good job getting it on the google's, cause I couldn't find it until I started posting about it...github:pass_to_shadow.py

Maybe this time I'll include some of the code in this post.


#!/usr/bin/python

# * Thu Jul 11 2013 Thomas Uphill
# - encrypt a password with sha512 by default
# - generate a crypt suitable for placement in /etc/shadow

import crypt
from random import randint
import string
import getpass
from optparse import OptionParser
import sys

# command line arguments
help_text = """%prog [options]
encrypt a password in a format suitable for /etc/shadow
sha512 is used by default
salt is calculated using randint if not supplied
"""
parser = OptionParser(usage=help_text)
parser.add_option("-s","--salt", dest="salt",action="store",type="string",
help="encryption salt", metavar="salt")
parser.add_option("-p","--password", dest="passwd",action="store",type="string",
help="password to encrypt", metavar="password")
parser.add_option("-a","--algorithm", dest="algo",action="store",type="string",
help="algorithm to use (sha512, sha256, md5",metavar="algorithm")

(options,args) = parser.parse_args()

# generate salt
try:
slen = len(options.salt)
salt = options.salt
if slen < 8:
sys.stderr.write("WARNING: Salt length should be at least 8 characters\n")
except:
salt_string = './' + string.ascii_letters + string.digits
salt = ''.join([salt_string[randint(0,len(salt_string)-1)] for x in range(8)])

try:
plen = len(options.passwd)
passwd = options.passwd
if plen < 8:
sys.stderr.write("WARNING: Password should be at least 8 characters\n")
except:
passwd = getpass.getpass()
plen = len(passwd)
if plen < 8:
sys.stderr.write("WARNING: Password should be at least 8 characters\n")

try:
len(options.algo)
algos = {'sha512': 6,
'sha256': 5,
'md5': 1,
}
try:
algo = algos[options.algo]
except:
sys.stderr.write("ERROR: Unknown encryption algorithm %s" % options.algo)
sys.exit(1)
except:
algo = '6'
print crypt.crypt(passwd, "$%s$%s" % (algo,salt))

About the Author...

Slides from LISA 2019 Linux systems troubleshooting #LISA2019 https://t.co/D4dMKflK6R Tue Oct 29 05:59:30 +0000 2019

https://t.co/AGeihMALAv configuring grub2 with EFI Fri Sep 13 05:20:01 +0000 2019

I published a Thing on @thingiverse! https://t.co/IYpRyEb7Hz #thingalert Tue Jul 23 19:27:57 +0000 2019

Nokogiri install on MacOSX https://t.co/v3An0miW9L Fri Jul 12 15:06:49 +0000 2019

HTML email with plain mailer plugin on Jenkins https://t.co/Z6FSDMDjy8 Thu Jul 11 21:07:25 +0000 2019