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();
}
}
}
use of java.security.DigestInputStream in project android_frameworks_base by ParanoidAndroid.
the class ManifestDigest method fromInputStream.
static ManifestDigest fromInputStream(InputStream fileIs) {
if (fileIs == null) {
return null;
}
final MessageDigest md;
try {
md = MessageDigest.getInstance(DIGEST_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(DIGEST_ALGORITHM + " must be available", e);
}
final DigestInputStream dis = new DigestInputStream(new BufferedInputStream(fileIs), md);
try {
byte[] readBuffer = new byte[8192];
while (dis.read(readBuffer, 0, readBuffer.length) != -1) {
// not using
}
} catch (IOException e) {
Slog.w(TAG, "Could not read manifest");
return null;
} finally {
IoUtils.closeQuietly(dis);
}
final byte[] digest = md.digest();
return new ManifestDigest(digest);
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testReadbyteArrayintint01.
/**
* Test #1 for <code>read(byte[],int,int)</code> method<br>
*
* Assertion: returns the number of bytes read<br>
*
* Assertion: put bytes read into specified array at specified offset<br>
*
* Assertion: updates associated digest<br>
*/
public final void testReadbyteArrayintint01() 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);
byte[] bArray = new byte[MY_MESSAGE_LEN];
// check that read(byte[],int,int) returns valid value
assertTrue("retval", dis.read(bArray, 0, bArray.length) == MY_MESSAGE_LEN);
// check that bArray has been filled properly
assertTrue("bArray", Arrays.equals(myMessage, bArray));
// 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");
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testRead05.
/**
* Test #5 for <code>read()</code> method<br>
*
* Assertion: broken <code>DigestInputStream</code>instance:
* associated <code>MessageDigest</code> not set.
* <code>read()</code> must not work when digest
* functionality is on
*/
public final void testRead05() {
InputStream is = new ByteArrayInputStream(myMessage);
DigestInputStream dis = new DigestInputStream(is, null);
// must result in an exception
try {
for (int i = 0; i < MY_MESSAGE_LEN; i++) {
dis.read();
}
fail("read() must not work when digest functionality is on");
} catch (Exception e) {
// Expected.
}
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testRead04.
/**
* Test #4 for <code>read()</code> method<br>
*
* Assertion: broken <code>DigestInputStream</code>instance:
* <code>InputStream</code> not set. <code>read()</code> must
* not work
*/
public final void testRead04() throws IOException {
for (int ii = 0; ii < algorithmName.length; ii++) {
try {
MessageDigest md = MessageDigest.getInstance(algorithmName[ii]);
DigestInputStream dis = new DigestInputStream(null, md);
// must result in an exception
try {
for (int i = 0; i < MY_MESSAGE_LEN; i++) {
dis.read();
}
} catch (Exception e) {
// Expected.
return;
}
fail("InputStream not set. read() must not work");
} catch (NoSuchAlgorithmException e) {
// allowed failure
}
}
fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
Aggregations