Ejemplo n.º 1
0
 private static void ResetBaseData()
 {
     _plainText        = string.Empty;
     _hashAlgorithm    = StringUtil.HASH_ALGORITHM.Unselected;
     _encryptionFormat = StringUtil.ENCRYPTION_FORMAT.Unselected;
     _encodingType     = StringUtil.ENCODING_TYPES.Unselected;
 }
Ejemplo n.º 2
0
 private static void SetBaseData(string text, string textField, string cbAlgo, string cbEncFormat, bool rbPosStartChecked, bool rbPosEndChecked, string cbEncTypes, string tbSalt, bool addSalt)
 {
     _plainText        = GetText(text, textField);
     _hashAlgorithm    = GetHashAlgorithm(cbAlgo);
     _encryptionFormat = GetEncryptionFormat(_hashAlgorithm, cbEncFormat);
     _saltPosition     = GetSaltPosition(rbPosStartChecked, rbPosEndChecked, _hashAlgorithm);
     _encodingType     = GetEncodingType(cbEncTypes, _hashAlgorithm);
     _salt             = GetSaltByte(tbSalt, _encodingType);
     _addSaltToHash    = addSalt;
 }
Ejemplo n.º 3
0
 public DtoTemplate(int index, string name, string salt, StringUtil.SALT_POSITION saltPosition, bool saltToHash, StringUtil.HASH_ALGORITHM hashAlgorithm, StringUtil.ENCRYPTION_FORMAT encryptionFormat, StringUtil.ENCODING_TYPES encodingType, bool editable)
 {
     Index            = index;
     Name             = name;
     Salt             = salt;
     SaltPosition     = saltPosition;
     SaltToHash       = saltToHash;
     HashAlgorithm    = hashAlgorithm;
     EncryptionFormat = encryptionFormat;
     EncodingType     = encodingType;
     Editable         = editable;
 }
Ejemplo n.º 4
0
 public static byte[] GetSaltByte(string tbSalt, StringUtil.ENCODING_TYPES encodingType)
 {
     return(!string.IsNullOrWhiteSpace(tbSalt) ? StringUtil.GetEncoder(encodingType).GetBytes(tbSalt) : StringHashing.GenerateRandomSalt());
 }
Ejemplo n.º 5
0
        public static byte[] GetByteArrayFromEncodedString(StringUtil.ENCRYPTION_FORMAT format, string strHash, StringUtil.ENCODING_TYPES encodingType)
        {
            switch (format)
            {
            case StringUtil.ENCRYPTION_FORMAT.Base64:
                return(Convert.FromBase64String(strHash));

            case StringUtil.ENCRYPTION_FORMAT.Hex:
                return(StringUtil.FromHex(strHash));

            default:
                return(StringUtil.GetEncoder(encodingType).GetBytes(strHash));
            }
        }
