java.security.DigestInputStream Java Examples

The following examples show how to use java.security.DigestInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From java-scanner-access-twain with GNU Affero General Public License v3.0 7 votes vote down vote up
static String getMd5(InputStream input) {
    if(input == null) {
        return null;
    }
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        DigestInputStream dis = new DigestInputStream(input, md);
        byte[] buffer = new byte[1024 * 8]; 
        while(dis.read(buffer) != -1) {
            ;
        }
        dis.close();
        byte[] raw = md.digest();

        BigInteger bigInt = new BigInteger(1, raw);
        StringBuilder hash = new StringBuilder(bigInt.toString(16));

        while(hash.length() < 32 ){
            hash.insert(0, '0');
        }
        return hash.toString();
    } catch (Throwable t) {
        return null;
    }
}
 
Example #2
Source File: Util.java    From java-n-IDE-for-Android with Apache License 2.0 7 votes vote down vote up
/**
 * @source http://stackoverflow.com/a/304350
 */
private static byte[] hash(String alg, InputStream in) throws Exception {
    MessageDigest md = MessageDigest.getInstance(alg);
    DigestInputStream dis = new DigestInputStream(new BufferedInputStream(in), md);
    try {
        byte[] buffer = new byte[1024];
        while (true) {
            int readCount = dis.read(buffer);
            if (readCount < 0) {
                break;
            }
        }
        return md.digest();
    } finally {
        in.close();
    }
}
 
Example #3
Source File: AbstractAwsSigner.java    From aws-sdk-java-v2 with Apache License 2.0 7 votes vote down vote up
byte[] hash(InputStream input) throws SdkClientException {
    try {
        MessageDigest md = getMessageDigestInstance();
        @SuppressWarnings("resource")
        DigestInputStream digestInputStream = new SdkDigestInputStream(
                input, md);
        byte[] buffer = new byte[1024];
        while (digestInputStream.read(buffer) > -1) {
            ;
        }
        return digestInputStream.getMessageDigest().digest();
    } catch (Exception e) {
        throw SdkClientException.builder()
                                .message("Unable to compute hash while signing request: " + e.getMessage())
                                .cause(e)
                                .build();
    }
}
 
Example #4
Source File: DigestInputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Test #1 for <code>read()</code> method<br>
 *
 * Assertion: returns the byte read<br>
 * Assertion: updates associated digest<br>
 */
public final void testRead01()
    throws IOException {
    for (int ii=0; ii<algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);
            for (int i=0; i<MY_MESSAGE_LEN; i++) {
                // check that read() returns valid values
                assertTrue("retval", ((byte)dis.read() == myMessage[i]));
            }
            // check that associated digest has been updated properly
            assertTrue("update",
                    Arrays.equals(
                            dis.getMessageDigest().digest(),
                            MDGoldenData.getDigest(algorithmName[ii])));
            return;
        } catch (NoSuchAlgorithmException e) {
            // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
 
Example #5
Source File: DigestInputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Test #2 for <code>read()</code> method<br>
 *
 * Assertion: returns -1 if EOS had been
 * reached but not read before method call<br>
 *
 * Assertion: must not update digest if EOS had been
 * reached but not read before method call<br>
 */
public final void testRead02()
    throws IOException {
    for (int ii=0; ii<algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);
            for (int i=0; i<MY_MESSAGE_LEN; i++) {
                dis.read();
            }
            // check that subsequent read() calls return -1 (eos)
            assertEquals("retval1", -1, dis.read());
            assertEquals("retval2", -1, dis.read());
            assertEquals("retval3", -1, dis.read());
            // check that 3 previous read() calls did not update digest
            assertTrue("update",
                    Arrays.equals(dis.getMessageDigest().digest(),
                            MDGoldenData.getDigest(algorithmName[ii])));
            return;
        } catch (NoSuchAlgorithmException e) {
            // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
 
Example #6
Source File: Rd5DiffManager.java    From brouter with MIT License 6 votes vote down vote up
public static String getMD5( File f ) throws Exception
{
  MessageDigest md = MessageDigest.getInstance("MD5");
  BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f ) );
  DigestInputStream dis = new DigestInputStream(bis, md);
  byte[] buf = new byte[8192];
  for(;;)
  {
    int len = dis.read( buf );
    if ( len <= 0 )
    {
      break;
    }
  }
  dis.close();
  byte[] bytes = md.digest();

  StringBuilder sb = new StringBuilder();
  for (int j = 0; j < bytes.length; j++)
  {
    int v = bytes[j] & 0xff;
    sb.append( hexChar( v >>> 4 ) ).append( hexChar( v & 0xf ) );
  }
  return sb.toString();
}
 
