EncodeBase64() private static method

Encode a byte array using bcrypt's slightly-modified Base64 encoding scheme. Note that this is _not_ compatible with the standard MIME-Base64 encoding.
private static EncodeBase64 ( byte d, int length ) : string
d byte The byte array to encode
length int The number of bytes to encode
return string
Example #1
0
    public static string HashPassword(string input, string salt)
    {
        if (input == null)
        {
            //throw new ArgumentNullException("input");
        }
        if (string.IsNullOrEmpty(salt))
        {
            //throw new ArgumentException("Invalid salt", "salt");
        }
        char c = '\0';

        if (salt[0] != '$' || salt[1] != '2')
        {
            //throw new SaltParseException("Invalid salt version");
        }
        int num;

        if (salt[2] == '$')
        {
            num = 3;
        }
        else
        {
            c = salt[2];
            if (c != 'a' || salt[3] != '$')
            {
                //throw new SaltParseException("Invalid salt revision");
            }
            num = 4;
        }
        if (salt[num + 2] > '$')
        {
            //throw new SaltParseException("Missing salt rounds");
        }
        int    num2          = Convert.ToInt32(salt.Substring(num, 2));
        string encodedstring = salt.Substring(num + 3, 22);

        byte[] bytes  = Encoding.UTF8.GetBytes(input + ((c >= 'a') ? "\0" : ""));
        byte[] array  = BCrypt.DecodeBase64(encodedstring, 16);
        BCrypt bCrypt = new BCrypt();

        byte[]        byteArray     = bCrypt.CryptRaw(bytes, array, num2);
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.Append("$2");
        if (c >= 'a')
        {
            stringBuilder.Append(c);
        }
        stringBuilder.AppendFormat("${0:00}$", num2);
        stringBuilder.Append(BCrypt.EncodeBase64(array, array.Length));
        stringBuilder.Append(BCrypt.EncodeBase64(byteArray, BCrypt._BfCryptCiphertext.Length * 4 - 1));
        return(stringBuilder.ToString());
    }
Example #2
0
    public static string GenerateSalt(int workFactor)
    {
        if (workFactor < 4 || workFactor > 31)
        {
            //throw new ArgumentOutOfRangeException("workFactor", workFactor, "The work factor must be between 4 and 31 (inclusive)");
        }
        byte[] array = new byte[16];
        RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();

        randomNumberGenerator.GetBytes(array);
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.AppendFormat("$2a${0:00}$", workFactor);
        stringBuilder.Append(BCrypt.EncodeBase64(array, array.Length));
        return(stringBuilder.ToString());
    }