Java 类java.security.DigestInputStream 实例源码

项目:GitHub    文件:MD5Utils.java   
/**
 * 获取文件的 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;
}
项目:elasticsearch_my    文件:MockAmazonS3.java   
@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;
}
项目:Java-EX    文件:SecurityUtil.java   
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();
  }
}
项目:mobly-bundled-snippets    文件:FileSnippet.java   
@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();
    }
}
项目:open-rmbt    文件:HttpProxyTask.java   
/**
 * 
 * @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;
}
项目:AptSpring    文件:GsonDefinitionModelStore.java   
/**
 * 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;
}
项目:javaide    文件:Util.java   
/**
 * @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();
    }
}
项目:zencash-swing-wallet-ui    文件:ProvingKeyFetcher.java   
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));
    }
}
项目:zencash-swing-wallet-ui    文件:Util.java   
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);
       }
}
项目:komodoGUI    文件:ProvingKeyFetcher.java   
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));
    }
}
项目:artifactory-resource    文件:Checksums.java   
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();
    }
}
项目:hadoop    文件:TestJets3tNativeFileSystemStore.java   
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());
}
项目:aws-sdk-java-v2    文件:AbstractAwsSigner.java   
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);
    }
}
项目:ibm-cos-sdk-java    文件:AbstractAWSSigner.java   
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);
    }
}
项目:S3Mock    文件:FileStore.java   
/**
 * 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);
  }
}
项目:dremio-oss    文件:ShimLoader.java   
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();
  }
}
项目:MineskinSponge    文件:MineskinServiceImpl.java   
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());
}
项目:fileserver    文件:FileScan.java   
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);
    }
项目:quadracoatl    文件:Md5Hasher.java   
/**
 * 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());
}
项目:java-installer    文件:MainApp.java   
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();
    }
}
项目:mycore    文件:MCRContentInputStream.java   
/**
 * 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;
}
项目:CloudMusicLH    文件:MD5Util.java   
/**
 * 获取文件的 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;
}
项目:aws-encryption-sdk-java    文件:TestIOUtils.java   
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;
}
项目:Java-EX    文件:SecurityUtil.java   
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();
  }
}
项目:okhttp-OkGo    文件:MD5Utils.java   
/**
 * 获取文件的 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;
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:FileUtils.java   
/**
 * 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);
    }
}
项目:azkaban    文件:Md5Hasher.java   
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();
}
项目:SolrFits    文件:DupeChecker.java   
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;
}
项目:JavaAyo    文件:MD5Utils.java   
/**
 * 获取文件的 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;
}
项目:LibreOCR    文件:FileUtil.java   
/**
 * 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();
}
项目:SafetyNetHelper    文件:SafetyNetHelper.java   
@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;
  }
}
项目:aliyun-tablestore-java-sdk    文件:StreamUtils.java   
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();
    }
项目:mesh    文件:FileUtils.java   
/**
 * 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);
    }
}
项目:mesh    文件:FileUtils.java   
/**
 * 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);
    }
}
项目:spring-boot-concourse    文件:FileUtils.java   
/**
 * 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);
    }
}
项目:android-wheels    文件:HashUtils.java   
/**
 * 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);
    }
}
项目:athenz    文件:KeyRefresher.java   
/**
 *  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;
}
项目:tazapp-android    文件:HashHelper.java   
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());
}
项目:Tingeltangel    文件:Book.java   
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);
    }
}
项目:open-ig    文件:PackageStuff.java   
/**
 * Compute the SHA1 digest of the given file.
 * @param fileName the filename
 * @return the digest string + the filename
 * @throws IOException on error
 */
static String computeDigest(String fileName) throws IOException {
    try (FileInputStream fis = new FileInputStream(fileName);
           DigestInputStream din = new DigestInputStream(fis, MessageDigest.getInstance("SHA1"))) {
        byte[] buffer = new byte[8192];
        while (true) {
            if (din.read(buffer) < 0) {
                break;
            }
        }
        byte[] digest = din.getMessageDigest().digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02X", b & 0xFF));
        }
        sb.append(" ").append(fileName).append("\r\n");
        return sb.toString();
    } catch (NoSuchAlgorithmException ex) {
        Exceptions.add(ex);
    }
    return "";
}