Example usage for java.security DigestException DigestException

List of usage examples for java.security DigestException DigestException

Introduction

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

Prototype

public DigestException(Throwable cause) 

Source Link

Document

Creates a DigestException with the specified cause and a detail message of (cause==null ?

Usage

From source file:MD4.java

/**
 * {@inheritDoc}/*w ww.  j ava  2  s  . c o  m*/
 */
protected int engineDigest(byte[] buf, int offset, int len) throws DigestException {
    if (offset < 0 || offset + len >= buf.length) {
        throw new DigestException("Wrong offset or not enough space to store the digest");
    }
    int destLength = Math.min(len, BYTE_DIGEST_LENGTH);
    System.arraycopy(engineDigest(), 0, buf, offset, destLength);
    return destLength;
}

From source file:com.example.android.vault.EncryptedDocument.java

/**
 * Decrypt and return parsed metadata section from this document.
 *
 * @throws DigestException if metadata fails MAC check, or if
 *                         {@link Document#COLUMN_DOCUMENT_ID} recorded in metadata is
 *                         unexpected.//from  w w w .  j  a  v a2 s .c o m
 */
public JSONObject readMetadata() throws IOException, GeneralSecurityException {
    final RandomAccessFile f = new RandomAccessFile(mFile, "r");
    try {
        assertMagic(f);

        // Only interested in metadata section
        final ByteArrayOutputStream metaOut = new ByteArrayOutputStream();
        readSection(f, metaOut);

        final String rawMeta = metaOut.toString(StandardCharsets.UTF_8.name());
        if (DEBUG_METADATA) {
            Log.d(TAG, "Found metadata for " + mDocId + ": " + rawMeta);
        }

        final JSONObject meta = new JSONObject(rawMeta);

        // Validate that metadata belongs to requested file
        if (meta.getLong(Document.COLUMN_DOCUMENT_ID) != mDocId) {
            throw new DigestException("Unexpected document ID");
        }

        return meta;

    } catch (JSONException e) {
        throw new IOException(e);
    } finally {
        f.close();
    }
}

From source file:at.gv.egiz.bku.local.stal.LocalSecureViewer.java

private List<HashDataInput> verifyHashDataInput(List<ReferenceType> signedReferences,
        HashDataInput hashDataInput) throws DigestException, NoSuchAlgorithmException, Exception {

    ArrayList<HashDataInput> verifiedHashDataInputs = new ArrayList<HashDataInput>();

    for (ReferenceType signedRef : signedReferences) {
        if (signedRef.getType() == null) {
            log.info("Verifying digest for signed reference {}.", signedRef.getId());

            String signedRefId = signedRef.getId();
            byte[] signedDigest = signedRef.getDigestValue();
            String signedDigestAlg = null;
            if (signedRef.getDigestMethod() != null) {
                signedDigestAlg = signedRef.getDigestMethod().getAlgorithm();
            } else {
                throw new NoSuchAlgorithmException(
                        "Failed to verify digest value for reference " + signedRefId + ": no digest algorithm");
            }/*w w w . j a  va2  s  .co  m*/

            if (hashDataInput == null) {
                throw new Exception("No hashdata input for reference " + signedRefId + " returned by service");
            }

            byte[] hdi = null;

            try {
                hdi = IOUtils.toByteArray(hashDataInput.getHashDataInput());
            } catch (IOException e) {
                throw new Exception("No hashdata input for reference " + signedRefId + " provided by service.",
                        e);
            }

            String mimeType = hashDataInput.getMimeType();
            String encoding = hashDataInput.getEncoding();
            String filename = hashDataInput.getFilename();

            if (log.isDebugEnabled()) {
                log.debug("Digesting reference " + signedRefId + " (" + mimeType + ";" + encoding + ")");
            }

            byte[] hashDataInputDigest;
            if ((signedRef.getURI() != null) && signedRef.getURI().startsWith("CMSExcludedByteRange:")) {
                String range = signedRef.getURI().substring(21);
                int sep = range.indexOf('-');
                int from = Integer.parseInt(range.substring(0, sep));
                int to = Integer.parseInt(range.substring(sep + 1));

                Arrays.fill(hdi, from, to + 1, (byte) 0);

                byte[] hashData = new byte[hdi.length - ((to + 1) - from)];
                if (from > 0)
                    System.arraycopy(hdi, 0, hashData, 0, from);
                if ((to + 1) < hdi.length)
                    System.arraycopy(hdi, to + 1, hashData, from, hdi.length - (to + 1));
                hashDataInputDigest = digest(hashData, signedDigestAlg);
            } else {
                hashDataInputDigest = digest(hdi, signedDigestAlg);
            }

            log.debug("Comparing digest to claimed digest value for reference {}.", signedRefId);
            if (!Arrays.equals(hashDataInputDigest, signedDigest)) {
                log.error("Bad digest value for reference {}.", signedRefId);
                throw new DigestException("Bad digest value for reference " + signedRefId);
            }

            verifiedHashDataInputs
                    .add(new ByteArrayHashDataInput(hdi, signedRefId, mimeType, encoding, filename));
        }
    }

    return verifiedHashDataInputs;
}

From source file:io.barracks.ota.client.PackageDownloadService.java

/**
 * This method checks the package's <code>file</code> integrity.<br>
 * It uses the md5 provided in the <code>details</code> parameter.
 *
 * @param details The {@link UpdateDetails} retrieved from the Barracks platform.
 * @param file    The file which was downloaded.
 * @throws IOException              If an exception is raised while accessing the file.
 * @throws GeneralSecurityException If the hash verification fails.
 *///from   ww w .j a  v a  2 s.c o m
protected void checkPackageIntegrity(UpdateDetails details, File file)
        throws IOException, GeneralSecurityException {
    InputStream is = null;
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
        is = new FileInputStream(file);
        is = new DigestInputStream(is, md);
        byte[] buffer = new byte[8192];
        while (is.read(buffer) != -1) {
        }
    } catch (NoSuchAlgorithmException | IOException e) {
        throw e;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    if (md != null) {
        byte[] digest = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }
        if (!sb.toString().equals(details.getPackageInfo().getMd5())) {
            throw new DigestException(
                    "Wrong file signature " + sb.toString() + " - " + details.getPackageInfo().getMd5());
        }
    }
}