]> WPIA git - gigi.git/blob - tests/com/lambdaworks/crypto/test/SCryptUtilTest.java
Merge "Update notes about password security"
[gigi.git] / tests / com / lambdaworks / crypto / test / SCryptUtilTest.java
1 // Copyright (C) 2011 - Will Glozer. All rights reserved.
2
3 package com.lambdaworks.crypto.test;
4
5 import java.util.Base64;
6
7 import com.lambdaworks.crypto.SCryptUtil;
8
9 import org.junit.Assert;
10 import org.junit.Test;
11
12 import static org.junit.Assert.*;
13
14 public class SCryptUtilTest {
15
16     String passwd = "secret";
17
18     @Test
19     public void scrypt() {
20         int N = 16384;
21         int r = 8;
22         int p = 1;
23
24         String hashed = SCryptUtil.scrypt(passwd, N, r, p);
25         String[] parts = hashed.split("\\$");
26
27         assertEquals(5, parts.length);
28         assertEquals("s0", parts[1]);
29         Assert.assertEquals(16, Base64.getDecoder().decode(parts[3]).length);
30         assertEquals(32, Base64.getDecoder().decode(parts[4]).length);
31
32         int params = Integer.valueOf(parts[2], 16);
33
34         assertEquals(N, (int) Math.pow(2, params >> 16 & 0xffff));
35         assertEquals(r, params >> 8 & 0xff);
36         assertEquals(p, params >> 0 & 0xff);
37     }
38
39     @Test
40     public void check() {
41         String hashed = SCryptUtil.scrypt(passwd, 16384, 8, 1);
42
43         assertTrue(SCryptUtil.check(passwd, hashed));
44         assertFalse(SCryptUtil.check("s3cr3t", hashed));
45     }
46
47     @Test
48     public void format_0_rp_max() throws Exception {
49         int N = 2;
50         int r = 255;
51         int p = 255;
52
53         String hashed = SCryptUtil.scrypt(passwd, N, r, p);
54         assertTrue(SCryptUtil.check(passwd, hashed));
55
56         String[] parts = hashed.split("\\$");
57         int params = Integer.valueOf(parts[2], 16);
58
59         assertEquals(N, (int) Math.pow(2, params >>> 16 & 0xffff));
60         assertEquals(r, params >> 8 & 0xff);
61         assertEquals(p, params >> 0 & 0xff);
62     }
63 }