19

What i want is to be able to get the sha1 hashed value of a particular password.

So for instance if my password was "hello" what command would i need to type into linux to get the sha1 hashed value of hello?

I tried

echo -n "hello" | sha1sum

but the value it returned did not give a value that was accepted by the database stored procedure that takes in the hashed value to verify a login(which the issue is not in this stored procedure because we use it all over the place for verfication purposes).

BASICALLY,

i just need to know a command to give a string and get back the sha1 hashed value of it

Thanks! :)

6
  • 2
    Your problem is missing salt. Either one in one half or the other.
    – SLaks
    Mar 25 '13 at 22:33
  • 1
    Both Python and sha1sum return aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d. Are you sure there's no salt appended to the password before it's hashed?
    – Blender
    Mar 25 '13 at 22:33
  • I guess if my command is not wrong it must be something else. Thanks for looking at this then!
    – Dan
    Mar 25 '13 at 22:42
  • ended being a salt issue
    – Dan
    Mar 27 '13 at 16:42
  • Hey Dan, any chance you can accept an answer that answered your question?
    – Dom
    Nov 25 '16 at 12:20
31

I know this is really old but here is why it did not work and what to do about it:

When you run the

echo -n "hello" | sha1sum

as in your example you get

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d  -

Notice the '-' in the end.

The hash in front is the correct sha1 hash for hello, but the dash messes up the hash.

In order to get only the first part you can do this:

echo -n "hello" | sha1sum | awk '{print $1}'

This will feed your output through awk and give you only the 1st column. Result: The correct sha1 for "hello"

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

Hope this helps someone.

2
  • the OP already commented it's a salt issue. and it's not the good practice to use only the hash value (MD5, SHA1, ...) as a password.
    – pynexj
    Jun 24 '17 at 15:26
  • 3
    I totally agree with not using md5 or sha1 etc unsalted as a password. I actually missed the fact that he no longer needed the answer. It just struck me that the command he used did not fully work as it would output an additional dash in the end. As his question was "I just need to know a command to give a string and get back the sha1 hashed value of it" and I did not see that fully answered, I chipped in. Thanks for pointing this out.
    – Dom
    Jun 26 '17 at 6:07
6
echo -n YOUR_TEXT | sha1sum | awk '{print $1}'

exchange YOUR_TEXT with the text you have to hashing it.

5

The password format may be different in different applications. For example, for /etc/passwd you can generate a SHA-256 password with:

# perl -e 'print crypt("password", q($5$salt$)), "\n";'
$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1
#

For passwords in LDAP (e.g. for slapd.conf), it may be:

# slappasswd -h "{SSHA}"
New password:
Re-enter new password:
{SSHA}bjEe8dPBjyecc7hD1kUhxQUdF9dt4Hya
#

You need to know the exact password format for your application and how the passwords are generated.

2
  • Hi I have two questions... Is the top method still secure? And why is the salt outputting directly to the hash? Isn't it supposed to be added before hashing? Thanks ahead! Feb 5 '18 at 19:48
  • 1) I cannot tell for sure if it's secure since this is very subjective. On my Debian 9 it still supports md5, sha256 and sha512. 2) Yes the salt would impact the hash result and the salt would also be part of the password. Tha't how the system verifies the user's password: The user enters a password (plaintext), the system get the salt and recalculate the result and compare it with the password (encrypted) in /etc/passwd (actually /etc/shadow). Without the salt, the system would have no way to verify it.
    – pynexj
    Feb 6 '18 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.