Ejemplo n.º 6
0
        public static string ComputeHash(string plainText, StringUtil.HASH_ALGORITHM hashAlgorithm, byte[] saltBytes, StringUtil.ENCRYPTION_FORMAT format, StringUtil.ENCODING_TYPES encodingType, StringUtil.SALT_POSITION saltPosition, bool addSaltToHash)
        {
            string hashValue;

            if (hashAlgorithm == StringUtil.HASH_ALGORITHM.Md5)
            {
                hashValue = GetMd5Hash(plainText);
            }
            else
            {
                // If salt is not specified, generate it on the fly.
                if (saltBytes == null)
                {
                    saltBytes = GenerateRandomSalt();
                }

                // Convert plain text into a byte array.
                byte[] plainTextBytes = StringUtil.GetEncoder(encodingType).GetBytes(plainText);

                // Allocate array, which will hold plain text and salt.
                byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];

                if (saltPosition == StringUtil.SALT_POSITION.Tail)
                {
                    // Copy plain text bytes into resulting array.
                    for (int i = 0; i < plainTextBytes.Length; i++)
                    {
                        plainTextWithSaltBytes[i] = plainTextBytes[i];
                    }

                    // Append salt bytes to the resulting array.
                    for (int i = 0; i < saltBytes.Length; i++)
                    {
                        plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
                    }
                }
                else
                {
                    // Copy salt bytes into resulting array.
                    for (int i = 0; i < saltBytes.Length; i++)
                    {
                        plainTextWithSaltBytes[i] = saltBytes[i];
                    }

                    // prefix salt bytes to the resulting array.
                    for (int i = 0; i < plainTextBytes.Length; i++)
                    {
                        plainTextWithSaltBytes[saltBytes.Length + i] = plainTextBytes[i];
                    }
                }


                // Because we support multiple hashing algorithms, we must define
                // hash object as a common (abstract) base class. We will specify the
                // actual hashing algorithm class later during object creation.

                // Make sure hashing algorithm name is specified.
                //if (hashAlgorithm == null)
                //    hashAlgorithm = "";

                // Initialize appropriate hashing algorithm class.
                HashAlgorithm hash = StringUtil.GetHashAlgorithm(hashAlgorithm);

                // Compute hash value of our plain text with appended salt.
                byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

                int hashLen;

                if (addSaltToHash)
                {
                    hashLen = hashBytes.Length + saltBytes.Length;
                }
                else
                {
                    hashLen = hashBytes.Length;
                }

                // Create array which will hold hash and original salt bytes.
                byte[] hashWithSaltBytes = new byte[hashLen];

                // Copy hash bytes into resulting array.
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    hashWithSaltBytes[i] = hashBytes[i];
                }

                if ((addSaltToHash) && (hashAlgorithm != StringUtil.HASH_ALGORITHM.Md5))
                {
                    // Append salt bytes to the result.
                    for (int i = 0; i < saltBytes.Length; i++)
                    {
                        hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
                    }
                }

                hashValue = GetFormatedStringFromHash(format, hashWithSaltBytes, encodingType);
            }

            // Convert result into a base64-encoded string.


            // Return the result.
            return(hashValue);
        }
Ejemplo n.º 7
0
        public static string GetFormatedStringFromHash(StringUtil.ENCRYPTION_FORMAT format, byte[] hash, StringUtil.ENCODING_TYPES encodingType)
        {
            switch (format)
            {
            case StringUtil.ENCRYPTION_FORMAT.Base64:
                return(Convert.ToBase64String(hash));

            case StringUtil.ENCRYPTION_FORMAT.Hex:
                return(StringUtil.ToHex(hash));

            default:
                return(StringUtil.GetEncoder(encodingType).GetString(hash));
            }
        }
Ejemplo n.º 8
0
        public static string HashPasswordFromSaltedPassword(string plainText, StringUtil.HASH_ALGORITHM hashAlgorithm, string hashValue, StringUtil.ENCRYPTION_FORMAT format, StringUtil.ENCODING_TYPES encodingType, StringUtil.SALT_POSITION saltPosition)
        {
            // Convert base64-encoded hash value into a byte array.
            byte[] hashWithSaltBytes = GetByteArrayFromEncodedString(format, hashValue, encodingType);

            // We must know size of hash (without salt).

            // Size of hash is based on the specified algorithm.
            int hashSizeInBits = hashAlgorithm.GetHashCode();

            // Convert size of hash from bits to bytes.
            int hashSizeInBytes = hashSizeInBits / 8;

            // Make sure that the specified hash value is long enough.
            if (hashWithSaltBytes.Length < hashSizeInBytes)
            {
                return(String.Empty);
            }

            // Allocate array to hold original salt bytes retrieved from hash.
            byte[] saltBytes = new byte[hashWithSaltBytes.Length -
                                        hashSizeInBytes];

            if (saltPosition == StringUtil.SALT_POSITION.Tail)
            {
                // Copy salt from the end of the hash to the new array.
                for (int i = 0; i < saltBytes.Length; i++)
                {
                    saltBytes[i] = hashWithSaltBytes[hashSizeInBytes + i];
                }
            }
            else
            {
                // Copy salt from the begin of the hash to the new array.
                for (int i = 0; i < saltBytes.Length; i++)
                {
                    saltBytes[i] = hashWithSaltBytes[i];
                }
            }

            // Compute a new hash string.
            string expectedHashString =
                ComputeHash(plainText, hashAlgorithm, saltBytes, format, encodingType, saltPosition, true);

            // If the computed hash matches the specified hash,
            // the plain text value must be correct.
            return(expectedHashString);
        }