@Test public void scrypt_WithSameSalt() { byte[] salt = new byte[16]; Arrays.fill(salt, (byte) 1); String hashed1 = SCryptUtil.scrypt(passwd, salt, N, r, p); String hashed2 = SCryptUtil.scrypt(passwd, salt, N, r, p); assertTrue(hashed1.equals(hashed2)); }
@Test public void check() { String hashed = SCryptUtil.scrypt(passwd, N, r, p); assertTrue(SCryptUtil.check(passwd, hashed)); assertFalse(SCryptUtil.check("s3cr3t", hashed)); }
@Test(expected = IllegalArgumentException.class) public void scrypt_WithTooLongSalt() { byte[] salt = new byte[17]; Arrays.fill(salt, (byte) 1); SCryptUtil.scrypt(passwd, salt, N, r, p); }
@Test public void check_WithSpecifiedSalt() { byte[] salt = new byte[16]; Arrays.fill(salt, (byte) 1); String hashed = SCryptUtil.scrypt(passwd, salt, N, r, p); assertTrue(SCryptUtil.check(passwd, hashed)); assertFalse(SCryptUtil.check("s3cr3t", hashed)); }
@Test public void format_0_rp_max() throws Exception { int N = 2; int r = 255; int p = 255; String hashed = SCryptUtil.scrypt(passwd, N, r, p); assertTrue(SCryptUtil.check(passwd, hashed)); String[] parts = hashed.split("\\$"); int params = Integer.valueOf(parts[2], 16); assertEquals(N, (int) Math.pow(2, params >>> 16 & 0xffff)); assertEquals(r, params >> 8 & 0xff); assertEquals(p, params >> 0 & 0xff); }
@Test public void check() { String hashed = SCryptUtil.scrypt(passwd, 16384, 8, 1); assertTrue(SCryptUtil.check(passwd, hashed)); assertFalse(SCryptUtil.check("s3cr3t", hashed)); String hashed2 = SCryptUtil.scryptRuby(passwd, 16384, 8, 1); assertTrue(SCryptUtil.check(passwd, hashed2)); assertFalse(SCryptUtil.check("s3cr3t", hashed2)); // Test with an example from the Ruby scrypt project String preHashed = "400$8$36$78f4ae6983f76119$37ec6ce55a2b928dc56ff9a7d0cdafbd7dbde49d9282c38a40b1434e88f24cf5"; assertTrue(SCryptUtil.check("my grand secret", preHashed)); }
@Test public void scrypt() { String hashed = SCryptUtil.scrypt(passwd, N, r, p); String[] parts = hashed.split("\\$"); assertEquals(5, parts.length); assertEquals("s0", parts[1]); assertEquals(16, Base64.decode(parts[3].toCharArray()).length); assertEquals(32, Base64.decode(parts[4].toCharArray()).length); int params = Integer.valueOf(parts[2], 16); assertEquals(N, (int) Math.pow(2, params >> 16 & 0xffff)); assertEquals(r, params >> 8 & 0xff); assertEquals(p, params >> 0 & 0xff); }
@Test public void scrypt_WithSpecifiedSalt() { byte[] salt = new byte[16]; Arrays.fill(salt, (byte) 1); String hashed = SCryptUtil.scrypt(passwd, salt, N, r, p); String[] parts = hashed.split("\\$"); assertEquals(5, parts.length); assertEquals("s0", parts[1]); assertEquals(16, Base64.decode(parts[3].toCharArray()).length); assertEquals(32, Base64.decode(parts[4].toCharArray()).length); int params = Integer.valueOf(parts[2], 16); assertEquals(N, (int) Math.pow(2, params >> 16 & 0xff)); assertEquals(r, params >> 8 & 0x0f); assertEquals(p, params >> 0 & 0x0f); }
@Test(expected = IllegalArgumentException.class) public void scrypt_WithNullSalt() { SCryptUtil.scrypt(passwd, null, N, r, p); }