001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.unit;
007:
008: import org.h2.security.AES;
009: import org.h2.security.SHA256;
010: import org.h2.security.XTEA;
011: import org.h2.test.TestBase;
012: import org.h2.util.ByteUtils;
013:
014: /**
015: * Tests various security primitives.
016: */
017: public class TestSecurity extends TestBase {
018:
019: public void test() throws Exception {
020: testSHA();
021: testAES();
022: testXTEA();
023: }
024:
025: public void testSHA() throws Exception {
026: SHA256 sha = new SHA256();
027: testOneSHA(sha);
028: }
029:
030: private String getHashString(SHA256 sha, byte[] data) {
031: byte[] result = sha.getHash(data);
032: return ByteUtils.convertBytesToString(result);
033: }
034:
035: private void testOneSHA(SHA256 sha) throws Exception {
036: if (!getHashString(sha, new byte[] {})
037: .equals(
038: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")) {
039: throw new Exception("x");
040: }
041: if (!getHashString(sha, new byte[] { 0x19 })
042: .equals(
043: "68aa2e2ee5dff96e3355e6c7ee373e3d6a4e17f75f9518d843709c0c9bc3e3d4")) {
044: throw new Exception("x");
045: }
046: if (!getHashString(
047: sha,
048: new byte[] { (byte) 0xe3, (byte) 0xd7, 0x25, 0x70,
049: (byte) 0xdc, (byte) 0xdd, 0x78, 0x7c,
050: (byte) 0xe3, (byte) 0x88, 0x7a, (byte) 0xb2,
051: (byte) 0xcd, 0x68, 0x46, 0x52 })
052: .equals(
053: "175ee69b02ba9b58e2b0a5fd13819cea573f3940a94f825128cf4209beabb4e8")) {
054: throw new Exception("x");
055: }
056: checkSHA256("",
057: "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855");
058: checkSHA256("a",
059: "CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB");
060: checkSHA256("abc",
061: "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD");
062: checkSHA256("message digest",
063: "F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650");
064: checkSHA256("abcdefghijklmnopqrstuvwxyz",
065: "71C480DF93D6AE2F1EFAD1447C66C9525E316218CF51FC8D9ED832F2DAF18B73");
066: checkSHA256(
067: "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
068: "248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1");
069: checkSHA256(
070: "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
071: "F371BC4A311F2B009EEF952DD83CA80E2B60026C8E935592D0F9C308453C813E");
072: StringBuffer buff = new StringBuffer(1000000);
073: buff.append('a');
074: checkSHA256(buff.toString(),
075: "CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB");
076: }
077:
078: void checkSHA256(String message, String expected) throws Exception {
079: SHA256 sha = new SHA256();
080: String hash = ByteUtils.convertBytesToString(
081: sha.getHash(message.getBytes())).toUpperCase();
082: check(expected, hash);
083: }
084:
085: public void testXTEA() throws Exception {
086: byte[] test = new byte[4096];
087: XTEA xtea = new XTEA();
088: xtea.setKey("abcdefghijklmnop".getBytes());
089: for (int i = 0; i < 10; i++) {
090: xtea.decryptBlock(test, test, 0);
091: }
092: }
093:
094: private void testAES() throws Exception {
095: AES test = new AES();
096: test
097: .setKey(ByteUtils
098: .convertStringToBytes("000102030405060708090A0B0C0D0E0F"));
099:
100: byte[] in = new byte[128];
101: byte[] enc = new byte[128];
102: test.encrypt(enc, 0, 128);
103: test.decrypt(enc, 0, 128);
104: if (ByteUtils.compareNotNull(in, enc) != 0) {
105: throw new Error("hey!");
106: }
107:
108: for (int i = 0; i < 10; i++) {
109: test.encrypt(in, 0, 128);
110: test.decrypt(enc, 0, 128);
111: }
112: }
113:
114: }
|