Ejemplo n.º 1
0
        public static string HashPassword(string password)
        {
            var salt = GetSecretSalt();

            var hashedBytes = BlowfishCipher.BCrypt(Encoding.UTF8.GetBytes(password), salt, BcryptDifficulty);

            return(Convert.ToBase64String(hashedBytes));
        }
Ejemplo n.º 2
0
        public void BlowfishCipherConstructorTest()
        {
            byte[]         key     = null; // TODO: Initialize to an appropriate value
            CipherMode     mode    = null; // TODO: Initialize to an appropriate value
            CipherPadding  padding = null; // TODO: Initialize to an appropriate value
            BlowfishCipher target  = new BlowfishCipher(key, mode, padding);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
Ejemplo n.º 3
0
        private string GetGameDataChunk(string gameid, string chunkid)
        {
            string content = client.Execute(
                new RestRequest(
                    "consumer/getGameDataChunk/NA1/" + gameid + "/" + chunkid + "/token",
                    Method.GET
                    )
                ).Content;

            var cipher = BlowfishCipher.Create(
                Encoding.ASCII.GetBytes(gameid)
                );

            int offset = 0;

            throw new NotImplementedException();
            //while (offset < )
        }
Ejemplo n.º 4
0
        /// <inheritdoc />
        public override string Crypt(byte[] password, string salt)
        {
            Check.Null("password", password);
            Check.Null("salt", salt);

            Match match = _regex.Match(salt);

            if (!match.Success)
            {
                throw Exceptions.Argument("salt", "Invalid salt.");
            }

            byte[] saltBytes = null, formattedKey = null, crypt = null;
            try
            {
                string prefixString = match.Groups["prefix"].Value;
                bool   compatible   = prefixString == "$2x$";

                int rounds = int.Parse(match.Groups["rounds"].Value);
                if (rounds < MinRounds || rounds > MaxRounds)
                {
                    throw Exceptions.ArgumentOutOfRange("salt", "Invalid number of rounds.");
                }
                saltBytes = Base64Encoding.Blowfish.GetBytes(match.Groups["salt"].Value);

                formattedKey = FormatKey(password);
                crypt        = BlowfishCipher.BCrypt(formattedKey, saltBytes, rounds,
                                                     compatible
                                                ? EksBlowfishKeyExpansionFlags.EmulateCryptBlowfishSignExtensionBug
                                                : EksBlowfishKeyExpansionFlags.None);

                string result = string.Format("{0}{1}${2}{3}", prefixString, rounds.ToString("00"),
                                              Base64Encoding.Blowfish.GetString(saltBytes),
                                              Base64Encoding.Blowfish.GetString(crypt));
                return(result);
            }
            finally
            {
                Security.Clear(saltBytes);
                Security.Clear(formattedKey);
                Security.Clear(crypt);
            }
        }
Ejemplo n.º 5
0
        public override string Crypt(byte[] key, string salt)
        {
            CheckKey(key);
            Helper.CheckNull("salt", salt);

            Match saltMatch = _regex.Match(salt);

            if (!saltMatch.Success)
            {
                throw new ArgumentException("Invalid salt.", "salt");
            }

            int rounds = int.Parse(saltMatch.Groups[1].Value);

            if (rounds < 4 || rounds > 31)
            {
                throw new ArgumentException("Invalid number of rounds.", "salt");
            }
            byte[] saltBytes = UnixBase64.Decode(saltMatch.Groups[2].Value, 128);

            bool resized = key.Length < MaxKeyLength;

            if (resized)
            {
                Array.Resize(ref key, key.Length + 1);
            }             // The ending null terminator is vital for compatibility

            byte[] crypt = BlowfishCipher.BCrypt(key, saltBytes, rounds);

            string result = string.Format("$2a${0}${1}{2}", rounds.ToString("00"),
                                          new string(UnixBase64.Encode(saltBytes)),
                                          new string(UnixBase64.Encode(crypt)));

            Array.Clear(crypt, 0, crypt.Length);
            Array.Clear(saltBytes, 0, saltBytes.Length);

            if (resized)
            {
                Array.Clear(key, 0, key.Length);
            }             // This is new since we resized it.
            return(result);
        }
Ejemplo n.º 6
0
        public void EncryptBlockTest()
        {
            byte[]         key     = null;                                   // TODO: Initialize to an appropriate value
            CipherMode     mode    = null;                                   // TODO: Initialize to an appropriate value
            CipherPadding  padding = null;                                   // TODO: Initialize to an appropriate value
            BlowfishCipher target  = new BlowfishCipher(key, mode, padding); // TODO: Initialize to an appropriate value

            byte[] inputBuffer = null;                                       // TODO: Initialize to an appropriate value
            int    inputOffset = 0;                                          // TODO: Initialize to an appropriate value
            int    inputCount  = 0;                                          // TODO: Initialize to an appropriate value

            byte[] outputBuffer = null;                                      // TODO: Initialize to an appropriate value
            int    outputOffset = 0;                                         // TODO: Initialize to an appropriate value
            int    expected     = 0;                                         // TODO: Initialize to an appropriate value
            int    actual;

            actual = target.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
Ejemplo n.º 7
0
        public void TestBlowfish()
        {
            var vectors = Assembly.GetExecutingAssembly().GetManifestResourceStream("CryptTest.vectors.Blowfish.txt");

            using (Stream stream = vectors)
            {
                using (var reader = new StreamReader(stream))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Match match = Regex.Match(line, @"^([0-9A-z]{16})\s*([0-9A-z]{16})\s*([0-9A-z]{16})$");
                        if (!match.Success)
                        {
                            continue;
                        }

                        string key = match.Groups[1].Value, clear = match.Groups[2].Value, cipher = match.Groups[3].Value;
                        byte[] keyBytes   = HexBase16.Decode(key.ToCharArray());
                        byte[] clearBytes = HexBase16.Decode(clear.ToCharArray());

                        using (BlowfishCipher fish = BlowfishCipher.Create(keyBytes))
                        {
                            var testCipherBytes = new byte[8];
                            fish.Encipher(clearBytes, 0, testCipherBytes, 0);
                            var testCipher = new string(HexBase16.Encode(testCipherBytes));

                            Assert.AreEqual(cipher, testCipher, string.Format("Encipher failed test ({0} became {1})", cipher, testCipher));

                            var testClearBytes = new byte[8];
                            fish.Decipher(testCipherBytes, 0, testClearBytes, 0);
                            var testClear = new string(HexBase16.Encode(testClearBytes));


                            Assert.AreEqual(clear, testClear, string.Format("Decipher failed ({0} became {1})", clear, testClear));
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public static void Test()
        {
            Console.Write("Testing Blowfish");
            using (Stream stream = Assembly.GetEntryAssembly() !.GetManifestResourceStream("CryptSharp.Core.Demo.Blowfish.TestVectors.txt") !)
            {
                using StreamReader reader = new(stream);
                string line;
                while ((line = reader.ReadLine() !) is not null)
                {
                    Match match = Regex.Match(line, @"^([0-9A-z]{16})\s*([0-9A-z]{16})\s*([0-9A-z]{16})$");
                    if (!match.Success)
                    {
                        continue;
                    }

                    string key = match.Groups[1].Value, clear = match.Groups[2].Value, cipher = match.Groups[3].Value;
                    byte[] keyBytes   = Base16Encoding.Hex.GetBytes(key);
                    byte[] clearBytes = Base16Encoding.Hex.GetBytes(clear);

                    Console.Write(".");
                    using BlowfishCipher fish = BlowfishCipher.Create(keyBytes);
                    byte[] testCipherBytes = new byte[8]; fish.Encipher(clearBytes, 0, testCipherBytes, 0);
                    string testCipher      = Base16Encoding.Hex.GetString(testCipherBytes);
                    if (cipher != testCipher)
                    {
                        Console.WriteLine("WARNING: Encipher failed test ({0} became {1})", cipher, testCipher);
                    }

                    byte[] testClearBytes = new byte[8]; fish.Decipher(testCipherBytes, 0, testClearBytes, 0);
                    string testClear      = Base16Encoding.Hex.GetString(testClearBytes);
                    if (clear != testClear)
                    {
                        Console.WriteLine("WARNING: Decipher failed ({0} became {1})", clear, testClear);
                    }
                }
            }

            Console.WriteLine("done.");
        }
Ejemplo n.º 9
0
 public LoginCrypt()
 {
     _cipher = new BlowfishCipher(_key);
 }
Ejemplo n.º 10
0
 public CryptEngine()
 {
     cipher = new BlowfishCipher(key);
 }
Ejemplo n.º 11
0
 public LoginCrypt(byte[] blowfishKey)
 {
     cipher           = new BlowfishCipher(key);
     this.blowfishKey = blowfishKey;
 }