Updated : Jul 31, 2015 in Bash

Encrypt a password with different encryption methods

Situation:
I happen to have configured a Linux system with MySQL database and wanted to enter a password in the password field in the DB. Here is a way I found.
This will output in the terminal the encrypted string you can then enter directly in the password field via phpmyadmin or mysql client.
Note: The encryption method may differ between Linux distributions and versions. To find it out, you should be able to get it with this command:
grep "^ENCRYPT_METHOD" /etc/login.defs | awk '{print $2}'

Encryption with SHA512 method: (best security)
(default method used by Ubuntu 14.04-2)
echo "<username>:<clear text password>" | chpasswd -S -c SHA512 | cut -d: -f2

Encryption with SHA256 method: (little less security)
echo "<username>:<clear text password>" | chpasswd -S -c SHA256 | cut -d: -f2

Encryption with MD5 method: (no more secure !!!)
echo "<username>:<clear text password>" | chpasswd -S -c MD5 | cut -d: -f2

Encryption with DES method: (really no more secure !!!)
echo "<username>:<clear text password>" | chpasswd -S -c DES | cut -d: -f2

Note: If you need to include such command in a script to change the password of a user in the Linux system, just do as follows:
echo "<username>:<clear text password>" | chpasswd -c SHA512

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: