use of java.security.DigestInputStream in project hadoop by apache.
the class MD5FileUtils method computeMd5ForFile.
/**
* Read dataFile and compute its MD5 checksum.
*/
public static MD5Hash computeMd5ForFile(File dataFile) throws IOException {
InputStream in = new FileInputStream(dataFile);
try {
MessageDigest digester = MD5Hash.getDigester();
DigestInputStream dis = new DigestInputStream(in, digester);
IOUtils.copyBytes(dis, new IOUtils.NullOutputStream(), 128 * 1024);
return new MD5Hash(digester.digest());
} finally {
IOUtils.closeStream(in);
}
}
use of java.security.DigestInputStream in project jodd by oblac.
the class FileUtil method digest.
// ---------------------------------------------------------------- digests
/**
* Calculates digest for a file using provided algorithm.
*/
public static byte[] digest(final File file, MessageDigest algorithm) throws IOException {
algorithm.reset();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DigestInputStream dis = new DigestInputStream(bis, algorithm);
try {
while (dis.read() != -1) {
}
} finally {
StreamUtil.close(fis);
}
return algorithm.digest();
}
use of java.security.DigestInputStream in project jdk8u_jdk by JetBrains.
the class TestDigestIOStream method testMDChange.
/**
* Test DigestInputStream and DigestOutputStream digest function when Swap
* the message digest engines between DigestIn/OutputStream
*
* @param algo
* Message Digest algorithm
* @param dataLength
* plain test data length.
* @exception Exception
* throw unexpected exception
*/
public boolean testMDChange(String algo, int dataLength) throws Exception {
// Generate the DigestInputStream/DigestOutputStream object
MessageDigest mdIn = MessageDigest.getInstance(algo);
MessageDigest mdOut = MessageDigest.getInstance(algo);
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
DigestInputStream dis = new DigestInputStream(bais, mdIn);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DigestOutputStream dos = new DigestOutputStream(baos, mdOut)) {
// Perform the update using all available/possible update methods
int k = 0;
byte[] buffer = new byte[10];
// use both read() and read(byte[], int, int)
while ((k = dis.read()) != -1) {
dos.write(k);
if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
dos.write(buffer, 0, k);
}
// Swap the message digest engines between
// DigestIn/OutputStream objects
dis.setMessageDigest(mdOut);
dos.setMessageDigest(mdIn);
mdIn = dis.getMessageDigest();
mdOut = dos.getMessageDigest();
}
// Get the output and the "correct" digest values
byte[] output1 = mdIn.digest();
byte[] output2 = mdOut.digest();
byte[] standard = md.digest(data);
// Compare generated digest values
return MessageDigest.isEqual(output1, standard) && MessageDigest.isEqual(output2, standard);
} catch (Exception ex) {
out.println("testMDChange failed at:" + algo + "/" + dataLength + " with unexpected exception");
throw ex;
}
}
use of java.security.DigestInputStream in project jdk8u_jdk by JetBrains.
the class TestDigestIOStream method testDigestOnOff.
/**
* Test DigestInputStream and DigestOutputStream digest function when digest
* set on and off
*
* @param algo
* Message Digest algorithm
* @param readModel
* which read method used(READ, BUFFER_READ, MIX_READ)
* @param on
* digest switch(on and off)
* @param dataLength
* plain test data length.
* @exception Exception
* throw unexpected exception
*/
public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on, int dataLength) throws Exception {
// Generate the DigestInputStream/DigestOutputStream object
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
DigestInputStream dis = new DigestInputStream(bais, MessageDigest.getInstance(algo));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DigestOutputStream dos = new DigestOutputStream(baos, MessageDigest.getInstance(algo));
ByteArrayOutputStream baOut = new ByteArrayOutputStream()) {
// Perform the update using all available/possible update methods
int k = 0;
byte[] buffer = new byte[5];
boolean enDigest = true;
// Make sure the digest function is on (default)
dis.on(enDigest);
dos.on(enDigest);
switch(readModel) {
case // use only read()
READ:
while ((k = dis.read()) != -1) {
if (on) {
dos.write(k);
} else {
dos.write(k);
if (enDigest) {
baOut.write(k);
}
enDigest = !enDigest;
dos.on(enDigest);
dis.on(enDigest);
}
}
break;
case // use only read(byte[], int, int)
BUFFER_READ:
while ((k = dis.read(buffer, 0, buffer.length)) != -1) {
if (on) {
dos.write(buffer, 0, k);
} else {
dos.write(buffer, 0, k);
if (enDigest) {
baOut.write(buffer, 0, k);
}
enDigest = !enDigest;
dis.on(enDigest);
dos.on(enDigest);
}
}
break;
case // use both read() and read(byte[], int, int)
MIX_READ:
while ((k = dis.read()) != -1) {
if (on) {
dos.write(k);
if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
dos.write(buffer, 0, k);
}
} else {
dos.write(k);
if (enDigest) {
baOut.write(k);
}
enDigest = !enDigest;
dis.on(enDigest);
dos.on(enDigest);
if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
dos.write(buffer, 0, k);
if (enDigest) {
baOut.write(buffer, 0, k);
}
enDigest = !enDigest;
dis.on(enDigest);
dos.on(enDigest);
}
}
}
break;
default:
out.println("ERROR: Invalid read/write combination choice!");
return false;
}
// Get the output and the "correct" digest values
byte[] output1 = dis.getMessageDigest().digest();
byte[] output2 = dos.getMessageDigest().digest();
byte[] standard;
if (on) {
standard = md.digest(data);
} else {
byte[] dataDigested = baOut.toByteArray();
standard = md.digest(dataDigested);
}
// Compare the output byte array value to the input data
if (!MessageDigest.isEqual(data, baos.toByteArray())) {
out.println("ERROR of " + readModel + ": output and input data unexpectedly changed");
return false;
}
// Compare generated digest values
if (!MessageDigest.isEqual(output1, standard) || !MessageDigest.isEqual(output2, standard)) {
out.println("ERROR" + readModel + ": generated digest data unexpectedly changed");
return false;
}
return true;
} catch (Exception ex) {
out.println("testDigestOnOff failed at:" + algo + "/" + readModel + "/" + dataLength + " with unexpected exception");
throw ex;
}
}
use of java.security.DigestInputStream in project UltimateAndroid by cymcsg.
the class CryptographyUtils method getMd5FromFile.
/**
* Get the MD5 of the file
*
* @param filePath
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
*/
public static String getMd5FromFile(String filePath) throws IOException, NoSuchAlgorithmException {
int bufferSize = 256 * 1024;
FileInputStream fileInputStream = null;
DigestInputStream digestInputStream = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
fileInputStream = new FileInputStream(filePath);
digestInputStream = new DigestInputStream(fileInputStream, messageDigest);
byte[] buffer = new byte[bufferSize];
while (digestInputStream.read(buffer) > 0) ;
messageDigest = digestInputStream.getMessageDigest();
byte[] resultByteArray = messageDigest.digest();
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
throw e;
} finally {
if (digestInputStream != null) {
digestInputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
Aggregations