1 // Copyright (C) 2011 - Will Glozer. All rights reserved.
3 package com.lambdaworks.crypto.test;
5 import com.lambdaworks.codec.Base64;
6 import com.lambdaworks.crypto.SCryptUtil;
7 import org.junit.Assert;
9 import static org.junit.Assert.*;
11 public class SCryptUtilTest {
12 String passwd = "secret";
15 public void scrypt() {
20 String hashed = SCryptUtil.scrypt(passwd, N, r, p);
21 String[] parts = hashed.split("\\$");
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);
28 int params = Integer.valueOf(parts[2], 16);
30 assertEquals(N, (int) Math.pow(2, params >> 16 & 0xffff));
31 assertEquals(r, params >> 8 & 0xff);
32 assertEquals(p, params >> 0 & 0xff);
37 String hashed = SCryptUtil.scrypt(passwd, 16384, 8, 1);
39 assertTrue(SCryptUtil.check(passwd, hashed));
40 assertFalse(SCryptUtil.check("s3cr3t", hashed));
44 public void format_0_rp_max() throws Exception {
49 String hashed = SCryptUtil.scrypt(passwd, N, r, p);
50 assertTrue(SCryptUtil.check(passwd, hashed));
52 String[] parts = hashed.split("\\$");
53 int params = Integer.valueOf(parts[2], 16);
55 assertEquals(N, (int) Math.pow(2, params >>> 16 & 0xffff));
56 assertEquals(r, params >> 8 & 0xff);
57 assertEquals(p, params >> 0 & 0xff);