How to hash a string in Android?

I am working on an Android application and have a couple of lines that I would like to encrypt before sending to the database. I would like for something that was safe, easy to implement, to generate the same thing every time it transmitted the same data, and preferably cause the string to remain constant, no matter how the string passed to it is large. Maybe I'm looking for a hash.

+49
java android cryptography hash
Oct 14 2018-10-14
source share
12 answers

This snippet computes md5 for any line

public String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i=0; i<messageDigest.length; i++) hexString.append(Integer.toHexString(0xFF & messageDigest[i])); return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } 

Source: http://www.androidsnippets.com/snippets/52/index.html

Hope this is helpful for you.

+86
Oct 14 2018-10-14
source share

This function above ( http://www.androidsnippets.org/snippets/52/index.html ) is erroneous. If one of the digits in messageDigest is not a two character hexadecimal value (i.e. 0x09), it does not work properly because it does not fill with 0. If you search around, you will find this function and complaints about it will not work . Here's the best found in the comments section of this page , which I slightly modified:

 public static String md5(String s) { MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); digest.update(s.getBytes(Charset.forName("US-ASCII")),0,s.length()); byte[] magnitude = digest.digest(); BigInteger bi = new BigInteger(1, magnitude); String hash = String.format("%0" + (magnitude.length << 1) + "x", bi); return hash; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } 
+64
Jul 27 '11 at 16:13
source share

method does not work:

 public static String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest .getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) hexString.append(Integer.toHexString(0xFF & messageDigest[i])); return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } 

Result: 1865e62e7129927f6e4cd9bff104f0 (length 30)

working method:

 public static final String md5(final String toEncrypt) { try { final MessageDigest digest = MessageDigest.getInstance("md5"); digest.update(toEncrypt.getBytes()); final byte[] bytes = digest.digest(); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(String.format("%02X", bytes[i])); } return sb.toString().toLowerCase(); } catch (Exception exc) { return ""; // Impossibru! } } 

result: 1865e62e7129927f6e4c0d9bff1004f0 (length 32)

+21
Jul 05 '13 at 13:43 on
source share
 private static char[] hextable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static String byteArrayToHex(byte[] array) { String s = ""; for (int i = 0; i < array.length; ++i) { int di = (array[i] + 256) & 0xFF; // Make it unsigned s = s + hextable[(di >> 4) & 0xF] + hextable[di & 0xF]; } return s; } public static String digest(String s, String algorithm) { MessageDigest m = null; try { m = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return s; } m.update(s.getBytes(), 0, s.length()); return byteArrayToHex(m.digest()); } public static String md5(String s) { return digest(s, "MD5"); } 
+7
Apr 13 '11 at 2:11
source share

The answer above is almost 100% correct. It will work with unicode.

  MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); byte utf8_bytes[] = tag_xml.getBytes(); digest.update(utf8_bytes,0,utf8_bytes.length); hash = new BigInteger(1, digest.digest()).toString(16); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } 

It takes a length from an array of bytes, not a string.

+5
Aug 27 2018-11-21T00:
source share

With a @Donut solution with UTF-8 encoded characters (ex: é) you should use getBytes("UTF-8") . Here is my digest method correction:

 private static char[] hextable = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; public static String byteArrayToHex(byte[] array) { String s = ""; for (int i = 0; i < array.length; ++i) { int di = (array[i] + 256) & 0xFF; // Make it unsigned s = s + hextable[(di >> 4) & 0xF] + hextable[di & 0xF]; } return s; } public static String digest(String s, String algorithm) { MessageDigest m = null; try { m = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return s; } try { m.update(s.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); m.update(s.getBytes()); } return byteArrayToHex(m.digest()); } public static String md5(String s) { return digest(s, "MD5"); } 
+5
Oct 25 '13 at 12:30
source share

Donut solution in one function:

 private static char[] hextable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static String md5(String s) { MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); digest.update(s.getBytes(), 0, s.length()); byte[] bytes = digest.digest(); String hash = ""; for (int i = 0; i < bytes.length; ++i) { int di = (bytes[i] + 256) & 0xFF; hash = hash + hextable[(di >> 4) & 0xF] + hextable[di & 0xF]; } return hash; } catch (NoSuchAlgorithmException e) { } return ""; } 
+3
Jan 23 '13 at 8:15
source share

The following worked for me on Android without truncating any 0 infront:

 MessageDigest md = null; String digest = null; try { md = MessageDigest.getInstance("MD5"); byte[] hash = md.digest(myStringToEncode.getBytes("UTF-8")); //converting byte array to Hexadecimal String StringBuilder sb = new StringBuilder(2*hash.length); for(byte b : hash){ sb.append(String.format("%02x", b&0xff)); } digest = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return digest; 
+2
Apr 03 '14 at 9:27
source share

If you did not have security restrictions and just needed to convert String to a unique int. I write this because it is what I was looking for and achieved here.

 String my_key int my_key.hashCode() 

if you have up to 10 characters, it will even be unique. See also https://stackoverflow.com/a/377628/

+2
Mar 10 '17 at 12:21
source share

It did not disappear '0'

  public static String md5(String string) { if (TextUtils.isEmpty(string)) { return ""; } MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(string.getBytes()); String result = ""; for (byte b : bytes) { String temp = Integer.toHexString(b & 0xff); if (temp.length() == 1) { temp = "0" + temp; } result += temp; } return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } 
+2
Jul 03 '17 at 6:29
source share
 MessageDigest md = MessageDigest.getInstance("MD5"); md.update('yourstring'); byte[] digest = md.digest(); StringBuffer sb = new StringBuffer(); for (byte b : digest) { sb.append(String.format("%02x", (0xFF & b))); } 

Late for the author, but before that I get Integer.toHexString(0xff&b) , which splits the leading 0s from the hex string. It makes me fight for a long time. Hope this is helpful to some guys.

+1
Apr 01 '13 at
source share

if you use guava:

 public String generateMd5(String input) { HashFunction hf = Hashing.md5(); Hasher hasher = hf.newHasher(); HashCode hc = hasher.putString(input, StandardCharsets.UTF_8).hash(); return hc.toString(); } 
+1
Nov 23 '16 at 13:35
source share



All Articles