001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.Digest;
004: import org.bouncycastle.util.encoders.Hex;
005: import org.bouncycastle.util.test.SimpleTest;
006:
007: public abstract class DigestTest extends SimpleTest {
008: private Digest digest;
009: private String[] input;
010: private String[] results;
011:
012: DigestTest(Digest digest, String[] input, String[] results) {
013: this .digest = digest;
014: this .input = input;
015: this .results = results;
016: }
017:
018: public String getName() {
019: return digest.getAlgorithmName();
020: }
021:
022: public void performTest() {
023: byte[] resBuf = new byte[digest.getDigestSize()];
024:
025: for (int i = 0; i < input.length - 1; i++) {
026: byte[] m = toByteArray(input[i]);
027:
028: vectorTest(digest, i, resBuf, m, Hex.decode(results[i]));
029: }
030:
031: byte[] lastV = toByteArray(input[input.length - 1]);
032: byte[] lastDigest = Hex.decode(results[input.length - 1]);
033:
034: vectorTest(digest, input.length - 1, resBuf, lastV, Hex
035: .decode(results[input.length - 1]));
036:
037: //
038: // clone test
039: //
040: digest.update(lastV, 0, lastV.length / 2);
041:
042: // clone the Digest
043: Digest d = cloneDigest(digest);
044:
045: digest.update(lastV, lastV.length / 2, lastV.length
046: - lastV.length / 2);
047: digest.doFinal(resBuf, 0);
048:
049: if (!areEqual(lastDigest, resBuf)) {
050: fail("failing clone vector test",
051: results[results.length - 1], new String(Hex
052: .encode(resBuf)));
053: }
054:
055: d.update(lastV, lastV.length / 2, lastV.length - lastV.length
056: / 2);
057: d.doFinal(resBuf, 0);
058:
059: if (!areEqual(lastDigest, resBuf)) {
060: fail("failing second clone vector test",
061: results[results.length - 1], new String(Hex
062: .encode(resBuf)));
063: }
064: }
065:
066: private byte[] toByteArray(String input) {
067: byte[] bytes = new byte[input.length()];
068:
069: for (int i = 0; i != bytes.length; i++) {
070: bytes[i] = (byte) input.charAt(i);
071: }
072:
073: return bytes;
074: }
075:
076: private void vectorTest(Digest digest, int count, byte[] resBuf,
077: byte[] input, byte[] expected) {
078: digest.update(input, 0, input.length);
079: digest.doFinal(resBuf, 0);
080:
081: if (!areEqual(resBuf, expected)) {
082: System.out.println(new String(Hex.decode(input)));
083: fail("Vector " + count + " failed got "
084: + new String(Hex.encode(resBuf)));
085: }
086: }
087:
088: protected abstract Digest cloneDigest(Digest digest);
089:
090: //
091: // optional tests
092: //
093: protected void millionATest(String expected) {
094: byte[] resBuf = new byte[digest.getDigestSize()];
095:
096: for (int i = 0; i < 1000000; i++) {
097: digest.update((byte) 'a');
098: }
099:
100: digest.doFinal(resBuf, 0);
101:
102: if (!areEqual(resBuf, Hex.decode(expected))) {
103: fail("Million a's failed");
104: }
105: }
106:
107: protected void sixtyFourKTest(String expected) {
108: byte[] resBuf = new byte[digest.getDigestSize()];
109:
110: for (int i = 0; i < 65536; i++) {
111: digest.update((byte) (i & 0xff));
112: }
113:
114: digest.doFinal(resBuf, 0);
115:
116: if (!areEqual(resBuf, Hex.decode(expected))) {
117: fail("64k test failed");
118: }
119: }
120: }
|