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


Java DigestInputStream.close方法代碼示例

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


在下文中一共展示了DigestInputStream.close方法的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: 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

示例3: 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

示例4: getMd5

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

示例5: computeFileDigest

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

示例6: sha1Hash

import java.security.DigestInputStream; //導入方法依賴的package包/類
/**
 * 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);
	}
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:26,代碼來源:FileUtils.java

示例7: inSolr

import java.security.DigestInputStream; //導入方法依賴的package包/類
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;
}
 
開發者ID:little9,項目名稱:SolrFits,代碼行數:27,代碼來源:DupeChecker.java

示例8: getApkDigestSha256

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

示例9: getHash

import java.security.DigestInputStream; //導入方法依賴的package包/類
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());
}
 
開發者ID:die-tageszeitung,項目名稱:tazapp-android,代碼行數:18,代碼來源:HashHelper.java

示例10: getMd5

import java.security.DigestInputStream; //導入方法依賴的package包/類
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;
    }
}
 
開發者ID:Asprise,項目名稱:java-scanner-access-twain,代碼行數:26,代碼來源:Utils.java

示例11: digestOpt

import java.security.DigestInputStream; //導入方法依賴的package包/類
static String digestOpt(Key k, String dg) throws IOException {
    try {
        MessageDigest md5 = MessageDigest.getInstance(dg);

        DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
        try {
            while (in.read(new byte[128]) > 0)
                ; // simply discard the input
        } finally {
            in.close();
        }
        StringBuilder buf = new StringBuilder();
        char[] hex = Hex.encodeHex(md5.digest());
        for( int i=0; i<hex.length; i+=2 ) {
            if(buf.length()>0)  buf.append(':');
            buf.append(hex,i,2);
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}
 
開發者ID:hudson3-plugins,項目名稱:ec2-plugin,代碼行數:23,代碼來源:EC2PrivateKey.java

示例12: md5

import java.security.DigestInputStream; //導入方法依賴的package包/類
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 "";
	}
}
 
開發者ID:laurent-clouet,項目名稱:sheepit-client,代碼行數:18,代碼來源:Utils.java

示例13: digest

import java.security.DigestInputStream; //導入方法依賴的package包/類
public static String digest(X509Certificate k) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("SHA1");
    DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
    try {
        while (in.read(new byte[128]) > 0) {
        }
    } finally {
        in.close();
    }
    StringBuilder buf = new StringBuilder();
    char[] hex = Hex.encodeHex(md5.digest());
    for (int i = 0; i < hex.length; i += 2) {
        if (buf.length() > 0)
            buf.append(':');
        buf.append(hex, i, 2);
    }
    return buf.toString();
}
 
開發者ID:rancher,項目名稱:cattle,代碼行數:19,代碼來源:SslCertificateUtils.java

示例14: calcHash

import java.security.DigestInputStream; //導入方法依賴的package包/類
private static String calcHash(InputStream input, String algorithm) throws IOException {
    try {
        final MessageDigest md = MessageDigest.getInstance(algorithm);
        final DigestInputStream dis = new DigestInputStream(input, md);
        try {
            byte[] buff = new byte[4096];
            // just read to get the Digest filled...
            while (dis.read(buff) > 0) {
                // nop
            }
            return new BigInteger(1, md.digest()).toString(16);
        } finally {
            dis.close();
        }
    } catch (NoSuchAlgorithmException e) {
        // this should not happen
        return "";
    }
}
 
開發者ID:apache,項目名稱:marmotta,代碼行數:20,代碼來源:HashUtils.java

示例15: generateImageMD5

import java.security.DigestInputStream; //導入方法依賴的package包/類
private MediaFileDataHash generateImageMD5(final File file) throws Exception {
  try {
    MessageDigest messageDigest = MessageDigest.getInstance("Md5");
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, messageDigest);

    // read the file and update the hash calculation
    boolean haveMore = true;
    while (haveMore) {
      haveMore = dis.read() != -1;
    }

    dis.close();

    // get the hash value as byte array
    byte[] hash = messageDigest.digest();
    return new MediaFileDataHash(byteArray2Hex(hash));
  }
  catch (NoSuchAlgorithmException | IOException e) {
    LOGGER.error("Could not generate image unique id", e);
    throw new CouldNotGenerateIDException();
  }
}
 
開發者ID:kotlinski,項目名稱:image-sort-master,代碼行數:25,代碼來源:MediaFileHashGenerator.java


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