smilevchy
5/25/2015 - 2:56 PM

MD5FileNameGenerator.java

public class MD5FileNameGenerator {
	private static final String HASH_ALGORITHM = "MD5";
	private static final int RADIX = 10 + 26;    // 10 digits + 26 letters


	public String generate(String imageUri) {
		byte[] md5 = getMD5(imageUri.getBytes());
		BigInteger bi = new BigInteger(md5).abs();
		
		return bi.toString(RADIX);
	}

	private byte[] getMD5(byte[] data) {
		byte[] hash = null;
		
		try {
			MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
			digest.update(data);
			hash = digest.digest();
		} catch (NoSuchAlgorithmException e) {
			System.out.println(e);
		}

		return hash;		
	}
}