How to create a hash password from text

root's picture

For this example I will user md5 but in similar way you can use whatever you want in order to create a hash version of your text password.
As a security note, I do not recommend using md5 to generate hash passwords because today it is very easily broken. This is jut a proof of concept.

Unix/Linux:

1. Create file whatever.txt with the new password in it AND NOTHING ELSE (no new line, no space at the end, no nothing, just the password).
2. Execute the combination of tr and md5sum commands below:

echo my_password >whatever.txt
tr -d '\r\n' < whatever.txt | md5sum | tr -d ' -'
rm whatever.txt

You can achieve this also without creating a file, with the help of printf or echo commands. The only downside is the password will be available in commands history so creating a file is recommended. A side note for echo is to be used only with -n (do not output the trailing newline) because otherwise you will get into the hash the newline too and it is not what you really want.

printf test1234 | md5sum | tr -d ' -'
echo -n test1234 | md5sum | tr -d ' -'
Examples:
root@eave:~# echo test1234 >whatever.txt
root@eave:~# cat whatever.txt
test1234
root@eave:~# tr -d '\r\n' < whatever.txt | md5sum | tr -d ' -'
16d7a4fca7442dda3ad93c9a726597e4
root@eave:~# rm whatever.txt
root@core:~# printf test1234 | md5sum | tr -d ' -'
16d7a4fca7442dda3ad93c9a726597e4
root@core:~# echo -n test1234 | md5sum | tr -d ' -'
16d7a4fca7442dda3ad93c9a726597e4

MAC OS X

1. Create file whatever.txt with the new password in it AND NOTHING ELSE (no new line, no space at the end, no nothing, just the password).
2. Some website are recommending to use md5 -q text but it is wrong because you will hash not only the text but also the invisible newline which is added like you would normally do cat file for example, even if there is no newline in the text file. Instead, you can achieve the correct output in similar UNIX/Linux way, only to replace md5sum with md5.
Execute the combination of tr and md5 commands below:

echo my_password >whatever.txt
tr -d '\r\n' < whatever.txt | md5 | tr -d ' -'
rm whatever.txt

Similar to Linux/UNIX, you can use also printf or echo -n without creating a file, with the same warning regarding the password in commands history. Examples below.

Examples:
osx:~ osx$ echo test1234 >whatever.txt
osx:~ osx$ cat whatever.txt
test1234
osx:~ osx$ tr -d '\r\n' < whatever.txt | md5 | tr -d ' -'
16d7a4fca7442dda3ad93c9a726597e4
osx:~ osx$ rm whatever.txt
osx:~ osx$ printf test1234 | md5
16d7a4fca7442dda3ad93c9a726597e4
osx:~ osx$ echo -n test1234 | md5
16d7a4fca7442dda3ad93c9a726597e4

If you want, you can display the hash directly like in the example below, or you can copy it directly to the clipboard with the help of pbcopy command. However, this will work if you are at the computer, not connected remotely.

osx:~ osx$ tr -d '\r\n' < whatever.txt | md5 | tr -d ' -' | pbcopy
osx:~ osx$ rm whatever.txt

As bad example, you have below the output of md5 -q file as recommended in other places:

osx:~ osx$ echo test1234>whatever.txt
osx:~ osx$ echo test1234>a
osx:~ osx$ cat a
test1234
osx:~ osx$ cat whatever.txt
test1234
osx:~ osx$ md5 -q whatever.txt
eddc02b200ae8a15a7e6b44ac05bf5f1
osx:~ osx$ md5 -q a
eddc02b200ae8a15a7e6b44ac05bf5f1
osx:~ osx$ echo test1234 | md5
eddc02b200ae8a15a7e6b44ac05bf5f1

As you can easily see, the hash is totally different, only because a new line is added.

PS: for those a bit confused about the echo output in both examples (good and bad), “-n” option tells echo to not output the new line. It brings a big difference in situations where even one character matters.

Cheers!

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.