如何在Android中散列string?

我正在开发一个Android应用程序,并且在发送到数据库之前有一串我想要encryption的string。 我想要一些安全,易于实现的东西,每次传递相同的数据时都会生成相同的东西,并且最好会得到一个string,不pipe传递给它的string有多大,它都会保持一个恒定的长度。 也许我正在寻找一个哈希。

这段代码为任何给定的string计算md5

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 ""; } 

来源: http : //www.androidsnippets.com/snippets/52/index.html

希望这对你有用

上面的函数( http://www.androidsnippets.org/snippets/52/index.html )是有缺陷的。 如果messageDigest中的其中一个数字不是两个字符的hex值(即0x09),则它不能正常工作,因为它没有填充0.如果您search周围,您会发现该function和投诉不工作。 这里有一个更好的,在这个页面的评论部分 ,我稍作修改:

 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 ""; } 

不工作方法:

 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 ""; } 

结果 – 1865e62e7129927f6e4cd9bff104f0(长度30)

工作方式:

 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! } } 

结果 – 1865e62e7129927f6e4c0d9bff1004f0(长度32)

 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"); } 

上面的答案几乎是100%正确的。 它会失败与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(); } 

需要从字节数组而不是string的长度。

使用@Donut解决scheme,使用UTF-8编码字符(例如:é),您必须使用getBytes("UTF-8") 。 这里是我对摘要方法的更正:

 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"); } 

甜甜圈的解决scheme在一个单一的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 ""; } 

下面的工作在Android上我没有截断任何0的盈方:

 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; 
 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))); } 

作者迟到了,但是在这之前,我得到了Integer.toHexString(0xff&b) ,它从hexstring中Integer.toHexString(0xff&b)前面的0。 这让我奋斗了很长时间。 希望对一些人有用。

如果你正在使用番石榴:

 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(); } 

如果你没有安全约束,只想将String转换为一个唯一的int。 我正在写这篇文章是因为我在找的东西到达了这里

 String my_key int my_key.hashCode() 

如果您有多达10个字符,它将甚至是唯一的另请参见https://stackoverflow.com/a/17583653/1984636

这不是缺less“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 ""; }