X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Fcom%2Flambdaworks%2Fcrypto%2Ftest%2FSCryptUtilTest.java;fp=tests%2Fcom%2Flambdaworks%2Fcrypto%2Ftest%2FSCryptUtilTest.java;h=f673657e653acaf0228106671d993f28f43b5ad7;hp=0000000000000000000000000000000000000000;hb=46039d771aaab9c83a2d2e6422725a8eeaf11a00;hpb=91bfb697cacaf050e772472f20efea8988acf04a diff --git a/tests/com/lambdaworks/crypto/test/SCryptUtilTest.java b/tests/com/lambdaworks/crypto/test/SCryptUtilTest.java new file mode 100644 index 00000000..f673657e --- /dev/null +++ b/tests/com/lambdaworks/crypto/test/SCryptUtilTest.java @@ -0,0 +1,59 @@ +// Copyright (C) 2011 - Will Glozer. All rights reserved. + +package com.lambdaworks.crypto.test; + +import com.lambdaworks.codec.Base64; +import com.lambdaworks.crypto.SCryptUtil; +import org.junit.Assert; +import org.junit.Test; +import static org.junit.Assert.*; + +public class SCryptUtilTest { + String passwd = "secret"; + + @Test + public void scrypt() { + int N = 16384; + int r = 8; + int p = 1; + + String hashed = SCryptUtil.scrypt(passwd, N, r, p); + String[] parts = hashed.split("\\$"); + + assertEquals(5, parts.length); + assertEquals("s0", parts[1]); + Assert.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)); + } + + @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); + } +}