use of java.security.DigestInputStream in project ceylon-compiler by ceylon.
the class Main method showClass.
/**
* Display the location and checksum of a class.
*/
void showClass(String className) {
out.println("javac: show class: " + className);
URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
if (url == null)
out.println(" class not found");
else {
out.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));
out.println(" " + algorithm + " checksum: " + sb);
} catch (Exception e) {
out.println(" cannot compute digest: " + e);
}
}
}
use of java.security.DigestInputStream in project mycore by MyCoRe-Org.
the class MCRSwordUtil method createTempFileFromStream.
/**
* Stores stream to temp file and checks md5
*
* @param inputStream the stream which holds the File
* @param checkMd5 the md5 to compare with (or null if no md5 check is needed)
* @return the path to the temp file
* @throws IOException if md5 does mismatch or if stream could not be read
*/
public static Path createTempFileFromStream(String fileName, InputStream inputStream, String checkMd5) throws IOException {
MCRSession currentSession = MCRSessionMgr.getCurrentSession();
if (currentSession.isTransactionActive()) {
currentSession.commitTransaction();
}
final Path zipTempFile = Files.createTempFile("swordv2_", fileName);
MessageDigest md5Digest = null;
if (checkMd5 != null) {
try {
md5Digest = MessageDigest.getInstance("MD5");
inputStream = new DigestInputStream(inputStream, md5Digest);
} catch (NoSuchAlgorithmException e) {
currentSession.beginTransaction();
throw new MCRConfigurationException("No MD5 available!", e);
}
}
Files.copy(inputStream, zipTempFile, StandardCopyOption.REPLACE_EXISTING);
if (checkMd5 != null) {
final String md5String = MCRUtils.toHexString(md5Digest.digest());
if (!md5String.equals(checkMd5)) {
currentSession.beginTransaction();
throw new IOException("MD5 mismatch, expected " + checkMd5 + " got " + md5String);
}
}
currentSession.beginTransaction();
return zipTempFile;
}
use of java.security.DigestInputStream in project AndroidUtilCode by Blankj.
the class EncryptUtils method encryptMD5File.
/**
* Return the bytes of file's MD5 encryption.
*
* @param file The file.
* @return the bytes of file's MD5 encryption
*/
public static byte[] encryptMD5File(final File file) {
if (file == null)
return null;
FileInputStream fis = null;
DigestInputStream digestInputStream;
try {
fis = new FileInputStream(file);
MessageDigest md = MessageDigest.getInstance("MD5");
digestInputStream = new DigestInputStream(fis, md);
byte[] buffer = new byte[256 * 1024];
while (true) {
if (!(digestInputStream.read(buffer) > 0))
break;
}
md = digestInputStream.getMessageDigest();
return md.digest();
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of java.security.DigestInputStream in project zm-mailbox by Zimbra.
the class ContentAddressableStoreManager method stage.
protected StagedBlob stage(InputStream in, long actualSize, Mailbox mbox, String locator) throws ServiceException {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw ServiceException.FAILURE("SHA-256 digest not found", e);
}
ByteUtil.PositionInputStream pin = new ByteUtil.PositionInputStream(new DigestInputStream(in, digest));
try {
writeStreamToStore(pin, actualSize, mbox, locator);
return new ExternalStagedBlob(mbox, ByteUtil.encodeFSSafeBase64(digest.digest()), pin.getPosition(), locator);
} catch (IOException e) {
throw ServiceException.FAILURE("unable to stage blob", e);
}
}
use of java.security.DigestInputStream in project box-java-sdk by box.
the class LargeFileUpload method generateDigest.
/**
* Generates the Base64 encoded SHA-1 hash for content available in the stream.
* It can be used to calculate the hash of a file.
*
* @param stream the input stream of the file or data.
* @return the Base64 encoded hash string.
*/
public String generateDigest(InputStream stream) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(DIGEST_ALGORITHM_SHA1);
} catch (NoSuchAlgorithmException ae) {
throw new BoxAPIException("Digest algorithm not found", ae);
}
// Calcuate the digest using the stream.
DigestInputStream dis = new DigestInputStream(stream, digest);
try {
int value = dis.read();
while (value != -1) {
value = dis.read();
}
} catch (IOException ioe) {
throw new BoxAPIException("Reading the stream failed.", ioe);
}
// Get the calculated digest for the stream
byte[] digestBytes = digest.digest();
return Base64.encode(digestBytes);
}
Aggregations