Java Code Examples for java.security.DigestInputStream

The following are top voted examples for showing how to use java.security.DigestInputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: GitHub   File: MD5Utils.java   View source code 7 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 2
Project: elasticsearch_my   File: MockAmazonS3.java   View source code 6 votes vote down vote up
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest)
        throws AmazonClientException, AmazonServiceException {
    String blobName = putObjectRequest.getKey();
    DigestInputStream stream = (DigestInputStream) putObjectRequest.getInputStream();

    if (blobs.containsKey(blobName)) {
        throw new AmazonS3Exception("[" + blobName + "] already exists.");
    }

    blobs.put(blobName, stream);

    // input and output md5 hashes need to match to avoid an exception
    String md5 = Base64.encodeAsString(stream.getMessageDigest().digest());
    PutObjectResult result = new PutObjectResult();
    result.setContentMd5(md5);

    return result;
}
 
Example 3
Project: Java-EX   File: SecurityUtil.java   View source code 6 votes vote down vote up
public static String digest(InputStream input, String algorithm) throws IOException, NoSuchAlgorithmException {
  Stopwatch sw = Stopwatch.createStarted();
  int bufferSize = 256 * 1024;
  MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
  try (DigestInputStream digestInputStream = new DigestInputStream(input, messageDigest);) {
    byte[] buffer = new byte[bufferSize];
    while (digestInputStream.read(buffer) > 0) {
      ;
    }
    messageDigest = digestInputStream.getMessageDigest();
    byte[] resultByteArray = messageDigest.digest();
    return byteArrayToHex(resultByteArray);
  } finally {
    sw.stop();
  }
}
 
Example 4
Project: mobly-bundled-snippets   File: FileSnippet.java   View source code 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 5
Project: open-rmbt   File: HttpProxyTask.java   View source code 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 6
Project: AptSpring   File: GsonDefinitionModelStore.java   View source code 6 votes vote down vote up
/**
 * Find definitions by name.
 * 
 * @param name the name of the definition to find.
 * @return A list of all definitions that happen to have the name (from multiple jars?)
 */
@Override
public List<DefinitionModel> lookup(String name) {
  List<DefinitionModel> output = new ArrayList<>();
  for (Resource resource : resourceLocator.getEntries(name)) {    
    try (DigestInputStream digestInputStream = new DigestInputStream(resource.getInputStream(), getSha256Digest()); 
        Reader reader = new InputStreamReader(digestInputStream, StandardCharsets.UTF_8)) {
      DefinitionModel definitionModel = gson.fromJson(reader, DefinitionModel.class);
      definitionModel.setSourceLocation(resource.getLocation());
      definitionModel.setSha256(bytesToHex(digestInputStream.getMessageDigest().digest()));
      output.add(definitionModel);
    } catch (IOException ex) {
      return null;
    }
  }
  return output;
}
 
Example 7
Project: javaide   File: Util.java   View source code 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 8
Project: zencash-swing-wallet-ui   File: ProvingKeyFetcher.java   View source code 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,"Verifying proving key",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(DatatypeConverter.printHexBinary(digest));
    }
}
 
Example 9
Project: zencash-swing-wallet-ui   File: Util.java   View source code 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 10
Project: komodoGUI   File: ProvingKeyFetcher.java   View source code 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 RuntimeException(impossible);
    }
    try (InputStream is = new BufferedInputStream(new FileInputStream(provingKey))) {
        ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent,"Verifying proving key",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(DatatypeConverter.printHexBinary(digest));
    }
}
 
Example 11
Project: artifactory-resource   File: Checksums.java   View source code 6 votes vote down vote up
public static Checksums calculate(InputStream content) throws IOException {
	Assert.notNull(content, "Content must not be null");
	try {
		DigestInputStream sha1 = new DigestInputStream(content,
				MessageDigest.getInstance("SHA-1"));
		DigestInputStream md5 = new DigestInputStream(sha1,
				MessageDigest.getInstance("MD5"));
		StreamUtils.drain(md5);
		return new Checksums(getDigestHex(sha1), getDigestHex(md5));
	}
	catch (Exception ex) {
		throw new RuntimeException(ex);
	}
	finally {
		content.close();
	}
}
 
