1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.codec.digest;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import org.junit.jupiter.api.Test;
25
26 public class CryptTest {
27
28
29
30 public static void main(final String[] args) {
31 final String hash;
32 switch (args.length) {
33 case 1:
34 hash = Crypt.crypt(args[0]);
35 System.out.println(hash.length() + ": " + hash);
36 break;
37 case 2:
38 hash = Crypt.crypt(args[0], args[1]);
39 System.out.println(hash.length() + "; " + hash);
40 break;
41 default:
42 System.out.println("Enter key [salt (remember to quote this!)]");
43 break;
44 }
45 }
46
47
48 private void startsWith(final String string, final String prefix) {
49 assertTrue(string.startsWith(prefix), string + " should start with " + prefix);
50 }
51
52 @Test
53 public void testBadSalt() {
54
55 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$1$"));
56 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$5$"));
57 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$6$"));
58
59 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$1$%"));
60 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$5$!"));
61 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$6$_"));
62 }
63
64 @Test
65 public void testBadType() {
66 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$2$xxxx"));
67 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$3$xxxx"));
68 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$4$"));
69 }
70
71 @Test
72 public void testCrypt() {
73 assertNotNull(new Crypt());
74 }
75 @Test
76 public void testCryptWithBytes() {
77 final byte[] keyBytes = { 'b', 'y', 't', 'e' };
78 final String hash = Crypt.crypt(keyBytes);
79 assertEquals(hash, Crypt.crypt("byte", hash));
80 }
81
82
83
84
85
86
87
88
89 @Test
90 public void testCryptWithEmptySalt() {
91 assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", ""));
92 }
93 @Test
94 public void testDefaultCryptVariant() {
95
96 assertTrue(Crypt.crypt("secret").startsWith("$6$"));
97 assertTrue(Crypt.crypt("secret", null).startsWith("$6$"));
98 }
99 @Test
100 public void testSamples() {
101 assertEquals("$1$xxxx$aMkevjfEIpa35Bh3G4bAc.", Crypt.crypt("secret", "$1$xxxx"));
102 assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx"));
103 }
104 @Test
105 public void testStored() {
106 assertEquals("$1$xxxx$aMkevjfEIpa35Bh3G4bAc.", Crypt.crypt("secret", "$1$xxxx$aMkevjfEIpa35Bh3G4bAc."));
107 assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xxWAum7tHdIUw"));
108 }
109
110 @Test
111 public void testType() {
112 startsWith(Crypt.crypt("secret", "xxxx"), "xx");
113 startsWith(Crypt.crypt("secret", "$1$xxxx"), "$1$xxxx$");
114 startsWith(Crypt.crypt("secret", "$5$xxxx"), "$5$xxxx$");
115 startsWith(Crypt.crypt("secret", "$6$xxxx"), "$6$xxxx$");
116 }
117 }