Example usage for org.bouncycastle.operator DigestCalculator getDigest

List of usage examples for org.bouncycastle.operator DigestCalculator getDigest

Introduction

In this page you can find the example usage for org.bouncycastle.operator DigestCalculator getDigest.

Prototype

byte[] getDigest();

Source Link

Document

Return the digest calculated on what has been written to the calculator's output stream.

Usage

From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationDownloader.java

License:Open Source License

void verifyContent(byte[] content, ConfigurationFile file) throws Exception {
    String algoId = getAlgorithmId(file.getHashAlgorithmId());
    log.trace("verifyContent({}, {})", file.getHash(), algoId);

    DigestCalculator dc = createDigestCalculator(algoId);
    dc.getOutputStream().write(content);

    byte[] hash = dc.getDigest();
    if (!Arrays.equals(hash, decodeBase64(file.getHash()))) {
        log.trace("Content {} hash {} does not match expected hash {}",
                new Object[] { file, encodeBase64(hash), file.getHash() });
        throw new CodedException(X_IO_ERROR, "Failed to verify content integrity (%s)", file);
    }/*ww w .  j a v  a  2  s. c  o  m*/
}

From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationDownloader.java

License:Open Source License

static byte[] hash(Path file, String algoId) throws Exception {
    DigestCalculator dc = createDigestCalculator(getAlgorithmId(algoId));

    try (InputStream in = Files.newInputStream(file)) {
        IOUtils.copy(in, dc.getOutputStream());
        return dc.getDigest();
    }//from   w  w  w  .jav  a 2s .c  om
}

From source file:ee.ria.xroad.common.conf.globalconf.GenerateTestData.java

License:Open Source License

static String hash(String content) throws Exception {
    DigestCalculator dc = createDigestCalculator("SHA-512");
    IOUtils.write(content, dc.getOutputStream());

    return encodeBase64(dc.getDigest());
}

From source file:ee.ria.xroad.common.conf.globalconf.GenerateTestData.java

License:Open Source License

static String hash(byte[] content) throws Exception {
    DigestCalculator dc = createDigestCalculator("SHA-512");
    IOUtils.write(content, dc.getOutputStream());

    return encodeBase64(dc.getDigest());
}

From source file:ee.ria.xroad.common.signature.BatchSignerIntegrationTest.java

License:Open Source License

private static byte[] hash(String data) throws Exception {
    DigestCalculator calc = createDigestCalculator(ALGORITHM);
    IOUtils.write(data, calc.getOutputStream());

    return calc.getDigest();
}

From source file:ee.ria.xroad.common.util.CryptoUtils.java

License:Open Source License

/**
 * Calculates message digest using the provided digest calculator.
 * @param dc the digest calculator//w ww  .j  a  v a  2  s . c  o  m
 * @param data the data
 * @return message digest
 * @throws IOException if the digest cannot be calculated
 */
public static byte[] calculateDigest(DigestCalculator dc, byte[] data) throws IOException {
    dc.getOutputStream().write(data);
    dc.getOutputStream().close();
    return dc.getDigest();
}

From source file:ee.ria.xroad.common.util.CryptoUtils.java

License:Open Source License

/**
 * Calculates message digest using the provided digest calculator.
 * @param dc the digest calculator// w w w. ja v  a  2  s. c  o  m
 * @param data the data
 * @return message digest
 * @throws IOException if the digest cannot be calculated
 */
public static byte[] calculateDigest(DigestCalculator dc, InputStream data) throws IOException {
    IOUtils.copy(data, dc.getOutputStream());
    dc.getOutputStream().close();
    return dc.getDigest();
}

From source file:ee.ria.xroad.proxy.protocol.ProxyMessageDecoder.java

License:Open Source License

private void parseAttachments(String attachmentContentType, InputStream is) throws MimeException, IOException {
    MimeConfig config = new MimeConfig.Builder().setHeadlessParsing(attachmentContentType).build();

    final MimeStreamParser attachmentParser = new MimeStreamParser(config);
    attachmentParser.setContentHandler(new AbstractContentHandler() {
        private Map<String, String> headers;
        private String partContentType;

        @Override/*from  w  w w . j  a v a2  s.c om*/
        public void startHeader() throws MimeException {
            headers = new HashMap<>();
            partContentType = null;
        }

        @Override
        public void field(Field field) throws MimeException {
            if (field.getName().toLowerCase().equals(HEADER_CONTENT_TYPE)) {
                partContentType = field.getBody();
            } else {
                headers.put(field.getName(), field.getBody());
            }
        }

        @Override
        public void startMultipart(BodyDescriptor bd) {
            attachmentParser.setFlat();
        }

        @Override
        public void body(BodyDescriptor bd, InputStream is) throws IOException {
            LOG.trace("attachment body: {}", bd.getMimeType());
            try {
                DigestCalculator dc = CryptoUtils.createDigestCalculator(getHashAlgoId());
                CountingOutputStream cos = new CountingOutputStream(dc.getOutputStream());
                TeeInputStream proxyIs = new TeeInputStream(is, cos, true);

                callback.attachment(partContentType, proxyIs, headers);

                attachmentsByteCount += cos.getByteCount();

                verifier.addPart(MessageFileNames.attachment(++attachmentNo), getHashAlgoId(), dc.getDigest());
            } catch (Exception ex) {
                throw translateException(ex);
            }
        }
    });

    attachmentParser.parse(is);
}

From source file:ee.ria.xroad.proxy.protocol.ProxyMessageEncoder.java

License:Open Source License

@Override
public void attachment(String contentType, InputStream content, Map<String, String> additionalHeaders)
        throws Exception {
    log.trace("writeAttachment({})", contentType);

    if (!inAttachmentPart) {
        mpEncoder.startNested(attachmentBoundary);
        inAttachmentPart = true;//from  w  ww  .  ja va  2 s .co m
    }

    DigestCalculator calc = createDigestCalculator(hashAlgoId);
    CountingOutputStream cos = new CountingOutputStream(calc.getOutputStream());
    TeeInputStream proxyIs = new TeeInputStream(content, cos, true);

    mpEncoder.startPart(contentType, toHeaders(additionalHeaders));
    mpEncoder.write(proxyIs);

    attachmentsByteCount += cos.getByteCount();

    signer.addPart(MessageFileNames.attachment(++attachmentNo), hashAlgoId, calc.getDigest());
}

From source file:ee.ria.xroad.proxy.testsuite.MessageTestCase.java

License:Open Source License

protected void generateQueryId() throws Exception {
    long seed = System.currentTimeMillis();
    DigestCalculator dc = CryptoUtils.createDigestCalculator(CryptoUtils.MD5_ID);
    dc.getOutputStream().write(ByteBuffer.allocate(8).putLong(seed).array());
    dc.getOutputStream().close();/*w  w w . j a  va  2 s. c o m*/
    this.queryId = CryptoUtils.encodeHex(dc.getDigest());
}