1818 */
1919package org.apache.shiro.crypto
2020
21+ import org.bouncycastle.jce.provider.BouncyCastleProvider
22+
23+ import java.security.Security
24+
25+ import static org.junit.Assert.*;
26+
2127import org.apache.shiro.codec.CodecSupport
2228import org.apache.shiro.util.ByteSource
2329import org.junit.Test
@@ -29,46 +35,146 @@ import static junit.framework.Assert.*
2935 *
3036 * @since 1.0
3137 */
32- public class AesCipherServiceTest {
38+ class AesCipherServiceTest {
3339
3440 private static final String [] PLAINTEXTS = [
3541 " Hello, this is a test." ,
3642 " Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
37- ];
43+ ]
44+
45+ AesCipherServiceTest () {
46+ Security . addProvider(new BouncyCastleProvider ())
47+ }
3848
3949 @Test
40- public void testBlockOperations () {
41- AesCipherService aes = new AesCipherService ();
50+ void testBlockOperations () {
51+ AesCipherService cipher = new AesCipherService ()
52+ assertBlock(cipher)
53+ }
4254
43- byte [] key = aes. generateNewKey(). getEncoded();
55+ @Test
56+ void testStreamingOperations () {
57+ AesCipherService cipher = new AesCipherService ()
58+ assertStreaming(cipher)
59+ }
4460
45- for (String plain : PLAINTEXTS ) {
46- byte [] plaintext = CodecSupport . toBytes(plain);
47- ByteSource ciphertext = aes. encrypt(plaintext, key);
48- ByteSource decrypted = aes. decrypt(ciphertext. getBytes(), key);
49- assertTrue (Arrays . equals(plaintext, decrypted. getBytes()));
50- }
61+ @Test
62+ void testAesGcm () {
63+ assertBlock(OperationMode . GCM )
64+ assertStreaming(OperationMode . GCM )
5165 }
5266
5367 @Test
54- public void testStreamingOperations () {
68+ void testCcm () {
69+ assertBlock(OperationMode . CCM , PaddingScheme . NONE , 13 * 8 ) // 13 bytes
70+ assertStreaming(OperationMode . CCM )
71+ }
5572
56- AesCipherService cipher = new AesCipherService ();
57- byte [] key = cipher. generateNewKey(). getEncoded();
73+ @Test
74+ void testCfb () {
75+ assertBlock(OperationMode . CFB )
76+ assertStreaming(OperationMode . CFB )
77+ }
5878
79+ @Test
80+ void testCtr () {
81+ assertBlock(OperationMode . CTR )
82+ assertStreaming(OperationMode . CTR )
83+ }
84+
85+ @Test
86+ void testEax () {
87+ assertBlock(OperationMode . EAX )
88+ assertStreaming(OperationMode . EAX )
89+ }
90+
91+ @Test
92+ void testEcb () {
93+ assertBlock(OperationMode . ECB , PaddingScheme . PKCS5 )
94+ }
95+
96+ @Test
97+ void testNone () {
98+ assertBlock((OperationMode ) null , null )
99+ }
100+
101+ @Test
102+ void testOcb () {
103+ assertBlock(OperationMode . OCB , PaddingScheme . NONE , 15 * 8 ) // 15 bytes
104+ assertStreaming(OperationMode . OCB , PaddingScheme . NONE , 16 * 8 ) // 16 bytes
105+ }
106+
107+ @Test
108+ void testOfb () {
109+ assertBlock(OperationMode . OFB )
110+ assertStreaming(OperationMode . OFB )
111+ }
112+
113+ @Test
114+ void testPcbc () {
115+ assertBlock(OperationMode . PCBC , PaddingScheme . PKCS5 )
116+ assertStreaming(OperationMode . PCBC , PaddingScheme . PKCS5 )
117+ }
118+
119+ private static assertBlock (OperationMode mode , PaddingScheme scheme = PaddingScheme . NONE , int ivSize = JcaCipherService . DEFAULT_KEY_SIZE ) {
120+ AesCipherService cipher = new AesCipherService ()
121+ cipher. setInitializationVectorSize(ivSize)
122+
123+ if (mode == null ) {
124+ cipher. setModeName(null )
125+ } else {
126+ cipher. setMode(mode)
127+ }
128+
129+ if (scheme == null ) {
130+ cipher. setPaddingSchemeName(null )
131+ } else {
132+ cipher. setPaddingScheme(scheme)
133+ }
134+ assertBlock(cipher)
135+ }
136+
137+ private static assertStreaming (OperationMode mode , PaddingScheme scheme = PaddingScheme . NONE , int ivSize = JcaCipherService . DEFAULT_KEY_SIZE ) {
138+ AesCipherService cipher = new AesCipherService ()
139+ cipher. setInitializationVectorSize(ivSize)
140+
141+ if (mode == null ) {
142+ cipher. setStreamingModeName(null )
143+ } else {
144+ cipher. setStreamingMode(mode)
145+ }
146+
147+ if (scheme == null ) {
148+ cipher. setStreamingPaddingScheme(null )
149+ } else {
150+ cipher. setStreamingPaddingScheme(scheme)
151+ }
152+ assertBlock(cipher)
153+ }
154+
155+ private static assertBlock (AesCipherService cipher , byte [] key = cipher. generateNewKey(). getEncoded()) {
59156 for (String plain : PLAINTEXTS ) {
60- byte [] plaintext = CodecSupport . toBytes(plain);
61- InputStream plainIn = new ByteArrayInputStream (plaintext);
62- ByteArrayOutputStream cipherOut = new ByteArrayOutputStream ();
63- cipher. encrypt(plainIn, cipherOut, key);
64-
65- byte [] ciphertext = cipherOut. toByteArray();
66- InputStream cipherIn = new ByteArrayInputStream (ciphertext);
67- ByteArrayOutputStream plainOut = new ByteArrayOutputStream ();
68- cipher. decrypt(cipherIn, plainOut, key);
69-
70- byte [] decrypted = plainOut. toByteArray();
71- assertTrue (Arrays . equals(plaintext, decrypted));
157+ byte [] plaintext = CodecSupport . toBytes(plain)
158+ ByteSource ciphertext = cipher. encrypt(plaintext, key)
159+ ByteSource decrypted = cipher. decrypt(ciphertext. getBytes(), key)
160+ assertTrue (Arrays . equals(plaintext, decrypted. getBytes()))
161+ }
162+ }
163+
164+ private static assertStreaming (AesCipherService cipher , byte [] key = cipher. generateNewKey(). getEncoded()) {
165+ for (String plain : PLAINTEXTS ) {
166+ byte [] plaintext = CodecSupport . toBytes(plain)
167+ InputStream plainIn = new ByteArrayInputStream (plaintext)
168+ ByteArrayOutputStream cipherOut = new ByteArrayOutputStream ()
169+ cipher. encrypt(plainIn, cipherOut, key)
170+
171+ byte [] ciphertext = cipherOut. toByteArray()
172+ InputStream cipherIn = new ByteArrayInputStream (ciphertext)
173+ ByteArrayOutputStream plainOut = new ByteArrayOutputStream ()
174+ cipher. decrypt(cipherIn, plainOut, key)
175+
176+ byte [] decrypted = plainOut. toByteArray()
177+ assertTrue (Arrays . equals(plaintext, decrypted))
72178 }
73179 }
74180}
0 commit comments