1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.spring.security;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import org.apache.syncope.common.lib.types.CipherAlgorithm;
27 import org.apache.syncope.core.spring.ApplicationContextProvider;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30
31 public class EncryptorTest {
32
33 private static final String PASSWORD_VALUE = "password";
34
35 private static Encryptor ENCRYPTOR;
36
37 @BeforeAll
38 public static void setUp() {
39 ApplicationContextProvider.getBeanFactory().registerSingleton("securityProperties", new SecurityProperties());
40 ENCRYPTOR = Encryptor.getInstance();
41 }
42
43 @Test
44 public void encoder() throws Exception {
45 for (CipherAlgorithm cipherAlgorithm : CipherAlgorithm.values()) {
46 String encPassword = ENCRYPTOR.encode(PASSWORD_VALUE, cipherAlgorithm);
47
48 assertNotNull(encPassword);
49 assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, cipherAlgorithm, encPassword));
50 assertFalse(ENCRYPTOR.verify(PASSWORD_VALUE + "diff", cipherAlgorithm, encPassword));
51
52
53 if (cipherAlgorithm == CipherAlgorithm.BCRYPT || cipherAlgorithm.isSalted()) {
54 String encSamePassword = ENCRYPTOR.encode(PASSWORD_VALUE, cipherAlgorithm);
55 assertNotNull(encSamePassword);
56 assertFalse(encSamePassword.equals(encPassword));
57 assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, cipherAlgorithm, encSamePassword));
58 }
59 }
60 }
61
62 @Test
63 public void decodeDefaultAESKey() throws Exception {
64 String decPassword = ENCRYPTOR.decode("9Pav+xl+UyHt02H9ZBytiA==", CipherAlgorithm.AES);
65 assertEquals(PASSWORD_VALUE, decPassword);
66 }
67
68 @Test
69 public void smallKey() throws Exception {
70 Encryptor smallKeyEncryptor = Encryptor.getInstance("123");
71 String encPassword = smallKeyEncryptor.encode(PASSWORD_VALUE, CipherAlgorithm.AES);
72 String decPassword = smallKeyEncryptor.decode(encPassword, CipherAlgorithm.AES);
73 assertEquals(PASSWORD_VALUE, decPassword);
74 }
75
76 @Test
77 public void saltedHash() throws Exception {
78 String encPassword = ENCRYPTOR.encode(PASSWORD_VALUE, CipherAlgorithm.SSHA256);
79 assertNotNull(encPassword);
80
81 assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, CipherAlgorithm.SSHA256, encPassword));
82 }
83
84 @Test
85 public void verifySaltedFromExternal() throws Exception {
86
87
88
89 String encPassword = "b098017d584647e3fa1f3e0eb437648aefa84093c15e0d3efb752a4183cfdcf3666ac543";
90 assertTrue(ENCRYPTOR.verify(PASSWORD_VALUE, CipherAlgorithm.SSHA256, encPassword));
91 }
92 }