Example 12
Project: hadoop   File: TestJets3tNativeFileSystemStore.java   View source code 6 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 13
Project: aws-sdk-java-v2   File: S3TestUtils.java   View source code 6 votes vote down vote up
public static void assertMd5MatchesEtag(InputStream content, GetObjectResponse response) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    try (InputStream is = new DigestInputStream(content, md)) {
        IoUtils.drainInputStream(is);
    }
    byte[] expectedMd5 = BinaryUtils.fromHex(response.eTag().replace("\"", ""));
    byte[] calculatedMd5 = md.digest();
    if (!Arrays.equals(expectedMd5, calculatedMd5)) {
        throw new AssertionError(
                String.format("Content malformed. Expected checksum was %s but calculated checksum was %s",
                              BinaryUtils.toBase64(expectedMd5), BinaryUtils.toBase64(calculatedMd5)));
    }
}
 
Example 14
Project: aws-sdk-java-v2   File: AbstractAwsSigner.java   View source code 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 15
Project: ibm-cos-sdk-java   File: AbstractAWSSigner.java   View source code 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 16
Project: S3Mock   File: FileStore.java   View source code 6 votes vote down vote up
/**
 * Uploads a part of a multipart upload.
 *
 * @param bucketName in which to upload
 * @param fileName of the file to upload
 * @param uploadId id of the upload
 * @param partNumber number of the part to store
 * @param inputStream file data to be stored
 * @param useV4Signing If {@code true}, V4-style signing is enabled.
 * @return the md5 hash of this part
 * @throws IOException if file could not be read to calculate digest
 */
public String putPart(final String bucketName,
    final String fileName,
    final String uploadId,
    final String partNumber,
    final InputStream inputStream,
    final boolean useV4Signing) throws IOException {
  try (DigestInputStream digestingInputStream =
      new DigestInputStream(wrapStream(inputStream, useV4Signing),
          MessageDigest.getInstance("MD5"))) {
    inputStreamToFile(digestingInputStream,
        Paths.get(rootFolder.getAbsolutePath(), bucketName, fileName, uploadId,
            partNumber + PART_SUFFIX));

    return new String(Hex.encodeHex(digestingInputStream.getMessageDigest().digest()));
  } catch (final NoSuchAlgorithmException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 17
Project: dremio-oss   File: ShimLoader.java   View source code 6 votes vote down vote up
static String md5sum(InputStream input) throws IOException {
  BufferedInputStream in = new BufferedInputStream(input);

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

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

    ByteArrayOutputStream md5out = new ByteArrayOutputStream();
    md5out.write(e.digest());
    String var7 = md5out.toString();
    return var7;
  } catch (NoSuchAlgorithmException var11) {
    throw new IllegalStateException("MD5 algorithm is not available: " + var11);
  } finally {
    in.close();
  }
}
 
Example 18
Project: MineskinSponge   File: MineskinServiceImpl.java   View source code 6 votes vote down vote up
private static CompletableFuture<String> computeMd5(Path path) {
    return CompletableFuture.supplyAsync(() -> {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");  // Possibly use a different hashing algorithm

            try (InputStream is = Files.newInputStream(path);
                 DigestInputStream dis = new DigestInputStream(is, md)) {
                int readByte;

                do {
                    readByte = dis.read();
                } while(readByte != -1);
            }

            byte[] bytes = md.digest();

            return HexBin.encode(bytes);
        } catch (IOException | NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }, MineskinSponge.getInstance().getAsyncExecutor());
}
 
Example 19
Project: fileserver   File: FileScan.java   View source code 6 votes vote down vote up
public String getMd5(File f) throws IOException {

		FileInputStream in = new FileInputStream(f);
		DigestInputStream di = new DigestInputStream(in, md);

		byte[] buffer = new byte[bufferSize];
		while (di.read(buffer) > 0)
			;
		md = di.getMessageDigest();

		di.close();
		in.close();

		byte[] digest = md.digest();
		return byteArrayToHex(digest);
	}
 
Example 20
Project: quadracoatl   File: Md5Hasher.java   View source code 6 votes vote down vote up
/**
 * Hashes the given data from the {@link InputStream} and returns the hash
 * as hex string. If the input is {@code null} the {@link #NULL_HASH} is
 * returned.
 * 
 * @param stream The stream with the data to be hashed.
 * @return The hash as hex string or if the input is {@code null}
 *         {@link #NULL_HASH}.
 * @throws IOException If reading from the given {@link InputStream} failed.
 */
public static final String hash(InputStream stream) throws IOException {
	if (stream == null) {
		return NULL_HASH;
	}
	
	MessageDigest digest = getMd5Digest();
	
	try (DigestInputStream digestInputStream = new DigestInputStream(stream, digest)) {
		while (digestInputStream.read() != -1) {
			// Nothing to do here, the read is all we need.
		}
	}
	
	return createHexString(digest.digest());
}
 
Example 21
Project: java-installer   File: MainApp.java   View source code 6 votes vote down vote up
public static String hashFile(File file) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    try (InputStream is = Files.newInputStream(Paths.get(file.getAbsolutePath()))) {
        DigestInputStream ds = new DigestInputStream(is, md);
        byte[] input = new byte[1024];

        // Read the stream to the end to get the md5
        while (ds.read(input) != -1) {
        }

        byte[] hash = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : hash) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
}
 
