Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java calculate hex representation of a SHA-1 digest of a String

Tags:

java

hash

sha1

I'm storing the user password on the db as a sha1 hash.

Unfortunately I'm getting strange answers.

I'm storing the string as this:

MessageDigest cript = MessageDigest.getInstance("SHA-1");               cript.reset();               cript.update(userPass.getBytes("utf8"));               this.password = new String(cript.digest()); 

I wanted something like this -->

aff --> "0c05aa56405c447e6678b7f3127febde5c3a9238"

rather than

aff --> �V@\D~fx����:�8

like image 274
Marcos Roriz Junior Avatar asked Dec 09 '10 16:12

Marcos Roriz Junior


People also ask

How many hex characters are in SHA1?

Returns a 40-character hex-encoded string containing the 160-bit SHA-1 message digest. These functions are synonymous.

What is Sha digestion?

The SHA digest is a short and convenient way to identify a key registered with either the mmauth show or mmremotecluster command. In theory, two keys may have the same SHA digest.

What is SHA-1 Hash function?

SHA-1 or Secure Hash Algorithm 1 is a cryptographic hash function which takes an input and produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. This message digest is usually then rendered as a hexadecimal number which is 40 digits long.


1 Answers

Using apache common codec library:

DigestUtils.sha1Hex("aff") 

The result is 0c05aa56405c447e6678b7f3127febde5c3a9238

That's it :)

like image 157
altumano Avatar answered Sep 19 '22 14:09

altumano