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

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


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

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

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

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

About the Author...

Slides from LISA 2019 Linux systems troubleshooting #LISA2019 Tue Oct 29 05:59:30 +0000 2019 configuring grub2 with EFI Fri Sep 13 05:20:01 +0000 2019

I published a Thing on @thingiverse! #thingalert Tue Jul 23 19:27:57 +0000 2019

Nokogiri install on MacOSX Fri Jul 12 15:06:49 +0000 2019

HTML email with plain mailer plugin on Jenkins Thu Jul 11 21:07:25 +0000 2019