So, based on the implementation presented at https://github.com/jeremyh/jBCrypt, you need to change the hashpw
and checkpw
methods to accept char[]
instead of String
Probably, the hardest part is in hashpw
...
try {
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
throw new AssertionError("UTF-8 is not supported");
}
The easiest solution would be to wrap the char[]
back into a String
, but we're trying to avoid that. Instead, based on the highest scoring answer from Converting char[] to byte[], we can do something more like...
char[] expanded = password;
if (minor >= 'a') {
expanded = Arrays.copyOf(expanded, expanded.length + 1);
expanded[expanded.length - 1] = '\000';
}
CharBuffer charBuffer = CharBuffer.wrap(expanded);
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
passwordb = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
The checkpw
method actually doesn't need any modifications (apart from the parameters), as it uses the hashpw
method to check the results.
So, testing...
// We want the same salt for comparison
String salt = BCrypt.gensalt(12);
String original = BCrypt.hashpw("Testing", salt);
System.out.println(original);
String hash = BCrypt.hashpw("Testing".toCharArray(), salt);
System.out.println(hash);
System.out.println(BCrypt.checkpw("Testing", hash));
System.out.println(BCrypt.checkpw("Testing".toCharArray(), hash));
Outputs...
$2a$12$KclXlnca78yhcrg1/mNrRepLYqeJE//SRhrh1X3UM7YUQMjY4x8gy
$2a$12$KclXlnca78yhcrg1/mNrRepLYqeJE//SRhrh1X3UM7YUQMjY4x8gy
true
true
Now, if you have a GitHub account, you could actually clone the original repo, make the suggested changes and generate a pull requests. I'd, personally, be temptered to get rid of the checkpw
and hashpw
methods which require String
I also found this implementation of the PDKDF2, which uses String
, but then promptly converted it to a char[]
... so, that was VERY simply to change...