當前位置: 首頁>>代碼示例>>Java>>正文


Java DigestInputStream類代碼示例

本文整理匯總了Java中java.security.DigestInputStream的典型用法代碼示例。如果您正苦於以下問題:Java DigestInputStream類的具體用法?Java DigestInputStream怎麽用?Java DigestInputStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


DigestInputStream類屬於java.security包,在下文中一共展示了DigestInputStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: encode

import java.security.DigestInputStream; //導入依賴的package包/類
/**
 * 獲取文件的 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;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:26,代碼來源:MD5Utils.java

示例2: putObject

import java.security.DigestInputStream; //導入依賴的package包/類
@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;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:MockAmazonS3.java

示例3: digest

import java.security.DigestInputStream; //導入依賴的package包/類
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();
  }
}
 
開發者ID:XDean,項目名稱:Java-EX,代碼行數:17,代碼來源:SecurityUtil.java

示例4: fileMd5Hash

import java.security.DigestInputStream; //導入依賴的package包/類
@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();
    }
}
 
開發者ID:google,項目名稱:mobly-bundled-snippets,代碼行數:19,代碼來源:FileSnippet.java

示例5: generateChecksum

import java.security.DigestInputStream; //導入依賴的package包/類
/**
 * 
 * @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;
}
 
開發者ID:rtr-nettest,項目名稱:open-rmbt,代碼行數:28,代碼來源:HttpProxyTask.java

示例6: lookup

import java.security.DigestInputStream; //導入依賴的package包/類
/**
 * 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;
}
 
開發者ID:salesforce,項目名稱:AptSpring,代碼行數:23,代碼來源:GsonDefinitionModelStore.java

示例7: hash

import java.security.DigestInputStream; //導入依賴的package包/類
/**
 * @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();
    }
}
 
開發者ID:tranleduy2000,項目名稱:javaide,代碼行數:20,代碼來源:Util.java

示例8: checkSHA256

import java.security.DigestInputStream; //導入依賴的package包/類
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));
    }
}
 
開發者ID:ZencashOfficial,項目名稱:zencash-swing-wallet-ui,代碼行數:19,代碼來源:ProvingKeyFetcher.java

示例9: calculateSHA256Digest

import java.security.DigestInputStream; //導入依賴的package包/類
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);
       }
}
 
開發者ID:ZencashOfficial,項目名稱:zencash-swing-wallet-ui,代碼行數:21,代碼來源:Util.java

示例10: checkSHA256

import java.security.DigestInputStream; //導入依賴的package包/類
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));
    }
}
 
開發者ID:ca333,項目名稱:komodoGUI,代碼行數:19,代碼來源:ProvingKeyFetcher.java

示例11: calculate

import java.security.DigestInputStream; //導入依賴的package包/類
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();
	}
}
 
開發者ID:spring-io,項目名稱:artifactory-resource,代碼行數:18,代碼來源:Checksums.java

示例12: writeRenameReadCompare

import java.security.DigestInputStream; //導入依賴的package包/類
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());
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:34,代碼來源:TestJets3tNativeFileSystemStore.java

示例13: assertMd5MatchesEtag

import java.security.DigestInputStream; //導入依賴的package包/類
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)));
    }
}
 
開發者ID:aws,項目名稱:aws-sdk-java-v2,代碼行數:14,代碼來源:S3TestUtils.java

示例14: hash

import java.security.DigestInputStream; //導入依賴的package包/類
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);
    }
}
 
開發者ID:aws,項目名稱:aws-sdk-java-v2,代碼行數:18,代碼來源:AbstractAwsSigner.java

示例15: hash

import java.security.DigestInputStream; //導入依賴的package包/類
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);
    }
}
 
開發者ID:IBM,項目名稱:ibm-cos-sdk-java,代碼行數:17,代碼來源:AbstractAWSSigner.java


注:本文中的java.security.DigestInputStream類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。