use of java.security.DigestInputStream in project robovm by robovm.
the class DigestInputStreamTest method testRead02.
/**
* 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");
}
use of java.security.DigestInputStream in project routerkeygenAndroid by routerkeygen.
the class HashUtils method checkDicMD5.
// Check RouterKeygen.dic file through md5
public static boolean checkDicMD5(String dicFile, final byte[] expected) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
InputStream is = new FileInputStream(dicFile);
try {
is = new DigestInputStream(is, md);
while (is.read() != -1) {
}
} finally {
is.close();
}
final byte[] hash = md.digest();
return Arrays.equals(hash, expected);
} catch (Exception e) {
return false;
}
}
use of java.security.DigestInputStream in project AndroidUtilCode by Blankj.
the class FileUtils 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 (dis.read(buffer) > 0) ;
md = dis.getMessageDigest();
return md.digest();
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
} finally {
CloseUtils.closeIO(dis);
}
return null;
}
use of java.security.DigestInputStream in project jdk8u_jdk by JetBrains.
the class TestDigestIOStream method testMDShare.
/**
* Test DigestInputStream and DigestOutputStream digest function when use
* same message digest object.
*
* @param algo
* Message Digest algorithm
* @param dataLength
* plain test data length.
* @exception Exception
* throw unexpected exception
*/
public boolean testMDShare(String algo, int dataLength) throws Exception {
MessageDigest mdCommon = MessageDigest.getInstance(algo);
// Generate the DigestInputStream/DigestOutputStream object
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
DigestInputStream dis = new DigestInputStream(bais, mdCommon);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DigestOutputStream dos = new DigestOutputStream(baos, mdCommon)) {
// 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 < data.length) {
int len = dis.read(buffer, 0, buffer.length);
if (len != -1) {
k += len;
if (k < data.length) {
dos.write(data[k]);
k++;
dis.skip(1);
}
}
}
// Get the output and the "correct" digest values
byte[] output = mdCommon.digest();
byte[] standard = md.digest(data);
// Compare generated digest values
return MessageDigest.isEqual(output, standard);
} catch (Exception ex) {
out.println("TestMDShare failed at:" + algo + "/" + dataLength + " with unexpected exception");
throw ex;
}
}
use of java.security.DigestInputStream in project jdk8u_jdk by JetBrains.
the class CipherStreamClose method streamDecrypt.
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();
}
}
}
Aggregations