下面列出了怎么用java.security.DigestInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
}
protected synchronized void download() throws RepoException, ValidationException {
if (downloaded) {
return;
}
URL remote = getRemote();
try {
console.progressFmt("Fetching %s", remote);
ByteSink sink = getSink();
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (ProfilerTask task = profiler.start("remote_file_" + remote)) {
try (DigestInputStream is = new DigestInputStream(transport.open(remote), digest)) {
sink.writeFrom(is);
sha256 = Optional.of(Bytes.asList(is.getMessageDigest().digest()).stream()
.map(b -> String.format("%02X", b)).collect(Collectors.joining()).toLowerCase());
}
downloaded = true;
}
} catch (IOException | NoSuchAlgorithmException e) {
throw new RepoException(String.format("Error downloading %s", remote), e);
}
}
/**
* Test #3 for <code>read()</code> method<br>
* Test #1 for <code>on(boolean)</code> method<br>
*
* Assertion: <code>read()</code> must not update digest if it is off<br>
* Assertion: <code>on(boolean)</code> turns digest functionality on
* (if <code>true</code> passed as a parameter) or off (if <code>false</code>
* passed)
*/
public final void testRead03()
throws IOException {
for (int ii=0; ii<algorithmName.length; ii++) {
try {
MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
InputStream is = new ByteArrayInputStream(myMessage);
DigestInputStream dis = new DigestInputStream(is, md);
// turn digest off
dis.on(false);
for (int i=0; i<MY_MESSAGE_LEN; i++) {
dis.read();
}
// check that digest value has not been updated by read()
assertTrue(Arrays.equals(dis.getMessageDigest().digest(),
MDGoldenData.getDigest(algorithmName[ii]+"_NU")));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
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;
}
}
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 "";
}
}
public static String getMD5( File f ) throws Exception
{
MessageDigest md = MessageDigest.getInstance("MD5");
BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f ) );
DigestInputStream dis = new DigestInputStream(bis, md);
byte[] buf = new byte[8192];
for(;;)
{
int len = dis.read( buf );
if ( len <= 0 )
{
break;
}
}
dis.close();
byte[] bytes = md.digest();
StringBuilder sb = new StringBuilder();
for (int j = 0; j < bytes.length; j++)
{
int v = bytes[j] & 0xff;
sb.append( hexChar( v >>> 4 ) ).append( hexChar( v & 0xf ) );
}
return sb.toString();
}
/**
* Test #2 for <code>read()</code> method<br>
*
* Assertion: returns -1 if EOS had been
* reached but not read before method call<br>
*
* Assertion: must not update digest if EOS had been
* reached but not read before method call<br>
*/
public final void testRead02()
throws IOException {
for (int ii=0; ii<algorithmName.length; ii++) {
try {
MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
InputStream is = new ByteArrayInputStream(myMessage);
DigestInputStream dis = new DigestInputStream(is, md);
for (int i=0; i<MY_MESSAGE_LEN; i++) {
dis.read();
}
// check that subsequent read() calls return -1 (eos)
assertEquals("retval1", -1, dis.read());
assertEquals("retval2", -1, dis.read());
assertEquals("retval3", -1, dis.read());
// check that 3 previous read() calls did not update digest
assertTrue("update",
Arrays.equals(dis.getMessageDigest().digest(),
MDGoldenData.getDigest(algorithmName[ii])));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
/**
* @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();
}
}
/**
* 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) { //NOPMD
// Read the entire stream
}
return bytesToHex(inputStream.getMessageDigest().digest());
} finally {
inputStream.close();
}
} catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException(ex);
}
}
@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();
}
}
private static boolean checkSHA256(final File provingKey, final Component parent) throws IOException {
final MessageDigest sha256;
try {
sha256 = MessageDigest.getInstance("SHA-256");
} catch (final NoSuchAlgorithmException impossible) {
throw new RuntimeException(impossible);
}
try (final InputStream is = new BufferedInputStream(new FileInputStream(provingKey))) {
final ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, "Verifying proving key", is);
pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
pmis.getProgressMonitor().setMillisToPopup(10);
final DigestInputStream dis = new DigestInputStream(pmis, sha256);
final byte[] temp = new byte[0x1 << 13];
while (dis.read(temp) >= 0) {
/* do the thing */
}
final byte[] digest = sha256.digest();
return SHA256.equalsIgnoreCase(DatatypeConverter.printHexBinary(digest));
}
}
private static String computeMD5ChecksumForInputStream(
final InputStream inputStream) throws NoSuchAlgorithmException,
IOException {
final MessageDigest md = MessageDigest.getInstance("MD5");
try {
final InputStream digestInputStream = new DigestInputStream(inputStream,
md);
while (digestInputStream.read() > 0) {
// do nothing
}
} finally {
inputStream.close();
}
final byte[] digest = md.digest();
final StringBuffer sb = new StringBuffer();
for (final byte element : digest) {
sb.append(Integer.toString((element & 0xff) + 0x100, 16)
.substring(1));
}
return sb.toString();
}
/**
* Test #1 for <code>read()</code> method<br>
*
* Assertion: returns the byte read<br>
* Assertion: updates associated digest<br>
*/
public final void testRead01()
throws IOException {
for (int ii=0; ii<algorithmName.length; ii++) {
try {
MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
InputStream is = new ByteArrayInputStream(myMessage);
DigestInputStream dis = new DigestInputStream(is, md);
for (int i=0; i<MY_MESSAGE_LEN; i++) {
// check that read() returns valid values
assertTrue("retval", ((byte)dis.read() == myMessage[i]));
}
// check that associated digest has been updated properly
assertTrue("update",
Arrays.equals(
dis.getMessageDigest().digest(),
MDGoldenData.getDigest(algorithmName[ii])));
return;
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
/**
* @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();
}
}
/**
* 获取文件的 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;
}
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,
LanguageUtil.instance().getString("proving.key.fetcher.option.pane.verify.progress.monitor.text"),
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(Util.bytesToHex(digest));
}
}
private static boolean checkSHA256SG(File sproutGroth, 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(sproutGroth))) {
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent,
LanguageUtil.instance().getString("sprout.groth.fetcher.option.pane.verify.progress.monitor.text"),
is);
pmis.getProgressMonitor().setMaximum(SPROUT_GROTH_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 SHA256SG.equalsIgnoreCase(Util.bytesToHex(digest));
}
}
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);
}
}
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 SdkClientException.builder()
.message("Unable to compute hash while signing request: " + e.getMessage())
.cause(e)
.build();
}
}
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);
}
}
static String md5sum(InputStream input) throws IOException {
BufferedInputStream in = new BufferedInputStream(input);
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
DigestInputStream digestInputStream = new DigestInputStream(in, digest);
boolean bytesRead = false;
byte[] buffer = new byte[8192];
while(digestInputStream.read(buffer) != -1) {
// CHECKSTYLE:OFF EmptyStatement
;
// CHECKSTYLE:ON
}
ByteArrayOutputStream md5out = new ByteArrayOutputStream();
md5out.write(digest.digest());
String md5String = md5out.toString();
return md5String;
} catch (NoSuchAlgorithmException noSuchAlgorithmException) {
throw new IllegalStateException("MD5 algorithm is not available: " + noSuchAlgorithmException);
} finally {
in.close();
}
}
/**
*
* @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;
}
public static String getMd5Checksum(InputStream in) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw Exceptions.propagate(e);
}
DigestInputStream dis = new DigestInputStream(in, md);
readFullyAndClose(dis);
byte[] digest = md.digest();
StringBuilder result = new StringBuilder();
for (byte b: digest) {
result.append(Strings.padStart(Integer.toHexString((256+b)%256), 2, '0'));
}
return result.toString().toUpperCase();
}
/**
* java.security.DigestInputStream#read(byte[], int, int)
*/
public void test_read$BII() throws IOException {
// Test for method int java.security.DigestInputStream.read(byte [],
// int, int)
DigestInputStream dis = new DigestInputStream(inStream, digest);
int bytesToRead = inStream.available();
byte buf1[] = new byte[bytesToRead + 5];
byte buf2[] = new byte[bytesToRead + 5];
// make sure we're actually reading some data
assertTrue("No data to read for this test", bytesToRead>0);
// read and compare the data that the inStream has
int bytesRead1 = dis.read(buf1, 5, bytesToRead);
int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
assertEquals("Didn't read the same from each stream", bytesRead1,
bytesRead2);
assertEquals("Didn't read the entire", bytesRead1, bytesToRead);
// compare the arrays
boolean same = true;
for (int i = 0; i < bytesToRead + 5; i++) {
if (buf1[i] != buf2[i]) {
same = false;
}
}// end for
assertTrue("Didn't get the same data", same);
}
public static String getDigestString(final File file) throws Exception {
final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
final FileInputStream fileInputStream = new FileInputStream(file);
final DigestInputStream digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
final byte[] buffer = new byte[8192];
for (int bytesRead = 1; bytesRead > 0;) {
bytesRead = digestInputStream.read(buffer);
}
final byte[] digestValue = messageDigest.digest();
final StringBuilder sb = new StringBuilder(digestValue.length);
for (final byte b : digestValue) {
sb.append(b);
}
return sb.toString();
}
private BoxFile.Info uploadHelper(BoxFileUploadSession.Info session, InputStream stream, long fileSize,
Map<String, String> fileAttributes)
throws InterruptedException, IOException {
//Upload parts using the upload session
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance(DIGEST_ALGORITHM_SHA1);
} catch (NoSuchAlgorithmException ae) {
throw new BoxAPIException("Digest algorithm not found", ae);
}
DigestInputStream dis = new DigestInputStream(stream, digest);
List<BoxFileUploadSessionPart> parts = this.uploadParts(session, dis, fileSize);
//Creates the file hash
byte[] digestBytes = digest.digest();
String digestStr = Base64.encode(digestBytes);
//Commit the upload session. If there is a failure, abort the commit.
try {
return session.getResource().commit(digestStr, parts, fileAttributes, null, null);
} catch (Exception e) {
session.getResource().abort();
throw new BoxAPIException("Unable to commit the upload session", e);
}
}
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
byte[] content;
byte[] newHash;
LOG.debug("Start processing file: {}", path);
try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
// Get content
content = IOUtils.toByteArray(is);
// Get hash
newHash = is.getMessageDigest().digest();
}
if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
LOG.info("Uploading file: {}", path);
ObjectMetadata bucketMetadata = new ObjectMetadata();
bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
bucketMetadata.setContentLength(content.length);
// Give Tika a few hints for the content detection
Metadata tikaMetadata = new Metadata();
tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
// Fire!
try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
s3.putObject(bucket.getName(), path, bis, bucketMetadata);
return true;
}
}
LOG.info("Skipping file (same checksum): {}", path);
return false;
}
public static Object streamDecrypt(byte[] data, SecretKey key,
MessageDigest digest) throws Exception {
Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
decCipher.init(Cipher.DECRYPT_MODE, key);
digest.reset();
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
DigestInputStream dis = new DigestInputStream(bis, digest);
CipherInputStream cis = new CipherInputStream(dis, decCipher)) {
try (ObjectInputStream ois = new ObjectInputStream(cis)) {
return ois.readObject();
}
}
}
public static File unzip(InputStream is, String eTag) throws IOException {
File tmpDir = FileUtils.getTempDirectory();
File modelDir = new File(tmpDir, "models");
FileUtils.forceMkdir(modelDir);
File tmp = File.createTempFile("model", ".download");
FileUtils.forceDelete(tmp);
FileUtils.forceMkdir(tmp);
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
ZipUtils.unzip(new DigestInputStream(is, md), tmp);
if (eTag == null) {
eTag = HexUtils.toHexString(md.digest());
}
File dir = new File(modelDir, eTag);
if (dir.exists()) {
FileUtils.deleteDirectory(tmp);
logger.info("model folder already exists: {}", eTag);
return dir;
}
FileUtils.moveDirectory(tmp, dir);
return dir;
}
/** Display the location and checksum of a class. */
void showClass(String className) {
PrintWriter pw = log.getWriter(WriterKind.NOTICE);
pw.println("javac: show class: " + className);
URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
if (url == null)
pw.println(" class not found");
else {
pw.println(" " + url);
try {
final String algorithm = "MD5";
byte[] digest;
MessageDigest md = MessageDigest.getInstance(algorithm);
DigestInputStream in = new DigestInputStream(url.openStream(), md);
try {
byte[] buf = new byte[8192];
int n;
do { n = in.read(buf); } while (n > 0);
digest = md.digest();
} finally {
in.close();
}
StringBuilder sb = new StringBuilder();
for (byte b: digest)
sb.append(String.format("%02x", b));
pw.println(" " + algorithm + " checksum: " + sb);
} catch (Exception e) {
pw.println(" cannot compute digest: " + e);
}
}
}