summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-04-17 14:36:53 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-04-17 14:36:53 +0700
commit022e86493b52c8c8dfe414940acc3a04cdfa3382 (patch)
tree8de29359508c5268149cfb3e4bcd738137b7da42
parentSCrypt: N parameters must be a power of 2 (diff)
downloadBouncyCastle.NET-ed25519-022e86493b52c8c8dfe414940acc3a04cdfa3382.tar.xz
BCrypt: Add method for explicitly including trailing zero on password
-rw-r--r--crypto/src/crypto/generators/BCrypt.cs11
-rw-r--r--crypto/test/src/crypto/test/BCryptTest.cs2
2 files changed, 13 insertions, 0 deletions
diff --git a/crypto/src/crypto/generators/BCrypt.cs b/crypto/src/crypto/generators/BCrypt.cs
index af8029a1b..6e9611ad2 100644
--- a/crypto/src/crypto/generators/BCrypt.cs
+++ b/crypto/src/crypto/generators/BCrypt.cs
@@ -587,6 +587,17 @@ namespace Org.BouncyCastle.Crypto.Generators
         internal const int MAX_PASSWORD_BYTES = 72;
 
         /**
+         * Converts a character password to bytes incorporating the required trailing zero byte.
+         *
+         * @param password the password to be encoded.
+         * @return a byte representation of the password in UTF8 + trailing zero.
+         */
+        public static byte[] PasswordToByteArray(char[] password)
+        {
+            return Arrays.Append(Strings.ToUtf8ByteArray(password), 0);
+        }
+
+        /**
          * Calculates the <b>bcrypt</b> hash of a password.
          * <p>
          * This implements the raw <b>bcrypt</b> function as defined in the bcrypt specification, not
diff --git a/crypto/test/src/crypto/test/BCryptTest.cs b/crypto/test/src/crypto/test/BCryptTest.cs
index 2d9771d17..42d925f80 100644
--- a/crypto/test/src/crypto/test/BCryptTest.cs
+++ b/crypto/test/src/crypto/test/BCryptTest.cs
@@ -134,6 +134,8 @@ namespace Org.BouncyCastle.Crypto.Tests
                 DoTest(password, salt, cost, expected);
             }
 
+            IsTrue(AreEqual(BCrypt.Generate(BCrypt.PasswordToByteArray("12341234".ToCharArray()), Hex.Decode("01020304050607080102030405060708"), 5), Hex.Decode("cdd19088721c50e5cb49a7b743d93b5a6e67bef0f700cd78")));
+            IsTrue(AreEqual(BCrypt.Generate(BCrypt.PasswordToByteArray("1234".ToCharArray()), Hex.Decode("01020304050607080102030405060708"), 5), Hex.Decode("02a3269aca2732484057b40c614204814cbfc2becd8e093e")));
         }
 
         private void DoTest(byte[] password, byte[] salt, int cost, byte[] expected)