Example 22
Project: mycore   File: MCRContentInputStream.java   View source code 6 votes vote down vote up
/**
 * Constructs a new MCRContentInputStream
 * 
 * @param in
 *            the InputStream to read from
 * @throws MCRConfigurationException
 *             if java classes supporting MD5 checksums are not found
 */
public MCRContentInputStream(InputStream in) throws MCRException {
    super(null);

    digest = buildMD5Digest();

    DigestInputStream dis = new DigestInputStream(in, digest);
    MCRBlockingInputStream bis = new MCRBlockingInputStream(dis, headerSize);

    byte[] buffer = new byte[headerSize];

    try {
        int num = bis.read(buffer, 0, buffer.length);
        header = new byte[Math.max(0, num)];

        if (num > 0) {
            System.arraycopy(buffer, 0, header, 0, num);
        }
    } catch (IOException ex) {
        String msg = "Error while reading content input stream header";
        throw new MCRException(msg, ex);
    }

    this.in = bis;
}
 
Example 23
Project: CloudMusicLH   File: MD5Util.java   View source code 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 24
Project: aws-encryption-sdk-java   File: TestIOUtils.java   View source code 6 votes vote down vote up
public static byte[] computeFileDigest(final String fileName) throws IOException {
    try {
        final FileInputStream fis = new FileInputStream(fileName);
        final MessageDigest md = MessageDigest.getInstance("SHA-256");
        final DigestInputStream dis = new DigestInputStream(fis, md);

        final int readLen = 128;
        final byte[] readBytes = new byte[readLen];
        while (dis.read(readBytes) != -1) {
        }
        dis.close();

        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        // shouldn't get here since we hardcode the algorithm.
    }

    return null;
}
 
Example 25
Project: Java-EX   File: SecurityUtil.java   View source code 6 votes vote down vote up
public static String digest(InputStream input, String algorithm) throws IOException, NoSuchAlgorithmException {
  Stopwatch sw = Stopwatch.createStarted();
  int bufferSize = 256 * 1024;
  MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
  try (DigestInputStream digestInputStream = new DigestInputStream(input, messageDigest);) {
    byte[] buffer = new byte[bufferSize];
    while (digestInputStream.read(buffer) > 0) {
      ;
    }
    messageDigest = digestInputStream.getMessageDigest();
    byte[] resultByteArray = messageDigest.digest();
    return byteArrayToHex(resultByteArray);
  } finally {
    sw.stop();
  }
}
 
