KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > unit > TestSecurity


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.unit;
6
7 import org.h2.security.AES;
8 import org.h2.security.SHA256;
9 import org.h2.security.XTEA;
10 import org.h2.test.TestBase;
11 import org.h2.util.ByteUtils;
12
13
14 /**
15  * @author Thomas
16  */

17
18 public class TestSecurity extends TestBase {
19     
20     public void test() throws Exception JavaDoc {
21         testSHA();
22         testAES();
23         testXTEA();
24     }
25         
26     public void testSHA() throws Exception JavaDoc {
27         SHA256 sha = new SHA256();
28         testOneSHA(sha);
29     }
30
31     private String JavaDoc getHashString(SHA256 sha, byte[] data) {
32         byte[] result = sha.getHash(data);
33         return ByteUtils.convertBytesToString(result);
34     }
35
36     private void testOneSHA(SHA256 sha) throws Exception JavaDoc {
37         if (!getHashString(sha, new byte[] { })
38                 .equals(
39                         "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")) {
40             throw new Exception JavaDoc("x");
41         }
42         if (!getHashString(sha, new byte[] { 0x19 })
43                 .equals(
44                         "68aa2e2ee5dff96e3355e6c7ee373e3d6a4e17f75f9518d843709c0c9bc3e3d4")) {
45             throw new Exception JavaDoc("x");
46         }
47         if (!getHashString(sha, new byte[] { (byte)0xe3, (byte)0xd7, 0x25, 0x70, (byte)0xdc, (byte)0xdd, 0x78, 0x7c, (byte)0xe3, (byte)0x88, 0x7a, (byte)0xb2, (byte)0xcd, 0x68, 0x46, 0x52 })
48                 .equals(
49                         "175ee69b02ba9b58e2b0a5fd13819cea573f3940a94f825128cf4209beabb4e8")) {
50             throw new Exception JavaDoc("x");
51         }
52     }
53
54     
55     
56
57     public void testXTEA() throws Exception JavaDoc {
58         byte[] test = new byte[4096];
59         XTEA tea = new XTEA();
60         tea.setKey("abcdefghijklmnop".getBytes());
61         for (int i = 0; i < 10; i++) {
62             tea.decryptBlock(test, test, 0);
63         }
64     }
65     
66     private void testAES() throws Exception JavaDoc {
67         AES test = new AES();
68         test.setKey(ByteUtils.convertStringToBytes("000102030405060708090A0B0C0D0E0F"));
69         
70         byte[] in = new byte[128];
71         byte[] enc = new byte[128];
72         test.encrypt(enc, 0, 128);
73         test.decrypt(enc, 0, 128);
74         if(ByteUtils.compareNotNull(in, enc)!=0) {
75             throw new Error JavaDoc("hey!");
76         }
77         
78         for (int i = 0; i < 10; i++) {
79             test.encrypt(in, 0, 128);
80             test.decrypt(enc, 0, 128);
81         }
82     }
83
84         
85 }
86
Popular Tags