Example #7
Source File: FileUtils.java    From sofa-ark with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a SHA.1 Hash for a given file.
 * @param file the file to hash
 * @return the hash value as a String
 * @throws IOException if the file cannot be read
 */
public static String sha1Hash(File file) throws IOException {
    try {
        DigestInputStream inputStream = new DigestInputStream(new FileInputStream(file),
            MessageDigest.getInstance("SHA-1"));
        try {
            byte[] buffer = new byte[4098];
            while (inputStream.read(buffer) != -1) { //NOPMD
                // Read the entire stream
            }
            return bytesToHex(inputStream.getMessageDigest().digest());
        } finally {
            inputStream.close();
        }
    } catch (NoSuchAlgorithmException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example #8
Source File: Utils.java    From sheepit-client with GNU General Public License v2.0 6 votes vote down vote up
public static String md5(String path_of_file_) {
	try {
		MessageDigest md = MessageDigest.getInstance("MD5");
		InputStream is = Files.newInputStream(Paths.get(path_of_file_));
		DigestInputStream dis = new DigestInputStream(is, md);
		byte[] buffer = new byte[8192];
		while (dis.read(buffer) > 0)
			; // process the entire file
		String data = DatatypeConverter.printHexBinary(md.digest()).toLowerCase();
		dis.close();
		is.close();
		return data;
	}
	catch (NoSuchAlgorithmException | IOException e) {
		return "";
	}
}
 
Example #9
Source File: Utils.java    From java-ocr-api with GNU Affero General Public License v3.0 6 votes vote down vote up
static String getMd5(InputStream input) {
    if(input == null) {
        return null;
    }
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        DigestInputStream dis = new DigestInputStream(input, md);
        byte[] buffer = new byte[1024 * 8]; 
        while(dis.read(buffer) != -1) {
            ;
        }
        dis.close();
        byte[] raw = md.digest();

        BigInteger bigInt = new BigInteger(1, raw);
        StringBuilder hash = new StringBuilder(bigInt.toString(16));

        while(hash.length() < 32 ){
            hash.insert(0, '0');
        }
        return hash.toString();
    } catch (Throwable t) {
        return null;
    }
}
 
Example #10
Source File: DigestInputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Test #3 for <code>read()</code> method<br>
 * Test #1 for <code>on(boolean)</code> method<br>
 *
 * Assertion: <code>read()</code> must not update digest if it is off<br>
 * Assertion: <code>on(boolean)</code> turns digest functionality on
 * (if <code>true</code> passed as a parameter) or off (if <code>false</code>
 *  passed)
 */
public final void testRead03()
    throws IOException {
    for (int ii=0; ii<algorithmName.length; ii++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
            InputStream is = new ByteArrayInputStream(myMessage);
            DigestInputStream dis = new DigestInputStream(is, md);

            // turn digest off
            dis.on(false);

            for (int i=0; i<MY_MESSAGE_LEN; i++) {
                dis.read();
            }

            // check that digest value has not been updated by read()
            assertTrue(Arrays.equals(dis.getMessageDigest().digest(),
                    MDGoldenData.getDigest(algorithmName[ii]+"_NU")));
            return;
        } catch (NoSuchAlgorithmException e) {
            // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
 
Example #11
Source File: FileSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@Rpc(description = "Compute MD5 hash on a content URI. Return the MD5 has has a hex string.")
public String fileMd5Hash(String uri) throws IOException, NoSuchAlgorithmException {
    Uri uri_ = Uri.parse(uri);
    ParcelFileDescriptor pfd = mContext.getContentResolver().openFileDescriptor(uri_, "r");
    MessageDigest md = MessageDigest.getInstance("MD5");
    int length = (int) pfd.getStatSize();
    byte[] buf = new byte[length];
    ParcelFileDescriptor.AutoCloseInputStream stream =
            new ParcelFileDescriptor.AutoCloseInputStream(pfd);
    DigestInputStream dis = new DigestInputStream(stream, md);
    try {
        dis.read(buf, 0, length);
        return Utils.bytesToHexString(md.digest());
    } finally {
        dis.close();
        stream.close();
    }
}
 
Example #12
Source File: ProvingKeyFetcher.java    From hush-swing-wallet-ui with MIT License 6 votes vote down vote up
private static boolean checkSHA256(final File provingKey, final Component parent) throws IOException {
    final MessageDigest sha256;
    try {
        sha256 = MessageDigest.getInstance("SHA-256");
    } catch (final NoSuchAlgorithmException impossible) {
        throw new RuntimeException(impossible);
    }
    try (final InputStream is = new BufferedInputStream(new FileInputStream(provingKey))) {
        final ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, "Verifying proving key", is);
        pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
        pmis.getProgressMonitor().setMillisToPopup(10);
        final DigestInputStream dis = new DigestInputStream(pmis, sha256);
        final byte[] temp = new byte[0x1 << 13];
        while (dis.read(temp) >= 0) {
            /* do the thing */
        }
        final byte[] digest = sha256.digest();
        return SHA256.equalsIgnoreCase(DatatypeConverter.printHexBinary(digest));
    }
}
 
Example #13
Source File: DefaultWishlistsResourceTest.java    From yaas_java_jersey_wishlist with Apache License 2.0 6 votes vote down vote up
private static String computeMD5ChecksumForInputStream(
        final InputStream inputStream) throws NoSuchAlgorithmException,
        IOException {
    final MessageDigest md = MessageDigest.getInstance("MD5");

    try {
        final InputStream digestInputStream = new DigestInputStream(inputStream,
                md);
        while (digestInputStream.read() > 0) {
            // do nothing
        }
    } finally {
        inputStream.close();
    }
    final byte[] digest = md.digest();
    final StringBuffer sb = new StringBuffer();

    for (final byte element : digest) {
        sb.append(Integer.toString((element & 0xff) + 0x100, 16)
                .substring(1));
    }
    return sb.toString();
}
 
Example #14
Source File: RemoteHttpFile.java    From copybara with Apache License 2.0 6 votes vote down vote up
protected synchronized void download() throws RepoException, ValidationException {
  if (downloaded) {
    return;
  }
  URL remote = getRemote();
  try {
    console.progressFmt("Fetching %s", remote);
    ByteSink sink = getSink();
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    try (ProfilerTask task = profiler.start("remote_file_" + remote)) {
      try (DigestInputStream is = new DigestInputStream(transport.open(remote), digest)) {
        sink.writeFrom(is);
        sha256 = Optional.of(Bytes.asList(is.getMessageDigest().digest()).stream()
            .map(b -> String.format("%02X", b)).collect(Collectors.joining()).toLowerCase());
      }
      downloaded = true;
    }
  } catch (IOException | NoSuchAlgorithmException e) {
    throw new RepoException(String.format("Error downloading %s", remote), e);
  }
}
 
Example #15
Source File: MD5Hash.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @source http://stackoverflow.com/a/304350
 */
private static byte[] hash(String alg, InputStream in) throws Exception {
    MessageDigest md = MessageDigest.getInstance(alg);
    DigestInputStream dis = new DigestInputStream(new BufferedInputStream(in), md);
    try {
        byte[] buffer = new byte[1024];
        while (true) {
            int readCount = dis.read(buffer);
            if (readCount < 0) {
                break;
            }
        }
        return md.digest();
    } finally {
        in.close();
    }
}
 
Example #16
Source File: MD5Utils.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/**
 * 获取文件的 MD5
 */
public static String encode(File file) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        FileInputStream inputStream = new FileInputStream(file);
        DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);
        //必须把文件读取完毕才能拿到md5
        byte[] buffer = new byte[4096];
        while (digestInputStream.read(buffer) > -1) {
        }
        MessageDigest digest = digestInputStream.getMessageDigest();
        digestInputStream.close();
        byte[] md5 = digest.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : md5) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString().toLowerCase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #17
