use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStream2Test method test_onZ.
/**
* java.security.DigestInputStream#on(boolean)
*/
public void test_onZ() throws Exception {
// Test for method void java.security.DigestInputStream.on(boolean)
MessageDigest originalDigest = (MessageDigest) (digest.clone());
MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
DigestInputStream dis = new DigestInputStream(inStream, noChangeDigest);
// turn off processing
dis.on(false);
// read some data
int c = dis.read();
assertEquals('T', c);
// make sure the digest for the part where it was off has not
// changed
assertTrue("MessageDigest changed even though processing was off", MessageDigest.isEqual(noChangeDigest.digest(), originalDigest.digest()));
MessageDigest changeDigest = (MessageDigest) (digest.clone());
dis = new DigestInputStream(inStream, digest);
// turn on processing
dis.on(true);
c = dis.read();
assertEquals('h', c);
// make sure the digest has changed
assertTrue("MessageDigest did not change with processing on", !MessageDigest.isEqual(digest.digest(), changeDigest.digest()));
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStream2Test method test_setMessageDigestLjava_security_MessageDigest.
/**
* java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
*/
public void test_setMessageDigestLjava_security_MessageDigest() {
// Test for method void
// java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
DigestInputStream dis = new DigestInputStream(inStream, null);
// make sure the digest is null when it's not been set
assertNull("Uninitialised MessageDigest should have been returned as null", dis.getMessageDigest());
dis.setMessageDigest(digest);
assertEquals("Wrong MessageDigest was returned.", digest, dis.getMessageDigest());
}
use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testDigestInputStream02.
/**
* Test #2 for <code>DigestInputStream</code> constructor<br>
*
* Assertion: creates new <code>DigestInputStream</code> instance
* using valid parameters (both <code>null</code>)
*/
public final void testDigestInputStream02() {
InputStream dis = new DigestInputStream(null, null);
assertTrue(dis instanceof DigestInputStream);
}
use of java.security.DigestInputStream in project robolectric by robolectric.
the class SQLiteLibraryLoader method md5sum.
private String md5sum(InputStream input) throws IOException {
BufferedInputStream in = new BufferedInputStream(input);
try {
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
DigestInputStream digestInputStream = new DigestInputStream(in, digest);
while (digestInputStream.read() >= 0) ;
ByteArrayOutputStream md5out = new ByteArrayOutputStream();
md5out.write(digest.digest());
return new BigInteger(md5out.toByteArray()).toString();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 algorithm is not available: " + e);
} finally {
in.close();
}
}
use of java.security.DigestInputStream in project AndroidUtilLib by SiberiaDante.
the class SDFileUtil method getFileMD5.
/**
* 获取文件的MD5校验码
*
* @param file 文件
* @return 文件的MD5校验码
*/
public static byte[] getFileMD5(File file) {
if (file == null)
return null;
DigestInputStream dis = null;
try {
FileInputStream fis = new FileInputStream(file);
MessageDigest md = MessageDigest.getInstance("MD5");
dis = new DigestInputStream(fis, md);
byte[] buffer = new byte[1024 * 256];
while (true) {
if (!(dis.read(buffer) > 0))
break;
}
md = dis.getMessageDigest();
return md.digest();
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
} finally {
SDCloseUtil.closeIO(dis);
}
return null;
}
Aggregations