本文介紹了如何在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
希望對你有用
這篇關于如何在Android中對字符串進行哈希處理?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!