Source File: ProvingKeyFetcher.java    From zencash-swing-wallet-ui with MIT License 6 votes vote down vote up
private static boolean checkSHA256(File provingKey, Component parent) throws IOException {
    MessageDigest sha256;
    try {
        sha256 = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException impossible) {
        throw new IOException(impossible);
    }
    try (InputStream is = new BufferedInputStream(new FileInputStream(provingKey))) {
        ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent,
                LanguageUtil.instance().getString("proving.key.fetcher.option.pane.verify.progress.monitor.text"),
                is);
        pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
        pmis.getProgressMonitor().setMillisToPopup(10);
        DigestInputStream dis = new DigestInputStream(pmis, sha256);
        byte [] temp = new byte[0x1 << 13];
        while(dis.read(temp) >= 0);
        byte [] digest = sha256.digest();

        return SHA256.equalsIgnoreCase(Util.bytesToHex(digest));
    }
}
 
Example #18
Source File: ProvingKeyFetcher.java    From zencash-swing-wallet-ui with MIT License 6 votes vote down vote up
private static boolean checkSHA256SG(File sproutGroth, Component parent) throws IOException {
    MessageDigest sha256;
    try {
        sha256 = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException impossible) {
        throw new IOException(impossible);
    }
    try (InputStream is = new BufferedInputStream(new FileInputStream(sproutGroth))) {
        ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent,
                LanguageUtil.instance().getString("sprout.groth.fetcher.option.pane.verify.progress.monitor.text"),
                is);
        pmis.getProgressMonitor().setMaximum(SPROUT_GROTH_SIZE);
        pmis.getProgressMonitor().setMillisToPopup(10);
        DigestInputStream dis = new DigestInputStream(pmis, sha256);
        byte [] temp = new byte[0x1 << 13];
        while(dis.read(temp) >= 0);
        byte [] digest = sha256.digest();
        
        return SHA256SG.equalsIgnoreCase(Util.bytesToHex(digest));
    }
}
 
