quickly generate an encrypted password


Hi everybody !  Here’s a quick method for generating encrypted passwords that are suitable for things like /etc/passwd .  I realise that this isn’t terribly complex, but honestly, I always forget how to do this until I actually need to do it – so here’s a reminder for all of us. :)

#!/bin/bash

if [ "x$1" == 'x' ]; then
echo "USAGE: $0 'password'"
exit 1
fi

# Get an md5sum of the password string; this is used for the SHA seed.
md5=$( echo $1 | md5sum )
extract="${md5:2:8}"

# Calculate the SHA hash of the password string using the extracted seed.
mkpasswd -m SHA-512 "$1" "$extract"
exit $?

,

  1. #1 by Mahesh on 2012/07/12 - 11:16

    I have named the script as password..when it is run.it is throwing below error
    USAGE: ./password ‘ ‘

  2. #2 by dan on 2012/07/13 - 12:35

    Mahesh :

    I have named the script as password..when it is run.it is throwing below error
    USAGE: ./password ‘ ‘

    Hi Mahesh,

    You need to give it the string (i.e. password) that you wish to encrypt – it will not generate a password for you, only the hash of the password you specify.

(will not be published)