I answered this before ( How is the SHA1 hash string in Android? ), But it works here too:
Android comes with the Apache Commons codec, so you can simply use the following line to create the SHA-1 hexadecimal string:
String myHexHash = DigestUtils.shaHex(myFancyInput);
This is an old deprecated method that you get with Android 4 by default. Newer versions of DigestUtils bring all the flavors of shaHex () methods, such as sha256Hex (), and also overload methods with different types of arguments.
Of course, DigestUtils and the rest of Commons Codec have more features. Just take a look.
http://commons.apache.org/proper/commons-codec//javadocs/api-release/org/apache/commons/codec/digest/DigestUtils.html
EDIT:
If you get a ClassNotFoundError, you have to explicitly add the Commons codec to the dependency (although it should appear with Android as a transitive dependency), in Maven, for example:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.7</version> </dependency>
In addition, you will need to change the call:
String myHexHash = new String(Hex.encodeHex(DigestUtils.sha512(myFancyInput)));
(My humble hunch is that this is probably due to the ClassLoader problem (collision of class names) in the Android VM, which actually proves that the commons-codec classes are already present ...)
See also: http : //stackoverflow.com
Risadinha Mar 26 '13 at 11:03 2013-03-26 11:03
source share