2017年5月17日 星期三

Create a md5 hash from a string

Reference:

https://dzone.com/articles/android-snippet-making-md5

Code snippet:

public static String encryptByMd5Hash(String in) {
    String md5 = "";
    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));
        }
        md5 = sb.toString();
    } catch (NoSuchAlgorithmException e){
        e.printStackTrace();
    }
    return md5;
}

沒有留言: