Example usage for java.security DigestInputStream read

List of usage examples for java.security DigestInputStream read

Introduction

In this page you can find the example usage for java.security DigestInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller.java

/**
 * Calcultes the check sum.//w w  w .j  av a  2  s.c  o m
 *
 * @return the check sum as hexidecimal
 */
private String calculateChecksum() {
    // we have to create the checksum for the mzML file (from its beginning to the
    // end of the fileChecksum start tag).
    // Since this stop location is very near the end of the file, we skip everything
    // until we come within a certain limit of the end of the file
    long limit = mzMLFile.length() - 200L;
    logger.debug("Looking for fileChecksum tag between byte " + limit + " and byte " + mzMLFile.length()
            + " (the end) of the mzML file.");

    // initialize the hash algorithm
    MessageDigest hash;
    try {
        hash = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("SHA-1 not recognized as Secure Hash Algorithm.", e);
    }

    // create the input stream that will calculate the checksum
    FileInputStream fis;
    try {
        fis = new FileInputStream(mzMLFile);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("File " + mzMLFile.getAbsoluteFile() + " could not be found!", e);
    }
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, hash);

    // prepare for input stream processing
    // we read through the file until we reach a specified limit before the end of the file
    // from there we populate a buffer with the read bytes (characters) and check if we have
    // already reached the position up to where we have to calculate the hash.
    CircularFifoBuffer bBuf = new CircularFifoBuffer(15);
    long cnt = 0; // counter to keep track of our position
    byte[] b = new byte[1]; // we only read one byte at a time
    try {
        while (dis.read(b) >= 0) {
            bBuf.add(b[0]);
            cnt++;
            // check if we have already reached the last bit of the file, where we have
            // to find the right position to stop (after the 'fileChecksum' start tag)
            if (cnt > limit) {
                // we should have reached the start of the <fileChecksum> tag,
                // now we have to find the end
                String readBuffer = convert2String(bBuf);
                if (readBuffer.endsWith("<fileChecksum>")) {
                    // we have found the end of the fileChecksum start tag, we have to stop the hash
                    if (b[0] != '>') { // check that we are really at the last character of the tag
                        throw new IllegalStateException("We are not at the end of <fileChecksum> tag!");
                    }
                    break;
                }
            } // else if not yet near the end of the file, just keep on going
        }
        dis.close();
    } catch (IOException e) {
        throw new IllegalStateException(
                "Could not read from file '" + mzMLFile.getAbsolutePath() + "' while trying ot calculate hash.",
                e);
    }
    logger.debug("Read over " + cnt + " bytes while calculating the file hash.");

    byte[] bytesDigest = dis.getMessageDigest().digest();

    return asHex(bytesDigest);
}

From source file:org.calrissian.mango.jms.stream.AbstractJmsFileTransferSupport.java

