mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
Move BCrypt class to avoid conflict with org.mindrot:jbcrypt (#717)
- Renamed BCryptTest and updated using JUnit Test annotations
This commit is contained in:
@@ -71,7 +71,7 @@ license {
|
|||||||
mapping {
|
mapping {
|
||||||
java = 'SLASHSTAR_STYLE'
|
java = 'SLASHSTAR_STYLE'
|
||||||
}
|
}
|
||||||
excludes(['**/djb/Curve25519.java', '**/sshj/common/Base64.java', '**/org/mindrot/jbcrypt/*.java'])
|
excludes(['**/djb/Curve25519.java', '**/sshj/common/Base64.java', '**/com/hierynomus/sshj/userauth/keyprovider/bcrypt/*.java'])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!JavaVersion.current().isJava9Compatible()) {
|
if (!JavaVersion.current().isJava9Compatible()) {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import net.schmizz.sshj.userauth.keyprovider.KeyFormat;
|
|||||||
import org.bouncycastle.asn1.nist.NISTNamedCurves;
|
import org.bouncycastle.asn1.nist.NISTNamedCurves;
|
||||||
import org.bouncycastle.asn1.x9.X9ECParameters;
|
import org.bouncycastle.asn1.x9.X9ECParameters;
|
||||||
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
||||||
import org.mindrot.jbcrypt.BCrypt;
|
import com.hierynomus.sshj.userauth.keyprovider.bcrypt.BCrypt;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
package org.mindrot.jbcrypt;
|
package com.hierynomus.sshj.userauth.keyprovider.bcrypt;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.DigestException;
|
import java.security.DigestException;
|
||||||
@@ -12,19 +12,23 @@
|
|||||||
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
package org.mindrot.jbcrypt;
|
package com.hierynomus.sshj.userauth.keyprovider.bcrypt;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JUnit unit tests for BCrypt routines
|
* JUnit unit tests for BCrypt routines
|
||||||
* @author Damien Miller
|
* @author Damien Miller
|
||||||
* @version 0.2
|
* @version 0.2
|
||||||
*/
|
*/
|
||||||
public class TestBCrypt extends TestCase {
|
public class BCryptTest {
|
||||||
String test_vectors[][] = {
|
String[][] test_vectors = {
|
||||||
{ "",
|
{ "",
|
||||||
"$2a$06$DCq7YPn5Rq63x1Lad4cll.",
|
"$2a$06$DCq7YPn5Rq63x1Lad4cll.",
|
||||||
"$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s." },
|
"$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s." },
|
||||||
@@ -87,17 +91,10 @@ public class TestBCrypt extends TestCase {
|
|||||||
"$2a$12$WApznUOJfkEGSmYRfnkrPOr466oFDCaj4b6HY3EXGvfxm43seyhgC" },
|
"$2a$12$WApznUOJfkEGSmYRfnkrPOr466oFDCaj4b6HY3EXGvfxm43seyhgC" },
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Entry point for unit tests
|
|
||||||
* @param args unused
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
junit.textui.TestRunner.run(TestBCrypt.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for 'BCrypt.hashpw(String, String)'
|
* Test method for 'BCrypt.hashpw(String, String)'
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testHashpw() {
|
public void testHashpw() {
|
||||||
System.out.print("BCrypt.hashpw(): ");
|
System.out.print("BCrypt.hashpw(): ");
|
||||||
for (int i = 0; i < test_vectors.length; i++) {
|
for (int i = 0; i < test_vectors.length; i++) {
|
||||||
@@ -108,16 +105,16 @@ public class TestBCrypt extends TestCase {
|
|||||||
assertEquals(hashed, expected);
|
assertEquals(hashed, expected);
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for 'BCrypt.gensalt(int)'
|
* Test method for 'BCrypt.gensalt(int)'
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testGensaltInt() {
|
public void testGensaltInt() {
|
||||||
System.out.print("BCrypt.gensalt(log_rounds):");
|
System.out.print("BCrypt.gensalt(log_rounds):");
|
||||||
for (int i = 4; i <= 12; i++) {
|
for (int i = 4; i <= 12; i++) {
|
||||||
System.out.print(" " + Integer.toString(i) + ":");
|
System.out.print(" " + i + ":");
|
||||||
for (int j = 0; j < test_vectors.length; j += 4) {
|
for (int j = 0; j < test_vectors.length; j += 4) {
|
||||||
String plain = test_vectors[j][0];
|
String plain = test_vectors[j][0];
|
||||||
String salt = BCrypt.gensalt(i);
|
String salt = BCrypt.gensalt(i);
|
||||||
@@ -127,12 +124,12 @@ public class TestBCrypt extends TestCase {
|
|||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for 'BCrypt.gensalt()'
|
* Test method for 'BCrypt.gensalt()'
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testGensalt() {
|
public void testGensalt() {
|
||||||
System.out.print("BCrypt.gensalt(): ");
|
System.out.print("BCrypt.gensalt(): ");
|
||||||
for (int i = 0; i < test_vectors.length; i += 4) {
|
for (int i = 0; i < test_vectors.length; i += 4) {
|
||||||
@@ -143,13 +140,13 @@ public class TestBCrypt extends TestCase {
|
|||||||
assertEquals(hashed1, hashed2);
|
assertEquals(hashed1, hashed2);
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for 'BCrypt.checkpw(String, String)'
|
* Test method for 'BCrypt.checkpw(String, String)'
|
||||||
* expecting success
|
* expecting success
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testCheckpw_success() {
|
public void testCheckpw_success() {
|
||||||
System.out.print("BCrypt.checkpw w/ good passwords: ");
|
System.out.print("BCrypt.checkpw w/ good passwords: ");
|
||||||
for (int i = 0; i < test_vectors.length; i++) {
|
for (int i = 0; i < test_vectors.length; i++) {
|
||||||
@@ -158,13 +155,13 @@ public class TestBCrypt extends TestCase {
|
|||||||
assertTrue(BCrypt.checkpw(plain, expected));
|
assertTrue(BCrypt.checkpw(plain, expected));
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test method for 'BCrypt.checkpw(String, String)'
|
* Test method for 'BCrypt.checkpw(String, String)'
|
||||||
* expecting failure
|
* expecting failure
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testCheckpw_failure() {
|
public void testCheckpw_failure() {
|
||||||
System.out.print("BCrypt.checkpw w/ bad passwords: ");
|
System.out.print("BCrypt.checkpw w/ bad passwords: ");
|
||||||
for (int i = 0; i < test_vectors.length; i++) {
|
for (int i = 0; i < test_vectors.length; i++) {
|
||||||
@@ -174,12 +171,12 @@ public class TestBCrypt extends TestCase {
|
|||||||
assertFalse(BCrypt.checkpw(plain, expected));
|
assertFalse(BCrypt.checkpw(plain, expected));
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for correct hashing of non-US-ASCII passwords
|
* Test for correct hashing of non-US-ASCII passwords
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testInternationalChars() {
|
public void testInternationalChars() {
|
||||||
System.out.print("BCrypt.hashpw w/ international chars: ");
|
System.out.print("BCrypt.hashpw w/ international chars: ");
|
||||||
String pw1 = "\u2605\u2605\u2605\u2605\u2605\u2605\u2605\u2605";
|
String pw1 = "\u2605\u2605\u2605\u2605\u2605\u2605\u2605\u2605";
|
||||||
@@ -192,7 +189,6 @@ public class TestBCrypt extends TestCase {
|
|||||||
String h2 = BCrypt.hashpw(pw2, BCrypt.gensalt());
|
String h2 = BCrypt.hashpw(pw2, BCrypt.gensalt());
|
||||||
assertFalse(BCrypt.checkpw(pw1, h2));
|
assertFalse(BCrypt.checkpw(pw1, h2));
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class BCryptHashTV {
|
private static class BCryptHashTV {
|
||||||
@@ -242,7 +238,8 @@ public class TestBCrypt extends TestCase {
|
|||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
public void testBCryptHashTestVectors() throws Exception {
|
@Test
|
||||||
|
public void testBCryptHashTestVectors() {
|
||||||
System.out.print("BCrypt.hash w/ known vectors: ");
|
System.out.print("BCrypt.hash w/ known vectors: ");
|
||||||
for (BCryptHashTV tv : bcrypt_hash_test_vectors) {
|
for (BCryptHashTV tv : bcrypt_hash_test_vectors) {
|
||||||
byte[] output = new byte[tv.out.length];
|
byte[] output = new byte[tv.out.length];
|
||||||
@@ -250,7 +247,6 @@ public class TestBCrypt extends TestCase {
|
|||||||
assertEquals(Arrays.toString(tv.out), Arrays.toString(output));
|
assertEquals(Arrays.toString(tv.out), Arrays.toString(output));
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class BCryptPbkdfTV {
|
private static class BCryptPbkdfTV {
|
||||||
@@ -281,7 +277,8 @@ public class TestBCrypt extends TestCase {
|
|||||||
(byte) 0x83, (byte) 0x3c, (byte) 0xf0, (byte) 0xdc, (byte) 0xf5, (byte) 0x6d, (byte) 0xb6, (byte) 0x56, (byte) 0x08, (byte) 0xe8, (byte) 0xf0, (byte) 0xdc, (byte) 0x0c, (byte) 0xe8, (byte) 0x82, (byte) 0xbd}),
|
(byte) 0x83, (byte) 0x3c, (byte) 0xf0, (byte) 0xdc, (byte) 0xf5, (byte) 0x6d, (byte) 0xb6, (byte) 0x56, (byte) 0x08, (byte) 0xe8, (byte) 0xf0, (byte) 0xdc, (byte) 0x0c, (byte) 0xe8, (byte) 0x82, (byte) 0xbd}),
|
||||||
};
|
};
|
||||||
|
|
||||||
public void testBCryptPbkdfTestVectors() throws Exception {
|
@Test
|
||||||
|
public void testBCryptPbkdfTestVectors() {
|
||||||
System.out.print("BCrypt.pbkdf w/ known vectors: ");
|
System.out.print("BCrypt.pbkdf w/ known vectors: ");
|
||||||
for (BCryptPbkdfTV tv : bcrypt_pbkdf_test_vectors) {
|
for (BCryptPbkdfTV tv : bcrypt_pbkdf_test_vectors) {
|
||||||
byte[] output = new byte[tv.out.length];
|
byte[] output = new byte[tv.out.length];
|
||||||
@@ -289,6 +286,5 @@ public class TestBCrypt extends TestCase {
|
|||||||
assertEquals(Arrays.toString(tv.out), Arrays.toString(output));
|
assertEquals(Arrays.toString(tv.out), Arrays.toString(output));
|
||||||
System.out.print(".");
|
System.out.print(".");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user