Example 26
Project: okhttp-OkGo   File: MD5Utils.java   View source code 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 27
Project: https-github.com-g0t4-jenkins2-course-spring-boot   File: FileUtils.java   View source code 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) {
				// Read the entire stream
			}
			return bytesToHex(inputStream.getMessageDigest().digest());
		}
		finally {
			inputStream.close();
		}
	}
	catch (NoSuchAlgorithmException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example 28
Project: azkaban   File: Md5Hasher.java   View source code 6 votes vote down vote up
public static byte[] md5Hash(File file) throws IOException {
  MessageDigest digest = getMd5Digest();

  FileInputStream fStream = new FileInputStream(file);
  BufferedInputStream bStream = new BufferedInputStream(fStream);
  DigestInputStream blobStream = new DigestInputStream(bStream, digest);

  byte[] buffer = new byte[BYTE_BUFFER_SIZE];

  int num = 0;
  do {
    num = blobStream.read(buffer);
  } while (num > 0);

  bStream.close();

  return digest.digest();
}
 
Example 29
Project: SolrFits   File: DupeChecker.java   View source code 6 votes vote down vote up
boolean inSolr() throws IOException, SolrServerException {
    // Check Solr to see if the file has already been index by getting the md5 checksum
    // of the file and doing a Solr query using that checksum. Don't run FITS if the
    // file already exists.
    try (InputStream inputStream = Files.newInputStream(this.getFile())) {
        byte[] buffer = new byte[8192];
        DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);

        try {
            while (digestInputStream.read(buffer) != -1) ;
        } finally {
            digestInputStream.close();
        }

        SolrQuery md5Query = new SolrQuery();
        md5Query.setQuery("md5checksum:" + DatatypeConverter.printHexBinary(messageDigest.digest()).toLowerCase());
        QueryResponse queryResponse = solrClient.query(md5Query);

        if (queryResponse.getResults().getNumFound() == 0) {
         this.status = false;
        } else{
            this.status = true;
        }
    }
    return this.status;
}
 
Example 30
Project: JavaAyo   File: MD5Utils.java   View source code 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 31
Project: LibreOCR   File: FileUtil.java   View source code 6 votes vote down vote up
/**
 * Calculates digest for a file using provided algorithm.
 */
public static byte[] digest(final File file, MessageDigest algorithm) throws IOException {
	algorithm.reset();
	FileInputStream fis = new FileInputStream(file);
	BufferedInputStream bis = new BufferedInputStream(fis);
	DigestInputStream dis = new DigestInputStream(bis, algorithm);

	try {
		while (dis.read() != -1) {
		}
	}
	finally {
		StreamUtil.close(fis);
	}

	return algorithm.digest();
}
 
Example 32
Project: SafetyNetHelper   File: SafetyNetHelper.java   View source code 6 votes vote down vote up
@Nullable private String getApkDigestSha256() {
  try {
    FileInputStream fis = new FileInputStream(context.getPackageCodePath());
    MessageDigest md = MessageDigest.getInstance(SHA_256);
    try {
      DigestInputStream dis = new DigestInputStream(fis, md);
      byte[] buffer = new byte[2048];
      while (dis.read(buffer) != -1) {
        //
      }
      dis.close();
    } finally {
      fis.close();
    }
    return Base64.encodeToString(md.digest(), Base64.NO_WRAP);
  } catch (IOException | NoSuchAlgorithmException e) {
    return null;
  }
}
 
Example 33
Project: aliyun-tablestore-java-sdk   File: StreamUtils.java   View source code 6 votes vote down vote up
public static String calculateMD5(InputStream in) throws Exception {

        DigestInputStream ds = new DigestInputStream(in, MessageDigest.getInstance("MD5"));
        while (ds.read() != -1) {
        }

        byte[] md5bytes = ds.getMessageDigest().digest();

        StringBuilder sb = new StringBuilder();
        for (byte md5byte : md5bytes) {
            String hexBiChars = String.format("%02x", md5byte);
            sb.append(hexBiChars);
        }

        return sb.toString();
    }
 
Example 34
Project: mesh   File: FileUtils.java   View source code 6 votes vote down vote up
/**
 * Generate a SHA 512 checksum from the given file and asynchronously return the hex encoded hash as a string.
 * 
 * @param path
 */
public static String hash(String path) {
	try {
		MessageDigest md = MessageDigest.getInstance("SHA-512");
		try (InputStream is = Files.newInputStream(Paths.get(path)); DigestInputStream mis = new DigestInputStream(is, md)) {
			byte[] buffer = new byte[4096];
			while (mis.read(buffer) >= 0) {
			}
		}
		byte[] digest = md.digest();
		return bytesToHex(digest);
	} catch (Exception e) {
		log.error("Error while hashing file {" + path + "}", e);
		throw error(INTERNAL_SERVER_ERROR, "node_error_upload_failed", e);
	}
}
 
