本文整理匯總了Java中java.security.DigestInputStream.read方法的典型用法代碼示例。如果您正苦於以下問題:Java DigestInputStream.read方法的具體用法?Java DigestInputStream.read怎麽用?Java DigestInputStream.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.DigestInputStream
的用法示例。
在下文中一共展示了DigestInputStream.read方法的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;
}
示例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();
}
}
示例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;
}
示例4: 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();
}
}
示例5: 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));
}
}
示例6: 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);
}
}
示例7: 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));
}
}
示例8: 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);
}
}
示例9: 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);
}
}
示例10: md5sum
import java.security.DigestInputStream; //導入方法依賴的package包/類
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();
}
}
示例11: 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);
}
示例12: hashFile
import java.security.DigestInputStream; //導入方法依賴的package包/類
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();
}
}
示例13: 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;
}
示例14: 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
示例15: generateHashBytes
import java.security.DigestInputStream; //導入方法依賴的package包/類
/**
* 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);
}
}