Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hash a string in 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.

like image 496
Jorsher Avatar asked Oct 14 '10 14:10

Jorsher


People also ask

How do I create a hash string?

Getting the hash code of a string is simple in C#. We use the GetHashCode() method. A hash code is a uniquely identified numerical value. Note that strings that have the same value have the same hash code.

Can strings be hashed?

The process of hashing in cryptography is to map any string of any given length, to a string with a fixed length. This smaller, fixed length string is known as a hash. To create a hash from a string, the string must be passed into a hash function.

What is hash code in Android?

This code(FA+9qCX9VSu) is the 11 character hash code which is used to identify the application uniquely. This key is different for debug builds, release builds and play-store builds for the same app depending on the key-store file.


2 Answers

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

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

Hope this is useful for you

like image 137
Antonio Avatar answered Sep 20 '22 07:09

Antonio


That function above from (http://www.androidsnippets.org/snippets/52/index.html) is flawed. If one of the digits in the messageDigest is not a two character hex value (i.e. 0x09), it doesn't work properly because it doesn't pad with a 0. If you search around you'll find that function and complaints about it not working. Here a better one found in the comment 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 ""; } 
like image 23
Craig B Avatar answered Sep 19 '22 07:09

Craig B