BCrypt.Net.BCrypt.CryptRaw C# (CSharp) Method

CryptRaw() private method

Perform the central hashing step in the bcrypt scheme.
Thrown when one or more arguments have unsupported or /// illegal values.
private CryptRaw ( byte inputBytes, byte saltBytes, int logRounds ) : byte[]
inputBytes byte The input byte array to hash.
saltBytes byte The salt byte array to hash with.
logRounds int The binary logarithm of the number of rounds of hashing to apply.
return byte[]
        private byte[] CryptRaw(byte[] inputBytes, byte[] saltBytes, int logRounds)
        {
            uint[] cdata = new uint[_BfCryptCiphertext.Length];
            Array.Copy(_BfCryptCiphertext, cdata, _BfCryptCiphertext.Length);
            int clen = cdata.Length;

            if (logRounds < 4 || logRounds > 31)
                throw new ArgumentException("Bad number of rounds", "logRounds");

            if (saltBytes.Length != BCRYPT_SALT_LEN)
                throw new ArgumentException("Bad salt Length", "saltBytes");

            uint rounds = 1u << logRounds;
            Debug.Assert(rounds > 0, "Rounds must be > 0"); // We overflowed rounds at 31 - added safety check

            InitializeKey();
            EKSKey(saltBytes, inputBytes);

            for (int i = 0; i < rounds; i++)
            {
                Key(inputBytes);
                Key(saltBytes);
            }

            for (int i = 0; i < 64; i++)
            {
                for (int j = 0; j < (clen >> 1); j++)
                    Encipher(cdata, j << 1);
            }

            byte[] ret = new byte[clen * 4];
            for (int i = 0, j = 0; i < clen; i++)
            {
                ret[j++] = (byte)((cdata[i] >> 24) & 0xff);
                ret[j++] = (byte)((cdata[i] >> 16) & 0xff);
                ret[j++] = (byte)((cdata[i] >> 8) & 0xff);
                ret[j++] = (byte)(cdata[i] & 0xff);
            }
            return ret;
        }
    }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Calculates the hash of the specified password.
        /// </summary>
        /// <param name="password">Password (1 - 72 bytes).</param>
        /// <param name="salt">Salt (16 bytes).</param>
        /// <param name="workLoad">Workload (4 - 31).</param>
        /// <returns>Hash.</returns>
        public static byte[] Crypt(byte[] password, byte[] salt, int workLoad)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            if (salt == null)
            {
                throw new ArgumentNullException("salt");
            }
            if (password.Length < 1 || password.Length > 72)
            {
                throw new ArgumentOutOfRangeException("password", password, "Must be between 1 and 72 bytes long.");
            }
            if (salt.Length != 16)
            {
                throw new ArgumentOutOfRangeException("salt", salt, "Must be 16 bytes long.");
            }
            if (workLoad < 4 || workLoad > 31)
            {
                throw new ArgumentOutOfRangeException("workLoad", workLoad, "Must be between 4 and 31.");
            }

            var crypt = new BCrypt();

            return(crypt.CryptRaw(password, salt, workLoad));
        }
All Usage Examples Of BCrypt.Net.BCrypt::CryptRaw