Example #19
Source File: Util.java    From zencash-swing-wallet-ui with MIT License 6 votes vote down vote up
public static byte[] calculateSHA256Digest(byte[] input)
    throws IOException 
{
    try 
    {
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        DigestInputStream dis = new DigestInputStream(new ByteArrayInputStream(input), sha256);
        byte [] temp = new byte[0x1 << 13];
        byte[] digest;
        while(dis.read(temp) >= 0);
        {
            digest = sha256.digest();
        }
        
        return digest;
    } catch (NoSuchAlgorithmException impossible) 
    {
        throw new IOException(impossible);
    }
}
 
Example #20
Source File: AbstractAWSSigner.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
protected byte[] hash(InputStream input) throws SdkClientException {
    try {
        MessageDigest md = getMessageDigestInstance();
        @SuppressWarnings("resource")
        DigestInputStream digestInputStream = new SdkDigestInputStream(
                input, md);
        byte[] buffer = new byte[1024];
        while (digestInputStream.read(buffer) > -1)
            ;
        return digestInputStream.getMessageDigest().digest();
    } catch (Exception e) {
        throw new SdkClientException(
                "Unable to compute hash while signing request: "
                        + e.getMessage(), e);
    }
}
 
Example #21
Source File: ShimLoader.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
static String md5sum(InputStream input) throws IOException {
  BufferedInputStream in = new BufferedInputStream(input);

  try {
    MessageDigest digest = MessageDigest.getInstance("MD5");
    DigestInputStream digestInputStream = new DigestInputStream(in, digest);
    boolean bytesRead = false;
    byte[] buffer = new byte[8192];

    while(digestInputStream.read(buffer) != -1) {
      // CHECKSTYLE:OFF EmptyStatement
      ;
      // CHECKSTYLE:ON
    }

    ByteArrayOutputStream md5out = new ByteArrayOutputStream();
    md5out.write(digest.digest());
    String md5String = md5out.toString();
    return md5String;
  } catch (NoSuchAlgorithmException noSuchAlgorithmException) {
    throw new IllegalStateException("MD5 algorithm is not available: " + noSuchAlgorithmException);
  } finally {
    in.close();
  }
}
 