@SuppressWarnings("unchecked")
public void sendStream(Request req, final Destination replyTo) throws IOException {

    DigestInputStream is;
    Assert.notNull(req, "Request cannot be null");
    final URI downloadUrl;
    try {/*www  .j a v  a2  s .  c  o  m*/
        downloadUrl = new URI(req.getDownloadUri());
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
    try {

        is = new DigestInputStream(new BufferedInputStream(streamOpener.openStream(downloadUrl)),
                MessageDigest.getInstance(getHashAlgorithm()));

    } catch (NoSuchAlgorithmException e) {
        throw new JmsFileTransferException(e);
    } catch (Throwable e) {

        logger.info("Error occurred opening stream: " + e);
        return;
    }

    MessageQueueListener queueListener = null;
    try {

        @SuppressWarnings("rawtypes")
        Message returnMessage = (Message) jmsTemplate.execute(new SessionCallback() {

            @Override
            public Object doInJms(Session session) throws JMSException {
                DestinationRequestor requestor = null;
                try {
                    Message responseMessage = DomainMessageUtils.toResponseMessage(session,
                            new Response(ResponseStatusEnum.ACCEPT));

                    // Actual file transfer should be done on a queue.
                    // Topics will not work
                    Destination streamTransferDestination = factoryDestination(session,
                            UUID.randomUUID().toString());
                    requestor = new DestinationRequestor(session, replyTo, streamTransferDestination,
                            jmsTemplate.getReceiveTimeout());
                    Message returnMessage = requestor.request(responseMessage);
                    requestor.close();
                    return returnMessage;
                } finally {
                    if (requestor != null)
                        requestor.close();
                }
            }

        }, true);

        // timeout
        if (returnMessage == null)
            return;
        Response response = DomainMessageUtils.fromResponseMessage(returnMessage);

        // cancel transfer
        if (!ResponseStatusEnum.STARTSEND.equals(response.getStatus()))
            return;

        final Destination receiveAckDestination = returnMessage.getJMSDestination();
        final Destination sendDataDestination = returnMessage.getJMSReplyTo();

        queueListener = new MessageQueueListener(this, receiveAckDestination);

        logger.info("Sender[" + req.getRequestId() + "]: Starting send to: " + sendDataDestination);

        byte[] buffer = new byte[getPieceSize()];
        int read = is.read(buffer);
        long placeInFile = 0;
        while (read >= 0) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            stream.write(buffer, 0, read);
            stream.close();
            final byte[] pieceData = stream.toByteArray();
            final Piece piece = new Piece(placeInFile, pieceData, getHashAlgorithm());
            logger.info("Sender[" + req.getRequestId() + "]: Sending piece with position: "
                    + piece.getPosition() + " Size of piece: " + pieceData.length);
            jmsTemplate.send(sendDataDestination, new MessageCreator() {

                @Override
                public Message createMessage(Session session) throws JMSException {
                    return DomainMessageUtils.toPieceMessage(session, piece);
                }

            });
            //                Message ret = jmsTemplate.receive(receiveAckDestination);
            Message ret = queueListener.getMessageInQueue();
            logger.info("Sender[" + req.getRequestId() + "]: Sent piece and got ack");

            // no one on the other end any longer, timeout
            if (ret == null)
                return;

            Response res = DomainMessageUtils.fromResponseMessage(ret);
            // stop transfer
            if (ResponseStatusEnum.RESEND.equals(res.getStatus())) {
                // resend piece
                logger.info("Sender[" + req.getRequestId() + "]: Resending piece");
            } else if (ResponseStatusEnum.DENY.equals(res.getStatus())) {
                return;
            } else {
                buffer = new byte[getPieceSize()];
                placeInFile += read;
                read = is.read(buffer);
            }
        }

        logger.info("Sender[" + req.getRequestId() + "]: Sending stop send");

        final DigestInputStream fiIs = is;

        jmsTemplate.send(sendDataDestination, new MessageCreator() {

            @Override
            public Message createMessage(Session session) throws JMSException {
                Response stopSendResponse = new Response(ResponseStatusEnum.STOPSEND);
                stopSendResponse.setHash(new String(fiIs.getMessageDigest().digest()));
                return DomainMessageUtils.toResponseMessage(session, stopSendResponse);
            }

        });

        Message ackMessage = queueListener.getMessageInQueue();

        Object fromMessage = DomainMessageUtils.fromMessage(ackMessage);
        if (fromMessage instanceof Response) {
            Response ackResponse = (Response) fromMessage;
            if (ResponseStatusEnum.RESEND.equals(ackResponse.getStatus())) {
                // TODO: resend the whole file
            }
        }

    } catch (Exception e) {
        throw new JmsFileTransferException(e);
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {
        }
        if (queueListener != null)
            queueListener.close();
    }
}

From source file:com.mobicage.rogerthat.plugins.messaging.BrandingMgr.java

