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.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.nio.charset.StandardCharsets;
25  import java.util.Arrays;
26  import java.util.concurrent.ThreadLocalRandom;
27  
28  import org.junit.jupiter.api.Test;
29  
30  public class Sha256CryptTest {
31  
32      @Test
33      public void testSha256CryptBytes() {
34          // An empty Bytearray equals an empty String
35          assertEquals("$5$foo$Fq9CX624QIfnCAmlGiPKLlAasdacKCRxZztPoeo7o0B", Crypt.crypt(new byte[0], "$5$foo"));
36          // UTF-8 stores \u00e4 "a with dieresis" as two bytes 0xc3 0xa4.
37          assertEquals("$5$./$iH66LwY5sTDTdHeOxq5nvNDVAxuoCcyH/y6Ptte82P8", Crypt.crypt("t\u00e4st", "$5$./$"));
38          // ISO-8859-1 stores "a with dieresis" as single byte 0xe4.
39          assertEquals("$5$./$qx5gFfCzjuWUOvsDDy.5Nor3UULPIqLVBZhgGNS0c14", Crypt.crypt("t\u00e4st".getBytes(StandardCharsets.ISO_8859_1), "$5$./$"));
40      }
41  
42      @Test
43      public void testSha256CryptExplicitCall() {
44          assertTrue(Sha2Crypt.sha256Crypt("secret".getBytes()).matches("^\\$5\\$[a-zA-Z0-9./]{0,16}\\$.{1,}$"));
45          assertTrue(Sha2Crypt.sha256Crypt("secret".getBytes(), null).matches("^\\$5\\$[a-zA-Z0-9./]{0,16}\\$.{1,}$"));
46      }
47  
48      @Test
49      public void testSha256CryptNullData() {
50          assertThrows(NullPointerException.class, () -> Sha2Crypt.sha256Crypt((byte[]) null));
51      }
52  
53      @Test
54      public void testSha256CryptStrings() {
55          // empty data
56          assertEquals("$5$foo$Fq9CX624QIfnCAmlGiPKLlAasdacKCRxZztPoeo7o0B", Crypt.crypt("", "$5$foo"));
57          // salt gets cut at dollar sign
58          assertEquals("$5$45678$LulJuUIJIn.1uU.KPV9x92umMYFopzVDD.o2ZqA1i2/", Crypt.crypt("secret", "$5$45678"));
59          assertEquals("$5$45678$LulJuUIJIn.1uU.KPV9x92umMYFopzVDD.o2ZqA1i2/", Crypt.crypt("secret", "$5$45678$012"));
60          assertEquals("$5$45678$LulJuUIJIn.1uU.KPV9x92umMYFopzVDD.o2ZqA1i2/", Crypt.crypt("secret", "$5$45678$012$456"));
61          // salt gets cut at maximum length
62          assertEquals("$5$1234567890123456$GUiFKBSTUAGvcK772ulTDPltkTOLtFvPOmp9o.9FNPB", Crypt.crypt("secret", "$5$1234567890123456"));
63          assertEquals("$5$1234567890123456$GUiFKBSTUAGvcK772ulTDPltkTOLtFvPOmp9o.9FNPB", Crypt.crypt("secret", "$5$1234567890123456789"));
64      }
65  
66      @Test
67      public void testSha256CryptWithEmptySalt() {
68          assertThrows(IllegalArgumentException.class, () -> Sha2Crypt.sha256Crypt("secret".getBytes(), ""));
69      }
70  
71      @Test
72      public void testSha256LargestThanBlocksize() {
73          final byte[] buffer = new byte[200];
74          Arrays.fill(buffer, 0, 200, (byte) 'A');
75          assertEquals("$5$abc$HbF3RRc15OwNKB/RZZ5F.1I6zsLcKXHQoSdB9Owx/Q8", Sha2Crypt.sha256Crypt(buffer, "$5$abc"));
76          // input password is 0-filled on return
77          assertArrayEquals(new byte[buffer.length], buffer);
78      }
79  
80      @Test
81      public void testSha2CryptRounds() {
82          // minimum rounds?
83          assertEquals("$5$rounds=1000$abcd$b8MCU4GEeZIekOy5ahQ8EWfT330hvYGVeDYkBxXBva.", Sha2Crypt.sha256Crypt("secret".getBytes(StandardCharsets.UTF_8), "$5$rounds=50$abcd$"));
84          assertEquals("$5$rounds=1001$abcd$SQsJZs7KXKdd2DtklI3TY3tkD7UYA99RD0FBLm4Sk48", Sha2Crypt.sha256Crypt("secret".getBytes(StandardCharsets.UTF_8), "$5$rounds=1001$abcd$"));
85          assertEquals("$5$rounds=9999$abcd$Rh/8ngVh9oyuS6lL3.fsq.9xbvXJsfyKWxSjO2mPIa7", Sha2Crypt.sha256Crypt("secret".getBytes(StandardCharsets.UTF_8), "$5$rounds=9999$abcd"));
86      }
87  
88      @Test
89      public void testSha2CryptRoundsThreadLocalRandom() {
90          final ThreadLocalRandom random = ThreadLocalRandom.current();
91          // minimum rounds?
92          assertEquals("$5$rounds=1000$abcd$b8MCU4GEeZIekOy5ahQ8EWfT330hvYGVeDYkBxXBva.", Sha2Crypt.sha256Crypt("secret".getBytes(StandardCharsets.UTF_8), "$5$rounds=50$abcd$", random));
93          assertEquals("$5$rounds=1001$abcd$SQsJZs7KXKdd2DtklI3TY3tkD7UYA99RD0FBLm4Sk48", Sha2Crypt.sha256Crypt("secret".getBytes(StandardCharsets.UTF_8), "$5$rounds=1001$abcd$", random));
94          assertEquals("$5$rounds=9999$abcd$Rh/8ngVh9oyuS6lL3.fsq.9xbvXJsfyKWxSjO2mPIa7", Sha2Crypt.sha256Crypt("secret".getBytes(StandardCharsets.UTF_8), "$5$rounds=9999$abcd", random));
95      }
96  
97      @Test
98      public void testZeroOutInput() {
99          final byte[] buffer = new byte[200];
100         Arrays.fill(buffer, (byte) 'A');
101         Sha2Crypt.sha256Crypt(buffer);
102         // input password is 0-filled on return
103         assertArrayEquals(new byte[buffer.length], buffer);
104     }
105 
106 }