Example #22
Source File: HttpProxyTask.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param inputStream
 * @return
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public static Md5Result generateChecksum(InputStream inputStream) throws NoSuchAlgorithmException, IOException {
	Md5Result md5 = new Md5Result();
	MessageDigest md = MessageDigest.getInstance("MD5");
	DigestInputStream dis = new DigestInputStream(inputStream, md);
	
	byte[] dataBytes = new byte[4096];
      
       int nread = 0; 
       while ((nread = dis.read(dataBytes)) != -1) {
       	md5.contentLength += nread;
       };
       
       dis.close();
       
       long startNs = System.nanoTime();
       md5.md5 = generateChecksumFromDigest(md.digest());
       md5.generatingTimeNs = System.nanoTime() - startNs;
       
       return md5;
}
 
Example #23
Source File: Streams.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
public static String getMd5Checksum(InputStream in) {
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw Exceptions.propagate(e);
    }
    DigestInputStream dis = new DigestInputStream(in, md);
    readFullyAndClose(dis);
    byte[] digest = md.digest();
    StringBuilder result = new StringBuilder();
    for (byte b: digest) {
        result.append(Strings.padStart(Integer.toHexString((256+b)%256), 2, '0'));
    }
    return result.toString().toUpperCase();
}
 
Example #24
Source File: DigestInputStream2Test.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.security.DigestInputStream#read(byte[], int, int)
 */
public void test_read$BII() throws IOException {
    // Test for method int java.security.DigestInputStream.read(byte [],
    // int, int)
    DigestInputStream dis = new DigestInputStream(inStream, digest);
    int bytesToRead = inStream.available();
    byte buf1[] = new byte[bytesToRead + 5];
    byte buf2[] = new byte[bytesToRead + 5];
    // make sure we're actually reading some data
    assertTrue("No data to read for this test", bytesToRead>0);

    // read and compare the data that the inStream has
    int bytesRead1 = dis.read(buf1, 5, bytesToRead);
    int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
    assertEquals("Didn't read the same from each stream", bytesRead1,
            bytesRead2);
    assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
    // compare the arrays
    boolean same = true;
    for (int i = 0; i < bytesToRead + 5; i++) {
        if (buf1[i] != buf2[i]) {
            same = false;
        }
    }// end for
    assertTrue("Didn't get the same data", same);
}
 
Example #25
Source File: ArgbRendererTest.java    From render with GNU General Public License v2.0 6 votes vote down vote up
public static String getDigestString(final File file) throws Exception {
    final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    final FileInputStream fileInputStream = new FileInputStream(file);
    final DigestInputStream digestInputStream = new DigestInputStream(fileInputStream, messageDigest);

    final byte[] buffer = new byte[8192];
    for (int bytesRead = 1; bytesRead > 0;) {
        bytesRead = digestInputStream.read(buffer);
    }

    final byte[] digestValue = messageDigest.digest();
    final StringBuilder sb = new StringBuilder(digestValue.length);
    for (final byte b : digestValue) {
        sb.append(b);
    }

    return sb.toString();
}
 
