如何在Android中对字符串进行哈希处理?
我正在开发一个 Android 应用程序,并且有几个字符串我想在发送到数据库之前对其进行加密.我想要一些安全、易于实现的东西,每次传递相同的数据时都会生成相同的东西,并且无论传递给它的字符串有多大,最好都会产生一个保持恒定长度的字符串.也许我正在寻找哈希.
I am working on an Android app and have a couple strings that I would like to encrypt before sending to a database. I'd like something that's secure, easy to implement, will generate the same thing every time it's passed the same data, and preferably will result in a string that stays a constant length no matter how large the string being passed to it is. Maybe I'm looking for a hash.
推荐答案
此代码段计算任何给定字符串的 md5
This snippet calculate md5 for any given string
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
希望对你有用
相关文章