Example #1
0
 /**
  * Gets this hash sum as an array of 16 bytes.
  *
  * @return Array of 16 bytes, the hash of all updated bytes.
  */
 public byte[] getHash() {
   if (!finalState.valid) {
     finalState.copy(workingState);
     long bitCount = finalState.bitCount;
     // Compute the number of left over bits
     int leftOver = (int) (((bitCount >>> 3)) & 0x3f);
     // Compute the amount of padding to add based on number of left over
     // bits.
     int padlen = (leftOver < 56) ? (56 - leftOver) : (120 - leftOver);
     // add the padding
     update(finalState, padding, 0, padlen);
     // add the length (computed before padding was added)
     update(finalState, encode(bitCount), 0, 8);
     finalState.valid = true;
   }
   // make a copy of the hash before returning it.
   return encode(finalState.state, 16);
 }
Example #2
0
 /**
  * Gets the MD5 hash the data on the given InputStream.
  *
  * @param in byte array for which an MD5 hash is desired.
  * @return Array of 16 bytes, the hash of all updated bytes.
  * @throws IOException if an I/O error occurs.
  */
 public static byte[] getHash(InputStream in) throws IOException {
   MD5 md5 = new MD5();
   byte[] buffer = new byte[1024];
   int read;
   while ((read = in.read(buffer)) != -1) {
     md5.update(buffer, read);
   }
   return md5.getHash();
 }
Example #3
0
 /**
  * Gets the MD5 hash of the given byte array.
  *
  * @param b byte array for which an MD5 hash is desired.
  * @return 32-character hex representation the data's MD5 hash.
  */
 public static String getHashString(byte[] b) {
   MD5 md5 = new MD5();
   md5.update(b);
   return md5.getHashString();
 }
Example #4
0
 /**
  * Gets the MD5 hash of the given byte array.
  *
  * @param b byte array for which an MD5 hash is desired.
  * @return Array of 16 bytes, the hash of all updated bytes.
  */
 public static byte[] getHash(byte[] b) {
   MD5 md5 = new MD5();
   md5.update(b);
   return md5.getHash();
 }
Example #5
0
 /**
  * Update this hash with a String.
  *
  * @param s String to be hashed.
  * @param enc The name of a supported character encoding.
  * @throws UnsupportedEncodingException If the named encoding is not supported.
  */
 public void update(String s, String enc) throws UnsupportedEncodingException {
   update(s.getBytes(enc));
 }
Example #6
0
 /**
  * Update this hash with a String. The string is converted to bytes using the current platform's
  * default character encoding.
  *
  * @param s String to be hashed.
  */
 public void update(String s) {
   update(s.getBytes());
 }
Example #7
0
 /**
  * Updates this hash with a single byte.
  *
  * @param b byte to be hashed.
  */
 public void update(byte b) {
   byte buffer[] = new byte[1];
   buffer[0] = b;
   update(buffer, 1);
 }
Example #8
0
 /**
  * Update this hash with the given data.
  *
  * @param buffer Array of bytes to be hashed.
  */
 public void update(byte buffer[]) {
   update(buffer, 0, buffer.length);
 }
Example #9
0
 /**
  * Update this hash with the given data.
  *
  * <p>If length bytes are not available to be hashed, as many bytes as possible will be hashed.
  *
  * @param buffer Array of bytes to be hashed.
  * @param length number of bytes to hash.
  */
 public void update(byte buffer[], int length) {
   update(buffer, 0, length);
 }
Example #10
0
 /**
  * Update this hash with the given data.
  *
  * <p>If length bytes are not available to be hashed, as many bytes as possible will be hashed.
  *
  * @param buffer Array of bytes to be hashed.
  * @param offset Offset to buffer array.
  * @param length number of bytes to hash.
  */
 public void update(byte buffer[], int offset, int length) {
   update(workingState, buffer, offset, length);
 }
Example #11
0
 /**
  * Gets the MD5 hash of the given String.
  *
  * @param s String for which an MD5 hash is desired.
  * @param enc The name of a supported character encoding.
  * @return 32-character hex representation the data's MD5 hash.
  * @throws UnsupportedEncodingException If the named encoding is not supported.
  * @since ostermillerutils 1.00.00
  */
 public static String getHashString(String s, String enc) throws UnsupportedEncodingException {
   MD5 md5 = new MD5();
   md5.update(s, enc);
   return md5.getHashString();
 }
Example #12
0
 /**
  * Gets the MD5 hash of the given String. The string is converted to bytes using the current
  * platform's default character encoding.
  *
  * @param s String for which an MD5 hash is desired.
  * @return 32-character hex representation the data's MD5 hash.
  */
 public static String getHashString(String s) {
   MD5 md5 = new MD5();
   md5.update(s);
   return md5.getHashString();
 }
Example #13
0
 /**
  * Gets the MD5 hash of the given String. The string is converted to bytes using the current
  * platform's default character encoding.
  *
  * @param s String for which an MD5 hash is desired.
  * @return Array of 16 bytes, the hash of all updated bytes.
  */
 public static byte[] getHash(String s) {
   MD5 md5 = new MD5();
   md5.update(s);
   return md5.getHash();
 }