Ejemplo n.º 1
0
 /// <summary>
 /// Uses the given key, salt, and cost to generate a BCrypt hash.
 /// Flags may modify the key expansion.
 /// </summary>
 /// <param name="key">
 ///     The key. This must be between 1 and 72 bytes.
 ///     Unlike <see cref="BlowfishCrypter"/>, this method does NOT automatically add a null byte to the key.
 /// </param>
 /// <param name="salt">The salt. This must be 16 bytes.</param>
 /// <param name="cost">
 ///     The expansion cost. This is a value between 4 and 31,
 ///     specifying the logarithm of the number of iterations.
 /// </param>
 /// <param name="flags">Flags modifying the key expansion.</param>
 /// <returns>A BCrypt hash.</returns>
 public static byte[] BCrypt(byte[] key, byte[] salt, int cost,
                             EksBlowfishKeyExpansionFlags flags)
 {
     using (BlowfishCipher fish = CreateEks(key, salt, cost, flags))
     {
         return(fish.BCrypt());
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a Blowfish cipher using the provided key.
        /// </summary>
        /// <param name="key">The Blowfish key. This must be between 4 and 56 bytes.</param>
        /// <returns>A Blowfish cipher.</returns>
        public static BlowfishCipher Create(byte[] key)
        {
            Check.Length("key", key, 4, 56);

            BlowfishCipher fish = new BlowfishCipher(null, null);

            fish.ExpandKey(key, _zeroSalt, EksBlowfishKeyExpansionFlags.None);
            return(fish);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Performs an Expensive Key Schedule (EKS) Blowfish key expansion and
        /// creates a Blowfish cipher using the result. Flags may modify the key expansion.
        /// </summary>
        /// <param name="key">
        ///     The key. This must be between 1 and 72 bytes.
        ///     Unlike <see cref="BlowfishCrypter"/>, this method does NOT automatically add a null byte to the key.
        /// </param>
        /// <param name="salt">The salt. This must be 16 bytes.</param>
        /// <param name="cost">
        ///     The expansion cost. This is a value between 4 and 31,
        ///     specifying the logarithm of the number of iterations.
        /// </param>
        /// <param name="flags">Flags modifying the key expansion.</param>
        /// <returns>A Blowfish cipher.</returns>
        public static BlowfishCipher CreateEks(byte[] key, byte[] salt, int cost,
                                               EksBlowfishKeyExpansionFlags flags)
        {
            Check.Length("key", key, 1, 72);
            Check.Length("salt", salt, 16, 16);
            Check.Range("cost", cost, 4, 31);

            BlowfishCipher fish = new BlowfishCipher(null, null);

            fish.ExpandKey(key, salt, flags);
            for (uint i = 1u << cost; i > 0; i--)
            {
                fish.ExpandKey(key, _zeroSalt, flags);
                fish.ExpandKey(salt, _zeroSalt, EksBlowfishKeyExpansionFlags.None);
            }
            return(fish);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Performs an Expensive Key Schedule (EKS) Blowfish key expansion and
        /// creates a Blowfish cipher using the result. Flags may modify the key expansion.
        /// </summary>
        /// <param name="key">
        ///     The key. This must be between 1 and 72 bytes.
        ///     Unlike <see cref="BlowfishCrypter"/>, this method does NOT automatically add a null byte to the key.
        /// </param>
        /// <param name="salt">The salt. This must be 16 bytes.</param>
        /// <param name="cost">
        ///     The expansion cost. This is a value between 4 and 31,
        ///     specifying the logarithm of the number of iterations.
        /// </param>
        /// <param name="flags">Flags modifying the key expansion.</param>
        /// <returns>A Blowfish cipher.</returns>
        public static BlowfishCipher CreateEks(byte[] key, byte[] salt, int cost,
                                               EksBlowfishKeyExpansionFlags flags)
        {
            Check.Length("key", key, 1, 72);
            Check.Length("salt", salt, 16, 16);
            Check.Range("cost", cost, 4, 31);

            BlowfishCipher fish = new BlowfishCipher(null, null);
            fish.ExpandKey(key, salt, flags);
            for (uint i = 1u << cost; i > 0; i --)
            {
                fish.ExpandKey(key, _zeroSalt, flags);
                fish.ExpandKey(salt, _zeroSalt, EksBlowfishKeyExpansionFlags.None);
            }
            return fish;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a Blowfish cipher using the provided key.
        /// </summary>
        /// <param name="key">The Blowfish key. This must be between 4 and 56 bytes.</param>
        /// <returns>A Blowfish cipher.</returns>
        public static BlowfishCipher Create(byte[] key)
        {
            Check.Length("key", key, 4, 56);

            BlowfishCipher fish = new BlowfishCipher(null, null);
            fish.ExpandKey(key, _zeroSalt, EksBlowfishKeyExpansionFlags.None);
            return fish;
        }