/** Construct a hash value for a byte array. */ public static MD5Hash digest(byte[] data) { return digest(data, 0, data.length); }
/** Construct a hash value for an array of byte array. */ public static MD5Hash digest(byte[][] dataArr, int start, int len) { byte[] digest; MessageDigest digester = getDigester(); for (byte[] data : dataArr) { digester.update(data, start, len); } digest = digester.digest(); return new MD5Hash(digest); }
@Override public void write(DataOutput out) throws IOException { md5.write(out); }
/** Constructs, reads and returns an instance. */ public static MD5Hash read(DataInput in) throws IOException { MD5Hash result = new MD5Hash(); result.readFields(in); return result; }
= new MD5Hash(D00); = new MD5Hash(DFF); MD5Hash orderedHash = new MD5Hash(new byte[]{1,2,3,4,5,6,7,8,9,10,11,12, 13,14,15,16}); MD5Hash backwardHash = new MD5Hash(new byte[]{-1,-2,-3,-4,-5,-6,-7,-8, -9,-10,-11,-12, -13, -14, -15,-16}); MD5Hash closeHash1 = new MD5Hash(new byte[]{-1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0}); MD5Hash closeHash2 = new MD5Hash(new byte[]{-1,1,0,0,0,0,0,0, 0,0,0,0,0,0,0,0}); assertTrue(md5Hash.compareTo(md5Hash) == 0); assertTrue(md5Hash00.compareTo(md5Hash) < 0); assertTrue(md5HashFF.compareTo(md5Hash) > 0); assertEquals(md5Hash, new MD5Hash(md5Hash.toString())); assertEquals(md5Hash00, new MD5Hash(md5Hash00.toString())); assertEquals(md5HashFF, new MD5Hash(md5HashFF.toString())); assertEquals(0x01020304, orderedHash.quarterDigest()); assertEquals(0xfffefdfc, backwardHash.quarterDigest()); assertEquals(0x0102030405060708L, orderedHash.halfDigest()); assertEquals(0xfffefdfcfbfaf9f8L, backwardHash.halfDigest()); assertTrue("hash collision", closeHash1.hashCode() != closeHash2.hashCode());
protected static String getTraceSignature(String input) throws IOException { Path inputPath = new Path(input); FileSystem fs = inputPath.getFileSystem(new Configuration()); FileStatus status = fs.getFileStatus(inputPath); Path qPath = fs.makeQualified(status.getPath()); String traceID = status.getModificationTime() + qPath.toString() + status.getOwner() + status.getLen(); return MD5Hash.digest(traceID).toString(); }
private FileChecksum getPFileChecksum(Path f) throws IOException { MessageDigest md5Digest; try { md5Digest = MessageDigest.getInstance("MD5"); MD5Hash md5Hash = new MD5Hash(getMD5Checksum(fs.open(f))); return new PFileChecksum(md5Hash, md5Digest.getAlgorithm()); } catch (Exception e) { throw new IOException(e); } }
MessageDigest digester = null; if (getChecksum) { digester = MD5Hash.getDigester(); stream = new DigestInputStream(stream, digester); MD5Hash computedDigest = new MD5Hash(digester.digest()); !computedDigest.equals(advertisedDigest)) { deleteTmpFiles(localPaths); throw new IOException("File " + url + " computed digest " +
/** * Set headers for content length, and, if available, md5. * @throws IOException */ public static void setVerificationHeadersForGet(HttpServletResponse response, File file) throws IOException { response.setHeader( Util.CONTENT_LENGTH, String.valueOf(file.length())); MD5Hash hash = MD5FileUtils.readStoredMd5ForFile(file); if (hash != null) { response.setHeader(Util.MD5_HEADER, hash.toString()); } }
@Override public byte[] getBytes() { if (md5 != null) { return md5.getDigest(); } return new byte[0]; } }
/** Returns a hash code value for this object. * Only uses the first 4 bytes, since md5s are evenly distributed. */ @Override public int hashCode() { return quarterDigest(); }
/** Sets the digest value from a hex string. */ public void setDigest(String hex) { if (hex.length() != MD5_LEN*2) throw new IllegalArgumentException("Wrong length: " + hex.length()); byte[] digest = new byte[MD5_LEN]; for (int i = 0; i < MD5_LEN; i++) { int j = i << 1; digest[i] = (byte)(charToNibble(hex.charAt(j)) << 4 | charToNibble(hex.charAt(j+1))); } this.digest = digest; }
@Override public void readFields(DataInput in) throws IOException { bytesPerCRC = in.readInt(); crcPerBlock = in.readLong(); md5 = MD5Hash.read(in); }
@Override public void readFields(DataInput in) throws IOException { md5.readFields(in); }
@Override void prepareDigester() throws IOException { digester = MD5Hash.getDigester(); }
/** Constructs, reads and returns an instance. */ public static MD5Hash read(DataInput in) throws IOException { MD5Hash result = new MD5Hash(); result.readFields(in); return result; }