Ferdinand
Ferdinand

Reputation: 1193

Make SHA1 encryption on Android?

Can you suggest me about how to encrypt string using SHA1 algorithm ? I've searched about it. But no luck.

Thanks in advance.

Upvotes: 4

Views: 16465

Answers (4)

Mikael Engver
Mikael Engver

Reputation: 4768

binnyb's convertToHex method is not working properly. A more correct one that works for me is:

private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < data.length; i++) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if ((0 <= halfbyte) && (halfbyte <= 9)) {
                buf.append((char) ('0' + halfbyte));
            }
            else {
                buf.append((char) ('a' + (halfbyte - 10)));
            }
            halfbyte = data[i] & 0x0F;
        } while(two_halfs++ < 1);
    } 
    return buf.toString();
} 


public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
} 

use the SHA1 method to get your sha1 string.

Update: providing a complete answer

Upvotes: 9

Risadinha
Risadinha

Reputation: 16666

I've answered this before (How to SHA1 hash a string in Android?) but it fits here, as well:

Android comes with Apache's Commons Codec so you can simply use the following line to create a SHA-1 hexed String:

String myHexHash = DigestUtils.shaHex(myFancyInput);

That is the old deprecated method you get with Android 4 by default. The new versions of DigestUtils bring all flavors of shaHex() methods like sha256Hex() and also overload the methods with different argument types.

Of course, there is more functionality in DigestUtils and the rest of Commons Codec. Just have a look.

http://commons.apache.org/proper/commons-codec//javadocs/api-release/org/apache/commons/codec/digest/DigestUtils.html

EDIT:

If you get a ClassNotFoundError you will have to explicitly add commons-codec as dependency (even though it should come with Android as transitive dependency), in Maven e.g.:

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.7</version>
    </dependency>

And also, you will have to change the call to:

String myHexHash = new String(Hex.encodeHex(DigestUtils.sha512(myFancyInput)));

(My humble guess is that this is probably due to a ClassLoader issue (class name collision) in the Android VM - which would actually prove that the commons-codec classes are already present...)

See also: https://stackoverflow.com/a/9284092/621690

Upvotes: 2

Ruben
Ruben

Reputation: 806

binnyb set me on the right track, but I found some more, easier to understand code here: http://www.coderanch.com/t/526487/java/java/Java-Byte-Hex-String

private static String convertToHex(byte[] data) {
    StringBuilder sb = new StringBuilder(data.length * 2);

    Formatter fmt = new Formatter(sb);
    for (byte b : data) {
        fmt.format("%02x", b);
    }

    return sb.toString();
}

Upvotes: 0

james
james

Reputation: 26271

here are 2 methods i have found while searching for a sha1 algorithm implementation:

private static String convertToHex(byte[] data) { 
    StringBuffer buf = new StringBuffer();
    int length = data.length;
    for(int i = 0; i < length; ++i) { 
        int halfbyte = (data[i] >>> 4) & 0x0F;
        int two_halfs = 0;
        do { 
            if((0 <= halfbyte) && (halfbyte <= 9)) 
                buf.append((char) ('0' + halfbyte));
            else 
                buf.append((char) ('a' + (halfbyte - 10)));
            halfbyte = data[i] & 0x0F;
        }
        while(++two_halfs < 1);
    } 
    return buf.toString();
}

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
} 

use the SHA1 method to get your sha1 string. I have not confirmed that this is indeed a sha1, but it works for my apps.

Upvotes: 2

Related Questions