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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

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