Java Code Examples for java.security.DigestInputStream

The following examples show how to use java.security.DigestInputStream. These examples are extracted from open source projects. 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
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 Project: java-n-IDE-for-Android   Source File: Util.java    License: 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 Project: copybara   Source File: RemoteHttpFile.java    License: 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 4
Source Project: j2objc   Source File: DigestInputStreamTest.java    License: 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 5
Source Project: java-ocr-api   Source File: Utils.java    License: 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 6
Source Project: sheepit-client   Source File: Utils.java    License: 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 7
Source Project: brouter   Source File: Rd5DiffManager.java    License: 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 8
Source Project: j2objc   Source File: DigestInputStreamTest.java    License: 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 9
Source Project: sofa-ark   Source File: FileUtils.java    License: 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 10
Source Project: mobly-bundled-snippets   Source File: FileSnippet.java    License: 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 11
Source Project: hush-swing-wallet-ui   Source File: ProvingKeyFetcher.java    License: 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 12
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 13
Source Project: j2objc   Source File: DigestInputStreamTest.java    License: 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 14
Source Project: javaide   Source File: MD5Hash.java    License: 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 15
Source Project: okhttp-OkGo   Source File: MD5Utils.java    License: 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 16
Source Project: zencash-swing-wallet-ui   Source File: ProvingKeyFetcher.java    License: 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 17
Source Project: zencash-swing-wallet-ui   Source File: ProvingKeyFetcher.java    License: 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 18
Source Project: zencash-swing-wallet-ui   Source File: Util.java    License: 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 19
Source Project: aws-sdk-java-v2   Source File: AbstractAwsSigner.java    License: Apache License 2.0 6 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 20
Source Project: ibm-cos-sdk-java   Source File: AbstractAWSSigner.java    License: 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 Project: dremio-oss   Source File: ShimLoader.java    License: 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 Project: open-rmbt   Source File: HttpProxyTask.java    License: 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 Project: brooklyn-server   Source File: Streams.java    License: 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 Project: j2objc   Source File: DigestInputStream2Test.java    License: 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 Project: render   Source File: ArgbRendererTest.java    License: 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 Project: box-java-sdk   Source File: LargeFileUpload.java    License: 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 Project: github-bucket   Source File: RepositoryS3.java    License: ISC License 5 votes vote down vote up
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
    byte[] content;
    byte[] newHash;
    LOG.debug("Start processing file: {}", path);
    try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
        // Get content
        content = IOUtils.toByteArray(is);
        // Get hash
        newHash = is.getMessageDigest().digest();
    }
    if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
        LOG.info("Uploading file: {}", path);
        ObjectMetadata bucketMetadata = new ObjectMetadata();
        bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
        bucketMetadata.setContentLength(content.length);
        // Give Tika a few hints for the content detection
        Metadata tikaMetadata = new Metadata();
        tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
        // Fire!
        try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
            bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
            s3.putObject(bucket.getName(), path, bis, bucketMetadata);
            return true;
        }
    }
    LOG.info("Skipping file (same checksum): {}", path);
    return false;
}
 
Example 28
public static Object streamDecrypt(byte[] data, SecretKey key,
    MessageDigest digest) throws Exception {

    Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    decCipher.init(Cipher.DECRYPT_MODE, key);
    digest.reset();
    try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
        DigestInputStream dis = new DigestInputStream(bis, digest);
        CipherInputStream cis = new CipherInputStream(dis, decCipher)) {

        try (ObjectInputStream ois = new ObjectInputStream(cis)) {
            return ois.readObject();
        }
    }
}
 
Example 29
Source Project: serve   Source File: ModelArchive.java    License: Apache License 2.0 5 votes vote down vote up
public static File unzip(InputStream is, String eTag) throws IOException {
    File tmpDir = FileUtils.getTempDirectory();
    File modelDir = new File(tmpDir, "models");
    FileUtils.forceMkdir(modelDir);

    File tmp = File.createTempFile("model", ".download");
    FileUtils.forceDelete(tmp);
    FileUtils.forceMkdir(tmp);

    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA1");
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
    ZipUtils.unzip(new DigestInputStream(is, md), tmp);
    if (eTag == null) {
        eTag = HexUtils.toHexString(md.digest());
    }
    File dir = new File(modelDir, eTag);
    if (dir.exists()) {
        FileUtils.deleteDirectory(tmp);
        logger.info("model folder already exists: {}", eTag);
        return dir;
    }

    FileUtils.moveDirectory(tmp, dir);

    return dir;
}
 
Example 30
Source Project: TencentKona-8   Source File: Main.java    License: GNU General Public License v2.0 5 votes vote down vote up
/** Display the location and checksum of a class. */
void showClass(String className) {
    PrintWriter pw = log.getWriter(WriterKind.NOTICE);
    pw.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        pw.println("  class not found");
    else {
        pw.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do { n = in.read(buf); } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b: digest)
                sb.append(String.format("%02x", b));
            pw.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            pw.println("  cannot compute digest: " + e);
        }
    }
}