]> WPIA git - gigi.git/blob - tests/com/lambdaworks/crypto/test/SCryptTest.java
Merge "Update notes about password security"
[gigi.git] / tests / com / lambdaworks / crypto / test / SCryptTest.java
1 // Copyright (C) 2011 - Will Glozer. All rights reserved.
2
3 package com.lambdaworks.crypto.test;
4
5 import com.lambdaworks.crypto.SCrypt;
6 import org.junit.Test;
7
8 import static org.junit.Assert.*;
9 import static com.lambdaworks.crypto.test.CryptoTestUtil.*;
10 import static com.lambdaworks.crypto.SCrypt.*;
11
12 public class SCryptTest {
13
14     @Test
15     public void scrypt_paper_appendix_b() throws Exception {
16         byte[] P, S;
17         int N, r, p, dkLen;
18         String DK;
19
20         // empty key & salt test missing because unsupported by JCE
21
22         P = "password".getBytes("UTF-8");
23         S = "NaCl".getBytes("UTF-8");
24         N = 1024;
25         r = 8;
26         p = 16;
27         dkLen = 64;
28         DK = "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640";
29
30         assertArrayEquals(decode(DK), SCrypt.scrypt(P, S, N, r, p, dkLen));
31
32         P = "pleaseletmein".getBytes("UTF-8");
33         S = "SodiumChloride".getBytes("UTF-8");
34         N = 16384;
35         r = 8;
36         p = 1;
37         dkLen = 64;
38         DK = "7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887";
39
40         assertArrayEquals(decode(DK), scrypt(P, S, N, r, p, dkLen));
41
42         P = "pleaseletmein".getBytes("UTF-8");
43         S = "SodiumChloride".getBytes("UTF-8");
44         N = 1048576;
45         r = 8;
46         p = 1;
47         dkLen = 64;
48         DK = "2101cb9b6a511aaeaddbbe09cf70f881ec568d574a2ffd4dabe5ee9820adaa478e56fd8f4ba5d09ffa1c6d927c40f4c337304049e8a952fbcbf45c6fa77a41a4";
49
50         assertArrayEquals(decode(DK), SCrypt.scrypt(P, S, N, r, p, dkLen));
51     }
52
53     @Test(expected = IllegalArgumentException.class)
54     public void scrypt_invalid_N_zero() throws Exception {
55         byte[] P = "pleaseletmein".getBytes("UTF-8");
56         byte[] S = "SodiumChloride".getBytes("UTF-8");
57         scrypt(P, S, 0, 1, 1, 64);
58     }
59
60     @Test(expected = IllegalArgumentException.class)
61     public void scrypt_invalid_N_odd() throws Exception {
62         byte[] P = "pleaseletmein".getBytes("UTF-8");
63         byte[] S = "SodiumChloride".getBytes("UTF-8");
64         scrypt(P, S, 3, 1, 1, 64);
65     }
66
67     @Test(expected = IllegalArgumentException.class)
68     public void scrypt_invalid_N_large() throws Exception {
69         byte[] P = "pleaseletmein".getBytes("UTF-8");
70         byte[] S = "SodiumChloride".getBytes("UTF-8");
71         int r = 8;
72         int N = Integer.MAX_VALUE / 128;
73         scrypt(P, S, N, r, 1, 64);
74     }
75
76     // @Test(expected = IllegalArgumentException.class)
77     // public void scrypt_invalid_r_large() throws Exception {
78     // byte[] P = "pleaseletmein".getBytes("UTF-8");
79     // byte[] S = "SodiumChloride".getBytes("UTF-8");
80     // int N = 1024;
81     // int r = Integer.MAX_VALUE / 128 + 1;
82     // int p = 0;
83     // scrypt(P, S, N, r, p, 64);
84     // }
85 }