View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.assertNotSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.nio.charset.StandardCharsets;
26  
27  import org.junit.jupiter.api.Test;
28  
29  public class UnixCryptTest {
30  
31      @Test
32      public void testCtor() {
33          assertNotNull(new UnixCrypt());
34      }
35  
36      @Test
37      public void testUnixCryptBytes() {
38          // An empty Bytearray equals an empty String
39          assertEquals("12UFlHxel6uMM", Crypt.crypt(new byte[0], "12"));
40          // UTF-8 stores \u00e4 "a with dieresis" as two bytes 0xc3 0xa4.
41          assertEquals("./287bds2PjVw", Crypt.crypt("t\u00e4st", "./"));
42          // ISO-8859-1 stores "a with dieresis" as single byte 0xe4.
43          assertEquals("./bLIFNqo9XKQ", Crypt.crypt("t\u00e4st".getBytes(StandardCharsets.ISO_8859_1), "./"));
44          assertEquals("./bLIFNqo9XKQ", Crypt.crypt(new byte[]{(byte) 0x74, (byte) 0xe4, (byte) 0x73, (byte) 0x74}, "./"));
45      }
46  
47      /**
48       * Some salts are invalid for crypt(3) but not for unixCrypt().
49       */
50      @Test
51      public void testUnixCryptExplicitCall() {
52          // A call to crypt() with an empty salt would result in a "$6$" hash.
53          // Using unixCrypt() explicitly results in a random salt.
54          assertTrue(UnixCrypt.crypt("secret".getBytes()).matches("^[a-zA-Z0-9./]{13}$"));
55          assertTrue(UnixCrypt.crypt("secret".getBytes(), null).matches("^[a-zA-Z0-9./]{13}$"));
56      }
57  
58      /**
59       * Unimplemented "$foo$" salt prefixes would be treated as UnixCrypt salt.
60       */
61      @Test
62      public void testUnixCryptInvalidSalt() {
63          assertThrows(IllegalArgumentException.class, () -> UnixCrypt.crypt("secret", "$a"));
64      }
65  
66      @Test
67      public void testUnixCryptNullData() {
68          assertThrows(NullPointerException.class, () -> UnixCrypt.crypt((byte[]) null));
69      }
70  
71      @Test
72      public void testUnixCryptStrings() {
73          // trivial test
74          assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx"));
75          // empty data
76          assertEquals("12UFlHxel6uMM", Crypt.crypt("", "12"));
77          // salt gets cut at maximum length
78          assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12"));
79          assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12345678"));
80      }
81  
82      @Test
83      public void testUnixCryptWithEmptySalt() {
84          assertThrows(IllegalArgumentException.class, () -> UnixCrypt.crypt("secret", ""));
85      }
86  
87      /**
88       * Single character salts are illegal!
89       * E.g. with glibc 2.13, crypt("secret", "x") = "xxZREZpkHZpkI" but
90       * crypt("secret", "xx") = "xxWAum7tHdIUw" which makes it unverifiable.
91       */
92      @Test
93      public void testUnixCryptWithHalfSalt() {
94          assertThrows(IllegalArgumentException.class, () -> UnixCrypt.crypt("secret", "x"));
95      }
96  
97      @Test
98      public void testUnixCryptWithoutSalt() {
99          final String hash = UnixCrypt.crypt("foo");
100         assertTrue(hash.matches("^[a-zA-Z0-9./]{13}$"));
101         final String hash2 = UnixCrypt.crypt("foo");
102         assertNotSame(hash, hash2);
103     }
104 }