Example 35
Project: mesh   File: FileUtils.java   View source code 6 votes vote down vote up
/**
 * Generate a SHA 512 checksum from the given file and asynchronously return the hex encoded hash as a string.
 * 
 * @param path
 */
public static String hash(String path) {
	try {
		MessageDigest md = MessageDigest.getInstance("SHA-512");
		try (InputStream is = Files.newInputStream(Paths.get(path)); DigestInputStream mis = new DigestInputStream(is, md)) {
			byte[] buffer = new byte[4096];
			while (mis.read(buffer) >= 0) {
			}
		}
		byte[] digest = md.digest();
		return bytesToHex(digest);
	} catch (Exception e) {
		log.error("Error while hashing file {" + path + "}", e);
		throw error(INTERNAL_SERVER_ERROR, "node_error_upload_failed", e);
	}
}
 
Example 36
Project: spring-boot-concourse   File: FileUtils.java   View source code 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) {
				// Read the entire stream
			}
			return bytesToHex(inputStream.getMessageDigest().digest());
		}
		finally {
			inputStream.close();
		}
	}
	catch (NoSuchAlgorithmException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example 37
Project: android-wheels   File: HashUtils.java   View source code 6 votes vote down vote up
/**
 * Generate hash bytes for the specified stream, using specified algorithm
 *
 * @param inputStream Data stream
 * @param algorithm   Hashing algorithm
 * @return Hash bytes
 */
@NonNull
public static byte[] generateHashBytes(@NonNull InputStream inputStream, @NonNull String algorithm) {
    try {
        DigestInputStream digestStream = new DigestInputStream(inputStream, MessageDigest.getInstance(algorithm));
        byte[] buffer = new byte[BUFFER_SIZE];
        for (; ; ) {
            if (digestStream.read(buffer) < 0) {
                break;
            }
        }
        return digestStream.getMessageDigest().digest();
    } catch (NoSuchAlgorithmException | IOException e) {
        throw new RuntimeException(e);
    } finally {
        CommonUtils.close(inputStream);
    }
}
 
Example 38
Project: athenz   File: KeyRefresher.java   View source code 6 votes vote down vote up
/**
 *  If the checksum for the file has changed, then update the checksum and return true.  else return false
 */
protected boolean haveFilesBeenChanged(final String filePath, byte[] checksum) {
    try (InputStream is = Files.newInputStream(Paths.get(filePath));
         DigestInputStream digestInputStream = new DigestInputStream(is, md)) {
        while (digestInputStream.read() != -1) {
            ; // do nothing, just read until the EoF
        }
    } catch (IOException ignored) {
        //this is best effort, if we couldn't read the file, assume its the same
        LOGGER.warn("Error reading file " + filePath, ignored);
        return false;
    }
    byte[] digest = md.digest();
    if (!Arrays.equals(checksum, digest)) {
        //they aren't the same, overwrite old checksum
        checksum = digest;
        return true;
    }
    return false;
}
 
Example 39
Project: tazapp-android   File: HashHelper.java   View source code 6 votes vote down vote up
public static String getHash(File file, String algorithm) throws NoSuchAlgorithmException, IOException {
    MessageDigest digest = MessageDigest.getInstance(algorithm);
    digest.reset();

    BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
    DigestInputStream digestInputStream = new DigestInputStream(is,digest);

    byte[] bytes = new byte[1024];
    //noinspection StatementWithEmptyBody
    while (digestInputStream.read(bytes) != -1) {

    }

    digestInputStream.close();

    return convertByteArrayToHexString(digest.digest());
}
 
Example 40
Project: Tingeltangel   File: Book.java   View source code 6 votes vote down vote up
private String md5(File file) throws IOException {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(file);
        DigestInputStream dis = new DigestInputStream(is, md);
        byte[] buffer = new byte[4096];
        while(dis.read(buffer) >= 0) {}
        byte[] bytes = md.digest();
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    } catch(NoSuchAlgorithmException e) {
        throw new Error(e);
    }
}