本文整理汇总了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);
}
}