001: package org.bouncycastle.jce.provider.test;
002:
003: import java.security.MessageDigest;
004: import java.security.Security;
005:
006: import org.bouncycastle.jce.provider.BouncyCastleProvider;
007: import org.bouncycastle.util.encoders.Hex;
008: import org.bouncycastle.util.test.SimpleTest;
009:
010: public class DigestTest extends SimpleTest {
011: final static String provider = "BC";
012:
013: static private String[][] abcVectors = {
014: { "MD2", "da853b0d3f88d99b30283a69e6ded6bb" },
015: { "MD4", "a448017aaf21d8525fc10ae87aa6729d" },
016: { "MD5", "900150983cd24fb0d6963f7d28e17f72" },
017: { "SHA-1", "a9993e364706816aba3e25717850c26c9cd0d89d" },
018: { "SHA-224",
019: "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" },
020: { "SHA-256",
021: "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
022: {
023: "SHA-384",
024: "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7" },
025: {
026: "SHA-512",
027: "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" },
028: { "RIPEMD128", "c14a12199c66e4ba84636b0f69144c77" },
029: { "RIPEMD160", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc" },
030: { "RIPEMD256",
031: "afbd6e228b9d8cbbcef5ca2d03e6dba10ac0bc7dcbe4680e1e42d2e975459b65" },
032: {
033: "RIPEMD320",
034: "de4c01b3054f8930a79d09ae738e92301e5a17085beffdc1b8d116713e74f82fa942d64cdbc4682d" },
035: { "Tiger",
036: "2AAB1484E8C158F2BFB8C5FF41B57A525129131C957B5F93" },
037: { "GOST3411",
038: "b285056dbf18d7392d7677369524dd14747459ed8143997e163b2986f92fd42c" },
039: {
040: "WHIRLPOOL",
041: "4E2448A4C6F486BB16B6562C73B4020BF3043E3A731BCE721AE1B303D97E6D4C7181EEBDB6C57E277D0E34957114CBD6C797FC9D95D8B582D225292076D4EEF5" } };
042:
043: public String getName() {
044: return "Digest";
045: }
046:
047: void test(String algorithm) throws Exception {
048: byte[] message = "hello world".getBytes();
049:
050: MessageDigest digest = MessageDigest.getInstance(algorithm,
051: provider);
052:
053: byte[] result = digest.digest(message);
054: byte[] result2 = digest.digest(message);
055:
056: // test one digest the same message with the same instance
057: if (!MessageDigest.isEqual(result, result2)) {
058: fail("Result object 1 not equal");
059: }
060:
061: // test two, single byte updates
062: for (int i = 0; i < message.length; i++) {
063: digest.update(message[i]);
064: }
065: result2 = digest.digest();
066:
067: if (!MessageDigest.isEqual(result, result2)) {
068: fail("Result object 2 not equal");
069: }
070:
071: // test three, two half updates
072: digest.update(message, 0, message.length / 2);
073: digest.update(message, message.length / 2, message.length
074: - message.length / 2);
075: result2 = digest.digest();
076:
077: if (!MessageDigest.isEqual(result, result2)) {
078: fail("Result object 3 not equal");
079: }
080:
081: // test four, clone test
082: digest.update(message, 0, message.length / 2);
083: MessageDigest d = (MessageDigest) digest.clone();
084: digest.update(message, message.length / 2, message.length
085: - message.length / 2);
086: result2 = digest.digest();
087:
088: if (!MessageDigest.isEqual(result, result2)) {
089: fail("Result object 4(a) not equal");
090: }
091:
092: d.update(message, message.length / 2, message.length
093: - message.length / 2);
094: result2 = d.digest();
095:
096: if (!MessageDigest.isEqual(result, result2)) {
097: fail("Result object 4(b) not equal");
098: }
099:
100: // test five, check reset() method
101: digest.update(message, 0, message.length / 2);
102: digest.reset();
103: digest.update(message, 0, message.length / 2);
104: digest.update(message, message.length / 2, message.length
105: - message.length / 2);
106: result2 = digest.digest();
107:
108: if (!MessageDigest.isEqual(result, result2)) {
109: fail("Result object 5 not equal");
110: }
111: }
112:
113: /**
114: * Test the hash against a standard value for the string "abc"
115: *
116: * @param algorithm algorithm to test
117: * @param hash expected value
118: * @return the test result.
119: */
120: void abcTest(String algorithm, String hash) throws Exception {
121: byte[] abc = { (byte) 0x61, (byte) 0x62, (byte) 0x63 };
122:
123: MessageDigest digest = MessageDigest.getInstance(algorithm,
124: provider);
125:
126: byte[] result = digest.digest(abc);
127:
128: if (!MessageDigest.isEqual(result, Hex.decode(hash))) {
129: fail("abc result not equal for " + algorithm);
130: }
131: }
132:
133: public void performTest() throws Exception {
134: for (int i = 0; i != abcVectors.length; i++) {
135: test(abcVectors[i][0]);
136:
137: abcTest(abcVectors[i][0], abcVectors[i][1]);
138: }
139: }
140:
141: public static void main(String[] args) {
142: Security.addProvider(new BouncyCastleProvider());
143:
144: runTest(new DigestTest());
145: }
146: }
|