Example usage for java.security MessageDigest isEqual

List of usage examples for java.security MessageDigest isEqual

Introduction

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

Prototype

public static boolean isEqual(byte[] digesta, byte[] digestb) 

Source Link

Document

Compares two digests for equality.

Usage

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureRS512(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        IllegalBlockSizeException, BadPaddingException, IOException {
    PublicKey publicKey = cert.getPublicKey();

    Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    byte[] decSig = cipher.doFinal(sigBytes);
    ASN1InputStream aIn = new ASN1InputStream(decSig);
    try {//from  w  w  w  .  j  av  a 2  s .co  m
        ASN1Sequence seq = (ASN1Sequence) aIn.readObject();

        MessageDigest hash = MessageDigest.getInstance("SHA-512", "BC");
        hash.update(signingInput);

        ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1);
        return MessageDigest.isEqual(hash.digest(), sigHash.getOctets());
    } finally {
        IOUtils.closeQuietly(aIn);
    }
}

From source file:org.nuxeo.ecm.automation.server.test.AbstractAutomationClientTest.java

@Test
public void testUploadSmallFile() throws Exception {
    DigestMockInputStream source = new DigestMockInputStream(100);
    FileInputStream in = new UploadFileSupport(session, automationTestFolder.getPath()).testUploadFile(source);
    byte[] sentSum = source.digest.digest();
    while (in.available() > 0) {
        source.digest.update((byte) in.read());
    }/*from w ww.  j a  v a2 s  . c o  m*/
    byte[] receivedSum = source.digest.digest();
    assertTrue("Expected (sent) bytes array: " + Arrays.toString(sentSum) + " - Actual (received) bytes array: "
            + Arrays.toString(receivedSum), MessageDigest.isEqual(sentSum, receivedSum));
}

From source file:org.lockss.hasher.BlockHasher.java

private void endVersionLocalHash() {
    if (currentVersionLocalHasher != null && currentVersionLocalHasher.isValid()) {
        // Local hash is enabled for this version
        byte[] hashOfContent = currentVersionLocalHasher.getDigest().digest();
        if (currentVersionStoredHash != null) {
            // This vesion has a stored hash in the properties
            if (isTrace) {
                log.debug3("Computed: " + ByteArray.toHexString(hashOfContent) + " Stored: "
                        + ByteArray.toHexString(currentVersionStoredHash));
            }//from w w  w . j  a v a2s .com
            if (MessageDigest.isEqual(hashOfContent, currentVersionStoredHash)) {
                // hashes match - all is well
                match(curVer);
            } else {
                // Something bad happened to either the content or the hash
                mismatch(curVer, localHashAlgorithm, hashOfContent, currentVersionStoredHash);
            }
        } else {
            // No checksum property - create one
            missing(curVer, localHashAlgorithm, hashOfContent);
        }
        currentVersionLocalHasher = null;
        currentVersionStoredHash = null;
    }
}

From source file:org.xwoot.wikiContentManager.XWikiSwizzleClient.XwikiSwizzleClient.java

/**
 * DOCUMENT ME!//w w  w.  j a  va 2 s .c  om
 * 
 * @param pageId DOCUMENT ME!
 * @param value DOCUMENT ME!
 * @param algo DOCUMENT ME!
 * @param rmd DOCUMENT ME!
 * @return DOCUMENT ME!
 * @throws NoSuchAlgorithmException
 * @throws XWikiSwizzleClientException
 */
synchronized public String setPageContent(String pageId, String value, String algo, byte[] rmd)
        throws NoSuchAlgorithmException, XWikiSwizzleClientException {
    String result = null;
    Page page = null;
    String pageContent = "";

    // if user have not connected client, method do it for him
    // else it's to the user to do the connection gestion...
    boolean b = this.relogin();

    page = this.getWikiPage(pageId);

    if (page != null) {
        pageContent = page.getContent();
    }

    byte[] messageDigest = this.getDigest(pageContent, algo);

    if (MessageDigest.isEqual(messageDigest, rmd)) {
        if (page == null) {
            Map p = this.createPage(pageId, value);
            if (p == null) {
                throw new XWikiSwizzleClientException("Problem with setPageContent : can't create the page");
            }
        } else {
            page.setContent(value);
            this.storeWikiPage(page);
        }
    } else {
        if ((pageContent == null)
                || ((pageContent.length() == 1) && (pageContent.codePointAt(0) == VOID_CHARACTER))
                || (pageContent.length() < 1)) {
            result = "";
        } else {
            result = pageContent;
        }
    }

    this.logout(b);

    return result;
}

From source file:misc.FileHandler.java

/**
 * Receives data from the given input stream and writes it to the given
 * file. This method reads data from the input stream until the given number
 * of bytes was read or the input stream is closed. Note that this function
 * does not close the given input stream. The output file is locked.
 * //from   w w w . j  a  v  a 2  s  .c  o  m
 * @param in
 *            the input stream with the data to write to the file.
 * @param data
 *            protected data of the file to receive.
 * @param store
 *            the complete path where the decrypted file is supposed to be
 *            stored.
 * @return <code>true</code>, if the file was successfully received and
 *         stored. <code>false</code>, otherwise.
 */
public static boolean receiveFile(InputStream in, ProtectedData data, Path store) {
    if (in == null) {
        throw new NullPointerException("in may not be null!");
    }
    if (data == null) {
        throw new NullPointerException("data may not be null!");
    }
    if (store == null) {
        throw new NullPointerException("file may not be null!");
    }

    MessageDigest digest = null;

    try {
        digest = MessageDigest.getInstance(MESSAGE_DIGEST);

        try (FileOutputStream out = new FileOutputStream(store.toFile(), false);) {
            FileLock lock = out.getChannel().tryLock(0, Long.MAX_VALUE, false);

            if (lock != null) {
                byte[] buffer = new byte[BUFFER_SIZE];
                long count = 0;
                int read;

                while ((count < data.getSize()) && ((read = in.read(buffer, 0,
                        Math.min(buffer.length, (int) (data.getSize() - count)))) != -1)) {
                    out.write(buffer, 0, read);
                    digest.update(buffer, 0, read);
                    count += read;
                }
                out.flush();
            }
        } catch (IOException | OverlappingFileLockException e) {
            Logger.logError(e);
        }
    } catch (NoSuchAlgorithmException e) {
        Logger.logError(e);
    }

    // check hash
    return (digest != null) && MessageDigest.isEqual(data.getHash(), digest.digest());
}

From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java

public boolean digestEquals(RequestPacket req, MessageDigest md) {
    byte[] d1 = this.getDigest(md);
    byte[] d2 = req.getDigest(md);
    return (MessageDigest.isEqual(d1, d2));
}