Android String Hash hashKeyForDisk(String key)

Here you can find the source of hashKeyForDisk(String key)

Description

hash Key For Disk

Declaration

public static String hashKeyForDisk(String key) 

Method Source Code

//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {

    public static String hashKeyForDisk(String key) {
        String cacheKey;//from w  ww  .ja  v  a  2 s .co  m
        try {
            final MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(key.getBytes());
            cacheKey = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            cacheKey = String.valueOf(key.hashCode());
        }
        return cacheKey;
    }

    private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}

Related

  1. hash(String text, String algorithm)
  2. hashIt(String s)
  3. generateHash(String pText)
  4. hashAlgorithm(String hash, String text)