@Test public void check() { String hashed = SCryptUtil.scrypt(passwd, N, r, p); assertTrue(SCryptUtil.check(passwd, hashed)); assertFalse(SCryptUtil.check("s3cr3t", hashed)); }
@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 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 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 generateSalt() { final byte[] bytes = SCryptUtil.generateSalt(); assertTrue(bytes != null); // verify 128 bit salt generation assertTrue(bytes.length == 16); }
@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 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 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_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 public void scryptRuby() { int N = 1024; int r = 8; int p = 1; String hashed = SCryptUtil.scryptRuby(passwd, N, r, p); String[] parts = hashed.split("\\$"); assertEquals(5, parts.length); try { assertEquals(8, Hex.decodeHex(parts[3].toCharArray()).length); assertEquals(32, Hex.decodeHex(parts[4].toCharArray()).length); } catch (DecoderException e) { fail("There was an exception decoding the hashed value: \n" + e.getMessage()); } assertEquals(N, Integer.parseInt(parts[0], 16)); assertEquals(r, Integer.parseInt(parts[1], 16)); assertEquals(p, Integer.parseInt(parts[2], 16)); }
@Test(expected = IllegalArgumentException.class) public void scrypt_WithNullSalt() { SCryptUtil.scrypt(passwd, null, N, r, p); }
@Test(expected = IllegalArgumentException.class) public void check_InvalidHashValueWrongVersion() { SCryptUtil.check(passwd, "$s1$PARAMS$SALT$KEY"); }
@Test(expected = IllegalArgumentException.class) public void check_InvalidHashValueWrongNumberOfParts() { SCryptUtil.check(passwd, "$s0$PARAMS$SALT$KEY$EXTRA"); }