Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SHA1 hash a string in Android?

Tags:

android

hash

sha1

In Objective C I've been using the following code to hash a string:

-(NSString *) sha1:(NSString*)stringToHash {         const char *cStr = [stringToHash UTF8String];     unsigned char result[20];     CC_SHA1( cStr, strlen(cStr), result );     return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",         result[0], result[1], result[2], result[3],          result[4], result[5], result[6], result[7],         result[8], result[9], result[10], result[11],         result[12], result[13], result[14], result[15],         result[16], result[17], result[18], result[19]         ];   } 

Now I need the same for Android but can't find out how to do it. I've been looking for example at this: Make SHA1 encryption on Android? but that doesn't give me the same result as on iPhone. Can anyone point me in the right direction?

like image 580
Martin Avatar asked May 12 '11 15:05

Martin


People also ask

What is the use of SHA-1 key in Android?

Uses of SHA-1 key: It is used for Cryptography. By using this key the input data is converted into a 160-bit hash value which is difficult to decode. These keys are also used for Data Integrity.

How do you create a SHA-1?

How to Generate SHA1 Hash? Step 1: Enter the Plain or Cypher Text. Step 2: Click on Generate SHA1 HASH Online. Step 3: Use Copy to Clipboard functionality to copy the generated SHA1 hash.

Why is SHA-1 no longer secure?

It is supposed to be unique and non-reversible. If a weakness is found in a hash function that allows for two files to have the same digest, the function is considered cryptographically broken, because digital fingerprints generated with it can be forged and cannot be trusted.

What is SHA1 hash generator?

What is a SHA-1 Hash? SHA-1 (Secure Hash Algorithm) is a 160 bit cryptographic hash function created by the NSA in 1995. It creates a 40 byte hash value for the input of the algorithm. SHA-1 is one-way, meaning that the original input cannot be be determined simply by knowing the hash value.


2 Answers

You don't need andorid for this. You can just do it in simple java.

Have you tried a simple java example and see if this returns the right sha1.

import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;  public class AeSimpleSHA1 {     private static String convertToHex(byte[] data) {         StringBuilder buf = new StringBuilder();         for (byte b : data) {             int halfbyte = (b >>> 4) & 0x0F;             int two_halfs = 0;             do {                 buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));                 halfbyte = b & 0x0F;             } while (two_halfs++ < 1);         }         return buf.toString();     }      public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {         MessageDigest md = MessageDigest.getInstance("SHA-1");         byte[] textBytes = text.getBytes("iso-8859-1");         md.update(textBytes, 0, textBytes.length);         byte[] sha1hash = md.digest();         return convertToHex(sha1hash);     } } 

Also share what your expected sha1 should be. Maybe ObjectC is doing it wrong.

like image 147
Amir Raminfar Avatar answered Oct 05 '22 06:10

Amir Raminfar


A simpler SHA-1 method: (updated from the commenter's suggestions, also using a massively more efficient byte->string algorithm)

String sha1Hash( String toHash ) {     String hash = null;     try     {         MessageDigest digest = MessageDigest.getInstance( "SHA-1" );         byte[] bytes = toHash.getBytes("UTF-8");         digest.update(bytes, 0, bytes.length);         bytes = digest.digest();          // This is ~55x faster than looping and String.formating()         hash = bytesToHex( bytes );     }     catch( NoSuchAlgorithmException e )     {         e.printStackTrace();     }     catch( UnsupportedEncodingException e )     {         e.printStackTrace();     }     return hash; }  // http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); public static String bytesToHex( byte[] bytes ) {     char[] hexChars = new char[ bytes.length * 2 ];     for( int j = 0; j < bytes.length; j++ )     {         int v = bytes[ j ] & 0xFF;         hexChars[ j * 2 ] = hexArray[ v >>> 4 ];         hexChars[ j * 2 + 1 ] = hexArray[ v & 0x0F ];     }     return new String( hexChars ); } 
like image 31
Adam Avatar answered Oct 05 '22 08:10

Adam