Java Code Examples for java.security.MessageDigest#clone()

The following examples show how to use java.security.MessageDigest#clone() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2objc   File: MessageDigest2Test.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * java.security.MessageDigest#clone()
 */
public void test_clone() throws Exception {
    for (Entry<Provider, List<String>> e : digestAlgs.entrySet()) {
        for (String algorithm : e.getValue()) {
            MessageDigest d1 = MessageDigest.getInstance(algorithm, e.getKey().getName());
            for (byte b = 0; b < 84; b++) {
                d1.update(b);
            }

            MessageDigest d2 = (MessageDigest) d1.clone();
            d1.update((byte) 1);
            d2.update((byte) 1);

            assertTrue("cloned hash differs from original for algorithm " + algorithm,
                       MessageDigest.isEqual(d1.digest(), d2.digest()));
        }
    }
}
 
Example 2
private MessageDigest cloneFrom(MessageDigest from) {
    try {
        return (MessageDigest)from.clone();
    } catch (CloneNotSupportedException e) { // should never occur
        throw new IllegalStateException("unexpected", e);
    }
}
 
Example 3
private MessageDigest cloneFrom(MessageDigest from) {
    try {
        return (MessageDigest)from.clone();
    } catch (CloneNotSupportedException e) { // should never occur
        throw new IllegalStateException("unexpected", e);
    }
}
 
Example 4
private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
    throws Exception {

    // copy#0: clone at state BLANK w/o any data
    MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();

    // copy#1: clone again at state BUFFERED w/ very short data
    mdObj.update(data1);
    mdCopy0.update(data1);
    MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();

    // copy#2: clone again after updating it w/ long data to trigger
    // the state into INIT
    mdObj.update(data2);
    mdCopy0.update(data2);
    mdCopy1.update(data2);
    MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();

    // copy#3: clone again after updating it w/ very short data
    mdObj.update(data1);
    mdCopy0.update(data1);
    mdCopy1.update(data1);
    mdCopy2.update(data1);
    MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();

    // copy#4: clone again after updating it w/ long data
    mdObj.update(data2);
    mdCopy0.update(data2);
    mdCopy1.update(data2);
    mdCopy2.update(data2);
    mdCopy3.update(data2);
    MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();

    // check digest equalities
    byte[] answer = mdObj.digest();
    byte[] result0 = mdCopy0.digest();
    byte[] result1 = mdCopy1.digest();
    byte[] result2 = mdCopy2.digest();
    byte[] result3 = mdCopy3.digest();
    byte[] result4 = mdCopy4.digest();


    check(answer, result0, "copy0");
    check(answer, result1, "copy1");
    check(answer, result2, "copy2");
    check(answer, result3, "copy3");
    check(answer, result4, "copy4");

    return mdCopy3;
}
 
Example 5
Source Project: aws-sdk-java-v2   File: Md5Checksum.java    License: Apache License 2.0 5 votes vote down vote up
private MessageDigest cloneFrom(MessageDigest from) {
    try {
        return (MessageDigest) from.clone();
    } catch (CloneNotSupportedException e) { // should never occur
        throw new IllegalStateException("unexpected", e);
    }
}
 
Example 6
private static boolean canBeCloned(MessageDigest digest) {
    try {
        digest.clone();
        return true;
    } catch (CloneNotSupportedException e) {
        return false;
    }
}
 
Example 7
private MessageDigest cloneFrom(MessageDigest from) {
    try {
        return (MessageDigest)from.clone();
    } catch (CloneNotSupportedException e) {
        throw new IllegalStateException("Message digest implementation does not support cloning.", e);
    }
}
 
Example 8
private static boolean supportsClone(MessageDigest digest) {
  try {
    digest.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}
 
Example 9
private static boolean supportsClone(MessageDigest digest) {
  try {
    digest.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}
 
Example 10
private static boolean supportsClone(MessageDigest digest) {
  try {
    digest.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}
 
Example 11
private static boolean supportsClone(MessageDigest digest) {
  try {
    digest.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}
 
Example 12
private static boolean supportsClone(MessageDigest digest) {
  try {
    digest.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}
 
Example 13
private static boolean supportsClone(MessageDigest digest) {
  try {
    digest.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}
 
Example 14
Source Project: bazel   File: DigestHashFunction.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean supportsClone(MessageDigest toCheck) {
  try {
    toCheck.clone();
    return true;
  } catch (CloneNotSupportedException e) {
    return false;
  }
}