Example #26
Source File: LargeFileUpload.java    From box-java-sdk with Apache License 2.0 6 votes vote down vote up
private BoxFile.Info uploadHelper(BoxFileUploadSession.Info session, InputStream stream, long fileSize,
        Map<String, String> fileAttributes)
    throws InterruptedException, IOException {
    //Upload parts using the upload session
    MessageDigest digest = null;
    try {
        digest = MessageDigest.getInstance(DIGEST_ALGORITHM_SHA1);
    } catch (NoSuchAlgorithmException ae) {
        throw new BoxAPIException("Digest algorithm not found", ae);
    }
    DigestInputStream dis = new DigestInputStream(stream, digest);
    List<BoxFileUploadSessionPart> parts = this.uploadParts(session, dis, fileSize);

    //Creates the file hash
    byte[] digestBytes = digest.digest();
    String digestStr = Base64.encode(digestBytes);

    //Commit the upload session. If there is a failure, abort the commit.
    try {
        return session.getResource().commit(digestStr, parts, fileAttributes, null, null);
    } catch (Exception e) {
        session.getResource().abort();
        throw new BoxAPIException("Unable to commit the upload session", e);
    }
}
 
Example #27
Source File: TestJets3tNativeFileSystemStore.java    From hadoop with Apache License 2.0 5 votes vote down vote up
protected void writeRenameReadCompare(Path path, long len)
    throws IOException, NoSuchAlgorithmException {
  // If len > fs.s3n.multipart.uploads.block.size,
  // we'll use a multipart upload copy
  MessageDigest digest = MessageDigest.getInstance("MD5");
  OutputStream out = new BufferedOutputStream(
      new DigestOutputStream(fs.create(path, false), digest));
  for (long i = 0; i < len; i++) {
    out.write('Q');
  }
  out.flush();
  out.close();

  assertTrue("Exists", fs.exists(path));

  // Depending on if this file is over 5 GB or not,
  // rename will cause a multipart upload copy
  Path copyPath = path.suffix(".copy");
  fs.rename(path, copyPath);

  assertTrue("Copy exists", fs.exists(copyPath));

  // Download file from S3 and compare the digest against the original
  MessageDigest digest2 = MessageDigest.getInstance("MD5");
  InputStream in = new BufferedInputStream(
      new DigestInputStream(fs.open(copyPath), digest2));
  long copyLen = 0;
  while (in.read() != -1) {copyLen++;}
  in.close();

  assertEquals("Copy length matches original", len, copyLen);
  assertArrayEquals("Digests match", digest.digest(), digest2.digest());
}
 
Example #28
Source File: AuditLogUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String calculateHash(StringBuffer str) throws Exception {
	MessageDigest algorithm = MessageDigest.getInstance("MD5");
	String is = new String(str);
	DigestInputStream dis = new DigestInputStream(new ByteArrayInputStream(is.getBytes()), algorithm);
	while (dis.read() != -1)
		;
	byte[] hash = algorithm.digest();
	return byteArray2Hex(hash);
}
 
Example #29
Source File: MD5FileUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Read dataFile and compute its MD5 checksum.
 */
public static MD5Hash computeMd5ForFile(File dataFile) throws IOException {
  InputStream in = new FileInputStream(dataFile);
  try {
    MessageDigest digester = MD5Hash.getDigester();
    DigestInputStream dis = new DigestInputStream(in, digester);
    IOUtils.copyBytes(dis, new IOUtils.NullOutputStream(), 128*1024);
    
    return new MD5Hash(digester.digest());
  } finally {
    IOUtils.closeStream(in);
  }
}
 
Example #30
Source File: DigestInputStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test #1 for <code>DigestInputStream</code> constructor<br>
 *
 * Assertion: creates new <code>DigestInputStream</code> instance
 * using valid parameters (both non <code>null</code>)
 *
 * @throws NoSuchAlgorithmException
 */
public final void testDigestInputStream01()  {
    for (int i=0; i<algorithmName.length; i++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[i]);
            InputStream is = new ByteArrayInputStream(myMessage);
            InputStream dis = new DigestInputStream(is, md);
            assertTrue(dis instanceof DigestInputStream);
            return;
        } catch (NoSuchAlgorithmException e) {
            // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}