SHA password hashes

From ArchWiki
Jump to navigation Jump to search
Warning: fgetty doesn't support sha512 passwords, so you'll have to switch to mingetty or you will be locked out (regular agetty supports sha512 too)

Why Should You Use SHA-2?

In common Linux distributions login passwords are hashed and stored in the file /etc/shadow. The algorithm used for that purpose is MD5 which is usually know to have some weaknesses due to collision issues. This does not mean MD5 is insecure for password hashing but it would be nice to use a more secure algorithm that has no known weaknesses. For that reason and because the basic Arch Linux installation uses MD5, it is advised to use a more robust encryption algorythm such as SHA.

In the below examples, sha512 may be used instead of sha256, for even stronger cryptography. SHA2

Editing the Necessary Files

Editing /etc/pam.d/passwd

You must be root to edit this file. What you will probably see is something like this:

#password	required difok=2 minlen=8 dcredit=2 ocredit=2 retry=3
#password	required md5 shadow use_authtok
password	required md5 shadow nullok

A more detailed explanation of those options is available in the pam man pages, but what we are interested in is the option md5.

Replace md5 to sha256, or sha512 (recommended by the NSA for RHEL5).

The rounds=N parameter is for Key Strengthening the choice of N has a more important impact on Security than the hashfunction in use! N = 65536 means that the Attacker has to compute 65536 hashes for each password he tests against the hash in your /etc/shadow, so he is slown down by that factor. Also this means that your box has to do 65536 hashes everytime you log in ... but even on slow computers that takes less than 1 second.

After doing so, the file should look like this:

#password	required difok=2 minlen=8 dcredit=2 ocredit=2 retry=3
#password	required md5 shadow use_authtok
password	required sha512 shadow nullok rounds=65536

Editing /etc/default/passwd

You will also need root access to edit this file. It most likely looks like this:

# This file contains some information for
# the passwd (1) command and other tools 
# creating or modifying passwords.

# Define default crypt hash
# CRYPT={des,md5,blowfish}

# Use another crypt hash for group passwowrds.
# This is used by gpasswd, fallback is the CRYPT entry.

# We can override the default for a special service
# by appending the service name (FILES, YP, NISPLUS, LDAP)

# for local files, use a more secure hash. We
# don't need to be portable here:
# sometimes we need to specify special options for
# a hash (variable is prepended by the name of the
# crypt hash).

# For NIS, we should always use DES:

Once again, the change is very simple. Change




or whatever SHA-2 encryption you are using.

Note: It's unclear whether this is still necessary with the /etc/shadow mechanism.

Editing /etc/login.defs

According to passwd's man page, this file has to be edited when the /etc/shadow mechanism is used for storing passwords. Add the following line (adjusted to whatever algorithm you use) to /etc/login.defs:


Final Steps

Even though you have changed the encryption, your passwords are not automatically rehashed!

To fix this, you must reset all user passwords so that they can be rehashed.

As root, the command

# passwd <username>

where <username> is the name of the user whose password you are changing, will allow you to do this. Simply re-enter their current password, and it will be rehashed to the more secure SHA-2 version!

To verify that your passwords have been rehashed, check the /etc/shadow file as root. Passwords hashed with sha256 should begin with a $5 (passwords hashed with sha512 will begin with $6).