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