private void extractJSEmbedding(final JSEmbeddingItemTO packet)
        throws BrandingFailureException, NoSuchAlgorithmException, FileNotFoundException, IOException {
    File brandingCache = getJSEmbeddingPacketFile(packet.name);
    if (!brandingCache.exists())
        throw new BrandingFailureException("Javascript package not found!");

    File jsRootDir = getJSEmbeddingRootDirectory();
    if (!(jsRootDir.exists() || jsRootDir.mkdir()))
        throw new BrandingFailureException("Could not create private javascript dir!");

    File jsPacketDir = getJSEmbeddingPacketDirectory(packet.name);
    if (jsPacketDir.exists() && !SystemUtils.deleteDir(jsPacketDir))
        throw new BrandingFailureException("Could not delete existing javascript dir");
    if (!jsPacketDir.mkdir())
        throw new BrandingFailureException("Could not create javascript dir");

    MessageDigest digester = MessageDigest.getInstance("SHA256");
    DigestInputStream dis = new DigestInputStream(new BufferedInputStream(new FileInputStream(brandingCache)),
            digester);/*w  w w.  ja  v a  2s . c om*/
    try {
        ZipInputStream zis = new ZipInputStream(dis);
        try {
            byte data[] = new byte[BUFFER_SIZE];
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                L.d("Extracting: " + entry);
                int count = 0;
                if (entry.isDirectory()) {
                    L.d("Skipping javascript dir " + entry.getName());
                    continue;
                }
                File destination = new File(jsPacketDir, entry.getName());
                destination.getParentFile().mkdirs();
                final OutputStream fos = new BufferedOutputStream(new FileOutputStream(destination),
                        BUFFER_SIZE);
                try {
                    while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                        fos.write(data, 0, count);
                    }
                } finally {
                    fos.close();
                }
            }
            while (dis.read(data) >= 0)
                ;
        } finally {
            zis.close();
        }
    } finally {
        dis.close();
    }
}

From source file:API.amazon.mws.feeds.service.MarketplaceWebServiceClient.java

/**
 * Calculate content MD5 header values for feeds stored on disk.
 *//* w w w .  ja  va  2 s .c o  m*/
private String computeContentMD5HeaderValue(FileInputStream fis) throws IOException, NoSuchAlgorithmException {

    DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5"));

    byte[] buffer = new byte[8192];
    while (dis.read(buffer) > 0)
        ;

    String md5Content = new String(
            org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest()));

    // Effectively resets the stream to be beginning of the file via a FileChannel.
    fis.getChannel().position(0);

    return md5Content;
}

From source file:com.mobicage.rogerthat.plugins.messaging.BrandingMgr.java

