Android Open Source - DiskLruImageCache Digest Utils






From Project

Back to project page DiskLruImageCache.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project DiskLruImageCache listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package it.sephiroth.android.library.disklruimagecache;
//from   w  w  w. j a  v a  2  s .  c om
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

class DigestUtils {

  private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

  static MessageDigest getDigest( String algorithm ) {
    try {
      return MessageDigest.getInstance( algorithm );
    } catch( NoSuchAlgorithmException e ) {
      throw new RuntimeException( e.getMessage() );
    }
  }

  private static MessageDigest getMd5Digest() {
    return getDigest( "MD5" );
  }

  public static byte[] md5( byte[] data ) {
    return getMd5Digest().digest( data );
  }

  public static byte[] md5( String data ) {
    return md5( data.getBytes() );
  }

  public static String md5Hex( String data ) {
    return new String( encodeHex( md5( data ) ) );
  }

  /**
   * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
   * The returned array will be double the length of the passed array, as it takes two characters to represent any
   * given byte.
   *
   * @param data a byte[] to convert to Hex characters
   * @return A char[] containing hexidecimal characters
   */
  public static char[] encodeHex( byte[] data ) {

    int l = data.length;

    char[] out = new char[l << 1];

    // two characters form the hex value.
    for( int i = 0, j = 0; i < l; i++ ) {
      out[j++] = DIGITS[( 0xF0 & data[i] ) >>> 4];
      out[j++] = DIGITS[0x0F & data[i]];
    }

    return out;
  }
}




Java Source Code List

it.sephiroth.android.library.disklruimagecache.DigestUtils.java
it.sephiroth.android.library.disklruimagecache.DiskLruImageCache.java
it.sephiroth.android.library.disklruimagecache.DiskUtils.java