1 // Copyright (C) 2011 - Will Glozer. All rights reserved.
3 package com.lambdaworks.crypto.test;
5 import java.util.Base64;
7 import com.lambdaworks.crypto.SCryptUtil;
9 import org.junit.Assert;
10 import org.junit.Test;
12 import static org.junit.Assert.*;
14 public class SCryptUtilTest {
15 String passwd = "secret";
18 public void scrypt() {
23 String hashed = SCryptUtil.scrypt(passwd, N, r, p);
24 String[] parts = hashed.split("\\$");
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);
31 int params = Integer.valueOf(parts[2], 16);
33 assertEquals(N, (int) Math.pow(2, params >> 16 & 0xffff));
34 assertEquals(r, params >> 8 & 0xff);
35 assertEquals(p, params >> 0 & 0xff);
40 String hashed = SCryptUtil.scrypt(passwd, 16384, 8, 1);
42 assertTrue(SCryptUtil.check(passwd, hashed));
43 assertFalse(SCryptUtil.check("s3cr3t", hashed));
47 public void format_0_rp_max() throws Exception {
52 String hashed = SCryptUtil.scrypt(passwd, N, r, p);
53 assertTrue(SCryptUtil.check(passwd, hashed));
55 String[] parts = hashed.split("\\$");
56 int params = Integer.valueOf(parts[2], 16);
58 assertEquals(N, (int) Math.pow(2, params >>> 16 & 0xffff));
59 assertEquals(r, params >> 8 & 0xff);
60 assertEquals(p, params >> 0 & 0xff);