private BrandingResult extractBranding(final BrandedItem item, final File encryptedBrandingFile,
        final File tmpDecryptedBrandingFile, final File tmpBrandingDir) throws BrandingFailureException {
    try {//from  ww w. java 2s  .com
        L.i("Extracting " + tmpDecryptedBrandingFile + " (" + item.brandingKey + ")");
        File brandingFile = new File(tmpBrandingDir, "branding.html");
        File watermarkFile = null;
        Integer backgroundColor = null;
        Integer menuItemColor = null;
        ColorScheme scheme = ColorScheme.light;
        boolean showHeader = true;
        String contentType = null;
        boolean wakelockEnabled = false;
        ByteArrayOutputStream brandingBos = new ByteArrayOutputStream();
        try {
            MessageDigest digester = MessageDigest.getInstance("SHA256");
            DigestInputStream dis = new DigestInputStream(
                    new BufferedInputStream(new FileInputStream(tmpDecryptedBrandingFile)), digester);
            try {
                ZipInputStream zis = new ZipInputStream(dis);
                try {
                    byte data[] = new byte[BUFFER_SIZE];
                    ZipEntry entry;
                    while ((entry = zis.getNextEntry()) != null) {
                        L.d("Extracting: " + entry);
                        int count = 0;
                        if (entry.getName().equals("branding.html")) {
                            while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                                brandingBos.write(data, 0, count);
                            }
                        } else {
                            if (entry.isDirectory()) {
                                L.d("Skipping branding dir " + entry.getName());
                                continue;
                            }
                            File destination = new File(tmpBrandingDir, entry.getName());
                            destination.getParentFile().mkdirs();
                            if ("__watermark__".equals(entry.getName())) {
                                watermarkFile = destination;
                            }
                            final OutputStream fos = new BufferedOutputStream(new FileOutputStream(destination),
                                    BUFFER_SIZE);
                            try {
                                while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                                    fos.write(data, 0, count);
                                }
                            } finally {
                                fos.close();
                            }
                        }
                    }
                    while (dis.read(data) >= 0)
                        ;
                } finally {
                    zis.close();
                }
            } finally {
                dis.close();
            }
            String hexDigest = com.mobicage.rogerthat.util.TextUtils.toHex(digester.digest());
            if (!hexDigest.equals(item.brandingKey)) {
                encryptedBrandingFile.delete();
                SystemUtils.deleteDir(tmpBrandingDir);
                throw new BrandingFailureException("Branding cache was invalid!");
            }
            brandingBos.flush();
            byte[] brandingBytes = brandingBos.toByteArray();
            if (brandingBytes.length == 0) {
                encryptedBrandingFile.delete();
                SystemUtils.deleteDir(tmpBrandingDir);
                throw new BrandingFailureException("Invalid branding package!");
            }
            String brandingHtml = new String(brandingBytes, "UTF8");

            switch (item.type) {
            case BrandedItem.TYPE_MESSAGE:
                MessageTO message = (MessageTO) item.object;
                brandingHtml = brandingHtml.replace(NUNTIUZ_MESSAGE,
                        TextUtils.htmlEncode(message.message).replace("\r", "").replace("\n", "<br>"));

                brandingHtml = brandingHtml.replace(NUNTIUZ_TIMESTAMP,
                        TimeUtils.getDayTimeStr(mContext, message.timestamp * 1000));

                FriendsPlugin friendsPlugin = mMainService.getPlugin(FriendsPlugin.class);
                brandingHtml = brandingHtml.replace(NUNTIUZ_IDENTITY_NAME,
                        TextUtils.htmlEncode(friendsPlugin.getName(message.sender)));
                break;
            case BrandedItem.TYPE_FRIEND:
                FriendTO friend = (FriendTO) item.object;
                // In this case Friend is fully populated
                brandingHtml = brandingHtml.replace(NUNTIUZ_MESSAGE,
                        TextUtils.htmlEncode(friend.description).replace("\r", "").replace("\n", "<br>"));

                brandingHtml = brandingHtml.replace(NUNTIUZ_IDENTITY_NAME, TextUtils.htmlEncode(friend.name));

                break;
            case BrandedItem.TYPE_GENERIC:
                if (item.object instanceof FriendTO) {
                    brandingHtml = brandingHtml.replace(NUNTIUZ_IDENTITY_NAME,
                            TextUtils.htmlEncode(((FriendTO) item.object).name));
                }
                break;
            }

            Matcher matcher = RegexPatterns.BRANDING_BACKGROUND_COLOR.matcher(brandingHtml);
            if (matcher.find()) {
                String bg = matcher.group(1);
                if (bg.length() == 4) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("#");
                    sb.append(bg.charAt(1));
                    sb.append(bg.charAt(1));
                    sb.append(bg.charAt(2));
                    sb.append(bg.charAt(2));
                    sb.append(bg.charAt(3));
                    sb.append(bg.charAt(3));
                    bg = sb.toString();
                }
                backgroundColor = Color.parseColor(bg);
            }

            matcher = RegexPatterns.BRANDING_MENU_ITEM_COLOR.matcher(brandingHtml);
            if (matcher.find()) {
                String bg = matcher.group(1);
                if (bg.length() == 4) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("#");
                    sb.append(bg.charAt(1));
                    sb.append(bg.charAt(1));
                    sb.append(bg.charAt(2));
                    sb.append(bg.charAt(2));
                    sb.append(bg.charAt(3));
                    sb.append(bg.charAt(3));
                    bg = sb.toString();
                }
                menuItemColor = Color.parseColor(bg);
            }

            matcher = RegexPatterns.BRANDING_COLOR_SCHEME.matcher(brandingHtml);
            if (matcher.find()) {
                String schemeStr = matcher.group(1);
                scheme = "dark".equalsIgnoreCase(schemeStr) ? ColorScheme.dark : ColorScheme.light;
            }

            matcher = RegexPatterns.BRANDING_SHOW_HEADER.matcher(brandingHtml);
            if (matcher.find()) {
                String showHeaderStr = matcher.group(1);
                showHeader = "true".equalsIgnoreCase(showHeaderStr);
            }

            matcher = RegexPatterns.BRANDING_CONTENT_TYPE.matcher(brandingHtml);
            if (matcher.find()) {
                String contentTypeStr = matcher.group(1);
                L.i("Branding content-type: " + contentTypeStr);
                if (AttachmentViewerActivity.CONTENT_TYPE_PDF.equalsIgnoreCase(contentTypeStr)) {
                    File tmpBrandingFile = new File(tmpBrandingDir, "embed.pdf");
                    if (tmpBrandingFile.exists()) {
                        contentType = AttachmentViewerActivity.CONTENT_TYPE_PDF;
                    }
                }
            }

            Dimension dimension1 = null;
            Dimension dimension2 = null;
            matcher = RegexPatterns.BRANDING_DIMENSIONS.matcher(brandingHtml);
            if (matcher.find()) {
                String dimensionsStr = matcher.group(1);
                L.i("Branding dimensions: " + dimensionsStr);
                String[] dimensions = dimensionsStr.split(",");
                try {
                    dimension1 = new Dimension(Integer.parseInt(dimensions[0]),
                            Integer.parseInt(dimensions[1]));
                    dimension2 = new Dimension(Integer.parseInt(dimensions[2]),
                            Integer.parseInt(dimensions[3]));
                } catch (Exception e) {
                    L.bug("Invalid branding dimension: " + matcher.group(), e);
                }
            }

            matcher = RegexPatterns.BRANDING_WAKELOCK_ENABLED.matcher(brandingHtml);
            if (matcher.find()) {
                String wakelockEnabledStr = matcher.group(1);
                wakelockEnabled = "true".equalsIgnoreCase(wakelockEnabledStr);
            }

            final List<String> externalUrlPatterns = new ArrayList<String>();
            matcher = RegexPatterns.BRANDING_EXTERNAL_URLS.matcher(brandingHtml);
            while (matcher.find()) {
                externalUrlPatterns.add(matcher.group(1));
            }

            FileOutputStream fos = new FileOutputStream(brandingFile);

            try {
                fos.write(brandingHtml.getBytes("UTF8"));
            } finally {
                fos.close();
            }
            if (contentType != null
                    && AttachmentViewerActivity.CONTENT_TYPE_PDF.equalsIgnoreCase(contentType)) {
                brandingFile = new File(tmpBrandingDir, "embed.pdf");
            }
            return new BrandingResult(tmpBrandingDir, brandingFile, watermarkFile, backgroundColor,
                    menuItemColor, scheme, showHeader, dimension1, dimension2, contentType, wakelockEnabled,
                    externalUrlPatterns);
        } finally {
            brandingBos.close();
        }
    } catch (IOException e) {
        L.e(e);
        throw new BrandingFailureException("Error copying cached branded file to private space", e);
    } catch (NoSuchAlgorithmException e) {
        L.e(e);
        throw new BrandingFailureException("Cannot validate ", e);
    }
}