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