summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorBrian Carlstrom <bdc@google.com>2011-06-01 12:33:45 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2011-06-01 12:33:45 -0700
commit8f1bbe0b6549136be386d75edf70ed4daf8a25b0 (patch)
tree62a371eb89ad8d55a2a1b1f72347775f690dc23b /luni
parent555c4728b8fdfd96590c218491f15b88712704dc (diff)
parentcbec51f5f2b9c425ce551405518e48d4046adb35 (diff)
downloadlibcore-8f1bbe0b6549136be386d75edf70ed4daf8a25b0.zip
libcore-8f1bbe0b6549136be386d75edf70ed4daf8a25b0.tar.gz
libcore-8f1bbe0b6549136be386d75edf70ed4daf8a25b0.tar.bz2
am cbec51f5: SHA1_MessageDigestImpl should work for inputs of 256 Megabytes
* commit 'cbec51f5f2b9c425ce551405518e48d4046adb35': SHA1_MessageDigestImpl should work for inputs of 256 Megabytes
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java2
-rw-r--r--luni/src/test/java/libcore/java/security/MessageDigestTest.java195
2 files changed, 196 insertions, 1 deletions
diff --git a/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java b/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java
index fe8a69e..71223b7 100644
--- a/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java
+++ b/luni/src/main/java/org/apache/harmony/security/provider/crypto/SHA1_MessageDigestImpl.java
@@ -42,7 +42,7 @@ public class SHA1_MessageDigestImpl extends MessageDigestSpi implements Cloneabl
private byte[] oneByte; // one byte buffer needed to use in engineUpdate(byte)
// having buffer as private field is just optimization
- private int messageLength; // total length of bytes supplied by user
+ private long messageLength; // total length of bytes supplied by user
/**
diff --git a/luni/src/test/java/libcore/java/security/MessageDigestTest.java b/luni/src/test/java/libcore/java/security/MessageDigestTest.java
index 0e55428..cd7b8db 100644
--- a/luni/src/test/java/libcore/java/security/MessageDigestTest.java
+++ b/luni/src/test/java/libcore/java/security/MessageDigestTest.java
@@ -18,7 +18,12 @@ package libcore.java.security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
+import java.security.Security;
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
import junit.framework.TestCase;
public final class MessageDigestTest extends TestCase {
@@ -35,4 +40,194 @@ public final class MessageDigestTest extends TestCase {
sha.update(new byte[] { 4, 5, 6 });
assertEquals(Arrays.toString(sha_456), Arrays.toString(sha.digest()));
}
+
+ public void test_getInstance() throws Exception {
+ Provider[] providers = Security.getProviders();
+ for (Provider provider : providers) {
+ Set<Provider.Service> services = provider.getServices();
+ for (Provider.Service service : services) {
+ String type = service.getType();
+ if (!type.equals("MessageDigest")) {
+ continue;
+ }
+ String algorithm = service.getAlgorithm();
+ try {
+ // MessageDigest.getInstance(String)
+ MessageDigest md1 = MessageDigest.getInstance(algorithm);
+ assertEquals(algorithm, md1.getAlgorithm());
+ test_MessageDigest(md1);
+
+ // MessageDigest.getInstance(String, Provider)
+ MessageDigest md2 = MessageDigest.getInstance(algorithm, provider);
+ assertEquals(algorithm, md2.getAlgorithm());
+ assertEquals(provider, md2.getProvider());
+ test_MessageDigest(md2);
+
+ // MessageDigest.getInstance(String, String)
+ MessageDigest md3 = MessageDigest.getInstance(algorithm, provider.getName());
+ assertEquals(algorithm, md3.getAlgorithm());
+ assertEquals(provider, md3.getProvider());
+ test_MessageDigest(md3);
+ } catch (Exception e) {
+ throw new Exception("Problem testing MessageDigest." + algorithm, e);
+ }
+ }
+ }
+ }
+
+ private static final Map<String, Map<String, byte[]>> EXPECTATIONS
+ = new HashMap<String, Map<String, byte[]>>();
+ private static void putExpectation(String algorithm, String inputName, byte[] expected) {
+ algorithm = algorithm.toUpperCase();
+ Map<String, byte[]> expectations = EXPECTATIONS.get(algorithm);
+ if (expectations == null) {
+ expectations = new HashMap<String, byte[]>();
+ EXPECTATIONS.put(algorithm, expectations);
+ }
+ expectations.put(inputName, expected);
+ }
+ private static Map<String, byte[]> getExpectations(String algorithm) throws Exception {
+ algorithm = algorithm.toUpperCase();
+ Map<String, byte[]> expectations = EXPECTATIONS.get(algorithm);
+ if (expectations == null) {
+ throw new Exception("No expectations for MessageDigest." + algorithm);
+ }
+ return expectations;
+ }
+ private static final String INPUT_EMPTY = "empty";
+ private static final String INPUT_256MB = "256mb";
+ static {
+ // INPUT_EMPTY
+ putExpectation("MD2",
+ INPUT_EMPTY,
+ new byte[] { -125, 80, -27, -93, -30, 76, 21, 61,
+ -14, 39, 92, -97, -128, 105, 39, 115 });
+ putExpectation("MD5",
+ INPUT_EMPTY,
+ new byte[] { -44, 29, -116, -39, -113, 0, -78, 4,
+ -23, -128, 9, -104, -20, -8, 66, 126 });
+ putExpectation("SHA",
+ INPUT_EMPTY,
+ new byte[] { -38, 57, -93, -18, 94, 107, 75, 13,
+ 50, 85, -65, -17, -107, 96, 24, -112,
+ -81, -40, 7, 9});
+ putExpectation("SHA-1",
+ INPUT_EMPTY,
+ new byte[] { -38, 57, -93, -18, 94, 107, 75, 13,
+ 50, 85, -65, -17, -107, 96, 24, -112,
+ -81, -40, 7, 9});
+ putExpectation("SHA-256",
+ INPUT_EMPTY,
+ new byte[] { -29, -80, -60, 66, -104, -4, 28, 20,
+ -102, -5, -12, -56, -103, 111, -71, 36,
+ 39, -82, 65, -28, 100, -101, -109, 76,
+ -92, -107, -103, 27, 120, 82, -72, 85 });
+ putExpectation("SHA-384",
+ INPUT_EMPTY,
+ new byte[] { 56, -80, 96, -89, 81, -84, -106, 56,
+ 76, -39, 50, 126, -79, -79, -29, 106,
+ 33, -3, -73, 17, 20, -66, 7, 67,
+ 76, 12, -57, -65, 99, -10, -31, -38,
+ 39, 78, -34, -65, -25, 111, 101, -5,
+ -43, 26, -46, -15, 72, -104, -71, 91 });
+ putExpectation("SHA-512",
+ INPUT_EMPTY,
+ new byte[] { -49, -125, -31, 53, 126, -17, -72, -67,
+ -15, 84, 40, 80, -42, 109, -128, 7,
+ -42, 32, -28, 5, 11, 87, 21, -36,
+ -125, -12, -87, 33, -45, 108, -23, -50,
+ 71, -48, -47, 60, 93, -123, -14, -80,
+ -1, -125, 24, -46, -121, 126, -20, 47,
+ 99, -71, 49, -67, 71, 65, 122, -127,
+ -91, 56, 50, 122, -7, 39, -38, 62 });
+
+ // Regression test input for problem SHA-1 with input of
+ // 256MBs. However, its currently too slow to be practical on
+ // devices, so its disabled in that case. http://b/4501620
+ boolean enabled256mb = (System.getProperty("os.arch").contains("x86") // host
+ || System.getProperty("os.arch").contains("amd64")); // RI
+ if (enabled256mb) {
+ // INPUT_256MB
+ putExpectation("MD2",
+ INPUT_256MB,
+ new byte[] { -63, -120, 6, 67, 12, -87, -39, -11,
+ -67, -3, -31, -41, -91, 16, -35, 91 });
+ putExpectation("MD5",
+ INPUT_256MB,
+ new byte[] { 31, 80, 57, -27, 11, -42, 107, 41,
+ 12, 86, 104, 77, -123, 80, -58, -62 });
+ putExpectation("SHA",
+ INPUT_256MB,
+ new byte[] { 123, -111, -37, -36, 86, -59, 120, 30,
+ -33, 108, -120, 71, -76, -86, 105, 101,
+ 86, 108, 92, 117 });
+ putExpectation("SHA-1",
+ INPUT_256MB,
+ new byte[] { 123, -111, -37, -36, 86, -59, 120, 30,
+ -33, 108, -120, 71, -76, -86, 105, 101,
+ 86, 108, 92, 117 });
+ putExpectation("SHA-256",
+ INPUT_256MB,
+ new byte[] { -90, -41, 42, -57, 105, 15, 83, -66,
+ 106, -28, 107, -88, -123, 6, -67, -105,
+ 48, 42, 9, 63, 113, 8, 71, 43,
+ -39, -17, -61, -50, -3, -96, 100, -124 });
+ putExpectation("SHA-384",
+ INPUT_256MB,
+ new byte[] { 71, 72, 77, -83, -110, 22, -118, -18,
+ -58, 119, 115, 74, -67, -36, 84, 122,
+ -105, -67, -75, 15, -33, 37, 78, -95,
+ 4, 118, -53, 106, 65, -115, -19, 121,
+ -59, -94, -45, -111, -124, 35, 35, 60,
+ 67, -34, 62, 106, -16, 122, -110, -14 });
+ putExpectation("SHA-512",
+ INPUT_256MB,
+ new byte[] { 36, 7, -120, 39, -87, -87, 84, -40,
+ -66, 114, 62, -73, 107, 101, -117, -12,
+ -124, 20, 109, 103, -92, 125, 111, 102,
+ 12, 114, -68, 100, 30, 25, -88, 62,
+ 108, 56, 9, -107, 89, -25, -50, 118,
+ -87, 100, 13, 37, -14, 66, -40, -97,
+ 105, -27, 79, -62, 53, -31, 83, 40,
+ 4, 57, 90, -81, 63, -77, -42, 113 });
+ }
+ }
+
+ private void test_MessageDigest(MessageDigest md) throws Exception {
+ String algorithm = md.getAlgorithm();
+ for (Map.Entry<String, byte[]> expectation : getExpectations(algorithm).entrySet()) {
+ String inputName = expectation.getKey();
+ byte[] expected = expectation.getValue();
+ byte[] actual;
+ if (inputName.equals(INPUT_EMPTY)) {
+ actual = md.digest();
+ } else if (inputName.equals(INPUT_256MB)) {
+ byte[] mb = new byte[1 * 1024 * 1024];
+ for (int i = 0; i < 256; i++) {
+ md.update(mb);
+ }
+ actual = md.digest();
+ } else {
+ throw new AssertionError(inputName);
+ }
+ assertDigest(algorithm, expected, actual);
+ assertEquals(algorithm, expected.length, md.getDigestLength());
+ }
+ }
+
+ private void assertDigest(String algorithm, byte[] actual, byte[] expected) {
+ assertEquals(algorithm, javaBytes(actual), javaBytes(expected));
+ }
+
+ private String javaBytes(byte[] bytes) {
+ StringBuffer buf = new StringBuffer();
+ buf.append("new byte[] { ");
+ for (byte b : bytes) {
+ buf.append(b);
+ buf.append(", ");
+ }
+ buf.append(" }");
+ return buf.toString();
+ }
+
}