Make SHA1 encryption on Android?

Can you suggest me how to encrypt a string using the SHA1 algorithm? I was looking for it. But no luck.

Thanks in advance.

+4
java android encryption sha1
Apr 22 '11 at 15:33
source share
4 answers

The binnyb convertToHex method does convertToHex work correctly. More correct for me is:

 private static String convertToHex(byte[] data) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < data.length; i++) { int halfbyte = (data[i] >>> 4) & 0x0F; int two_halfs = 0; do { if ((0 <= halfbyte) && (halfbyte <= 9)) { buf.append((char) ('0' + halfbyte)); } else { buf.append((char) ('a' + (halfbyte - 10))); } halfbyte = data[i] & 0x0F; } while(two_halfs++ < 1); } return buf.toString(); } public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] sha1hash = new byte[40]; md.update(text.getBytes("iso-8859-1"), 0, text.length()); sha1hash = md.digest(); return convertToHex(sha1hash); } 

use the SHA1 method to get the sha1 string.

Update: providing a complete answer

+9
Nov 08 2018-11-11T00:
source share

Here are 2 methods I found when looking for an implementation of the sha1 algorithm:

 private static String convertToHex(byte[] data) { StringBuffer buf = new StringBuffer(); int length = data.length; for(int i = 0; i < length; ++i) { int halfbyte = (data[i] >>> 4) & 0x0F; int two_halfs = 0; do { if((0 <= halfbyte) && (halfbyte <= 9)) buf.append((char) ('0' + halfbyte)); else buf.append((char) ('a' + (halfbyte - 10))); halfbyte = data[i] & 0x0F; } while(++two_halfs < 1); } return buf.toString(); } public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] sha1hash = new byte[40]; md.update(text.getBytes("iso-8859-1"), 0, text.length()); sha1hash = md.digest(); return convertToHex(sha1hash); } 

use the SHA1 method to get the sha1 string. I have not confirmed that this is really sha1, but it works for my applications.

+2
Apr 22 2018-11-22T00:
source share

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

+2
Mar 26 '13 at 11:03
source share

binnyb put me on the right track, but I found a little more clear code here: http://www.coderanch.com/t/526487/java/java/Java-Byte-Hex-String

 private static String convertToHex(byte[] data) { StringBuilder sb = new StringBuilder(data.length * 2); Formatter fmt = new Formatter(sb); for (byte b : data) { fmt.format("%02x", b); } return sb.toString(); } 
0
Aug 29 '12 at 7:57
source share



All Articles