Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Android Snippet: MAking a md5 hash from a string in Java

DZone's Guide to

Android Snippet: MAking a md5 hash from a string in Java

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Security is always an issue when you connect your application to the web. Here is a nice little method that will let you implement md5 encryption.
private String md5(String in) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for (int i = 0; i < len; i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
    return null;
}

How to use:

String unsecurepassword = "unhackablepassword";
String securepassword = md5(unsecurepassword );
Please mind that md5 is not decryptable. If you need an encryption that you can reverse you will have to look into something other then md5. ( AES for example) Android tutorial

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}