Manual encrypting your shadow password

Your passwords are stored encrypted in the /etc/shadow file. This encryption is normally done by the passwd command, but for some cases you want to produce your own encrypted password for manually placing it in the shadow file.

With following perl oneliner you can produce encrypt your password:
# echo "EnterHereYourNonEncryptedPassword" | perl -nle 'print crypt($_, "\$1\$".join "", (".", "/", 0..9, "A".."Z", "a".."z")[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64]);'
This will print an ecrypted string on the screen:
$1$UckA8UmW$Ck0rISvkyk2rDutFT4nU20

Let’s split the oneliner in different pieces so it is easier to understand. The most important perl function, which does the actual encrypting, used in the oneliner is:
crypt("password", "salt");
The “salt” is a random string starting with $1$. Following join will produce such 8 charachters long random string:
join "", (".", "/", 0..9, "A".."Z", "a".."z")[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64];

More information about the crypt function can be found here: http://perldoc.perl.org/functions/crypt.html.
As you can notice the join is also listed on that page ;)

3 Comments

  1. Paul Cobbaut said:

    You can also use openssl:
    [root@RHEL4b ~]# openssl passwd stargate
    ZZNX16QZVgUQg

    or compile your own crypt function:
    [root@RHEL4b ~]# cat MyCrypt.c
    #include
    #include
    int main(int argc, char** argv)
    {
    printf(”%s\n”, crypt(argv[1], “01″));
    return 0;
    }
    [root@RHEL4b ~]# g++ MyCrypt.c -o MyCrypt -lcrypt
    [root@RHEL4b ~]# ./MyCrypt stargate
    01Y.yPnlQ6R.Y

  2. Artem Nosulchik said:

    Thanks, Johan for explanation of “salt” word in crypt command :)
    Paul Cobbaut, you’re star! Your openssl tip is amazing!

  3. Christian Lete said:

    This would work too:

    openssl passwd -1 -salt “tlas” “password”

Leave a Reply