1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.messages.value;
21
22
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.util.Arrays;
26
27 import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
28 import org.apache.directory.server.kerberos.shared.io.encoder.EncryptionKeyEncoder;
29 import org.apache.directory.shared.asn1.codec.EncoderException;
30
31 import junit.framework.TestCase;
32
33
34
35
36
37
38
39
40 public class EncryptionKeyTest extends TestCase
41 {
42 public void testEncodingFast() throws Exception
43 {
44 EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
45 { 0x01, 0x02, 0x03 } );
46
47 ByteBuffer encoded = ByteBuffer.allocate( ec.computeLength() );
48
49 ec.encode( encoded );
50
51 byte[] expectedResult = new byte[]
52 {
53 0x30, 0x0c,
54 ( byte ) 0xA0, 0x03,
55 0x02, 0x01, 0x11,
56 ( byte ) 0xA1, 0x05,
57 0x04, 0x03, 0x01, 0x02, 0x03
58 };
59
60 assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
61 }
62
63
64 public void testEncodingNoStructureFast() throws Exception
65 {
66 EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, null );
67
68 ByteBuffer encoded = ByteBuffer.allocate( ec.computeLength() );
69
70 ec.encode( encoded );
71
72 byte[] expectedResult = new byte[]
73 {
74 0x30, 0x09,
75 ( byte ) 0xA0, 0x03,
76 0x02, 0x01, 0x11,
77 ( byte ) 0xA1, 0x02,
78 0x04, 0x00
79 };
80
81 assertTrue( Arrays.equals( expectedResult, encoded.array() ) );
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 public void testEncodingSlow() throws Exception
106 {
107 EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
108 { 0x01, 0x02, 0x03 } );
109
110 byte[] encoded = EncryptionKeyEncoder.encode( ec );
111
112 byte[] expectedResult = new byte[]
113 {
114 0x30, 0x0c,
115 ( byte ) 0xA0, 0x03,
116 0x02, 0x01, 0x11,
117 ( byte ) 0xA1, 0x05,
118 0x04, 0x03, 0x01, 0x02, 0x03
119 };
120
121 assertTrue( Arrays.equals( expectedResult, encoded ) );
122 }
123
124
125 public void testPerfSlow() throws IOException
126 {
127 EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
128 { 0x01, 0x02, 0x03 } );
129 EncryptionKeyEncoder.encode( ec );
130
131 long t0 = System.currentTimeMillis();
132
133
134 {
135 EncryptionKeyEncoder.encode( ec );
136 }
137
138 long t1 = System.currentTimeMillis();
139
140 System.out.println( "Delta = " + ( t1 - t0 ) );
141 }
142
143
144 public void testPerfFast() throws EncoderException
145 {
146 EncryptionKey ec = new EncryptionKey( EncryptionType.AES128_CTS_HMAC_SHA1_96, new byte[]
147 { 0x01, 0x02, 0x03 } );
148 ByteBuffer encoded = ByteBuffer.allocate( ec.computeLength() );
149 ec.encode( encoded );
150
151 long t0 = System.currentTimeMillis();
152
153
154 {
155 encoded = ByteBuffer.allocate( ec.computeLength() );
156
157 ec.encode( encoded );
158 }
159
160 long t1 = System.currentTimeMillis();
161
162 System.out.println( "Delta2 = " + ( t1 - t0 ) );
163 }
164 }