Example #1
0
            public static string ComputeHash(string plaintext, Supported_HA hash, byte[] salt)
            {
                int minSaltLength = 4;
                int maxSaltLenght = 6;
                byte[] SaltBytes = null;

                if(salt!=null)
                {
                    SaltBytes = salt;
                }
                else
                {
                    Random r = new Random();
                    int SaltLenght = r.Next(minSaltLength, maxSaltLenght);
                    SaltBytes = new byte[SaltLenght];
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                    rng.GetNonZeroBytes(SaltBytes);
                    rng.Dispose();
                }

                byte[] plaintData = ASCIIEncoding.UTF8.GetBytes(plaintext);
                byte[] plainDataAndSalt = new byte[plaintData.Length + SaltBytes.Length];

                for(int x=0; x<plaintData.Length;x++)
                {
                    plainDataAndSalt[x] = plaintData[x];
                    
                }
                for (int n = 0; n < SaltBytes.Length; n++)
                    plainDataAndSalt[plaintData.Length + n] = SaltBytes[n];

                byte[] hashValue = null;

                switch(hash)
                {
                    case Supported_HA.SHA256:
                        SHA256Managed sha= new SHA256Managed();
                        hashValue= sha.ComputeHash(plainDataAndSalt);
                        sha.Dispose();
                        break;

                    case Supported_HA.SHA384:
                        SHA384Managed sha1 = new SHA384Managed();
                        hashValue = sha1.ComputeHash(plainDataAndSalt);
                        sha1.Dispose();
                        break;
                    case Supported_HA.SHA512:
                        SHA512Managed sha2 = new SHA512Managed();
                        hashValue = sha2.ComputeHash(plainDataAndSalt);
                        sha2.Dispose();
                        break;
                }

                byte[] resuflt = new byte[hashValue.Length + SaltBytes.Length];
                for (int x = 0; x < hashValue.Length; x++)
                    resuflt[x] = hashValue[x];
                for (int n = 0; n < SaltBytes.Length; n++)
                    resuflt[hashValue.Length + n] = SaltBytes[n];
                return Convert.ToBase64String(resuflt);
            }
        public static bool Confirm(string plainText, string hashValue, Supported_HA hash)
        {
            if (plainText == null)
            {
                plainText = "";
            }
            byte[] hashBytes = Convert.FromBase64String(hashValue);
            int    hashSize  = 0;

            switch (hash)
            {
            case Supported_HA.SHA256:
                hashSize = 32;
                break;

            case Supported_HA.SHA384:
                hashSize = 48;
                break;

            case Supported_HA.SHA512:
                hashSize = 64;
                break;
            }

            byte[] saltBytes = new byte[hashBytes.Length - hashSize];

            for (int x = 0; x < saltBytes.Length; x++)
            {
                saltBytes[x] = hashBytes[hashSize + x];
            }

            string newHash = ComputeHash(plainText, hash, saltBytes);

            return(hashValue == newHash);
        }
Example #3
0
        public static bool Confirm(string plainText, string hashvalue, Supported_HA hash)
        {
            byte[] hashBytes = Convert.FromBase64String(hashvalue);

            int hashSize = 0;

            switch (hash)
            {
            case Supported_HA.SHA256:
                hashSize = 32;
                break;

            case Supported_HA.SHA512:
                hashSize = 64;
                break;
            }

            byte[] saltBytes = new byte[hashBytes.Length - hashSize];

            for (int i = 0; i < saltBytes.Length; i++)
            {
                saltBytes[i] = hashBytes[hashSize + i];
            }

            string NewHash = ComputeHash(plainText, hash, saltBytes);

            return(hashvalue == NewHash);
        }
Example #4
0
        public static string ComputeHash(string plainText, Supported_HA hash, byte[] salt)
        {
            int minSaltLength = 4, maxSaltLength = 16;

            byte[] saltBytes = null;
            if (salt != null)
            {
                saltBytes = salt;
            }
            else
            {
                Random r          = new Random();
                int    SaltLength = r.Next(minSaltLength, maxSaltLength);
                saltBytes = new byte[SaltLength];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetNonZeroBytes(saltBytes);
                rng.Dispose();
            }

            byte[] plainData         = ASCIIEncoding.UTF8.GetBytes(plainText);
            byte[] plainDataWithSalt = new byte[plainData.Length + saltBytes.Length];

            for (int x = 0; x < plainData.Length; x++)
            {
                plainDataWithSalt[x] = plainData[x];
            }
            for (int n = 0; n < saltBytes.Length; n++)
            {
                plainDataWithSalt[plainData.Length + n] = saltBytes[n];
            }

            byte[]        hashValue = null;
            SHA256Managed sha       = new SHA256Managed();

            hashValue = sha.ComputeHash(plainDataWithSalt);
            sha.Dispose();



            byte[] result = new byte[hashValue.Length + saltBytes.Length];
            for (int x = 0; x < hashValue.Length; x++)
            {
                result[x] = hashValue[x];
            }
            for (int n = 0; n < saltBytes.Length; n++)
            {
                result[hashValue.Length + n] = saltBytes[n];
            }

            return(Convert.ToBase64String(result));
        }
Example #5
0
        public static bool Confirm(string plainText, string hashValue, Supported_HA hash)
        {
            byte[] hashBytes = Convert.FromBase64String(hashValue);
            int    hashSize  = 32;

            byte[] saltBytes = new byte[hashBytes.Length - hashSize];

            for (int x = 0; x < saltBytes.Length; x++)
            {
                saltBytes[x] = hashBytes[hashSize + x];
            }

            string newHash = ComputeHash(plainText, hash, saltBytes);

            return(hashValue == newHash);
        }
Example #6
0
            public static bool Confirm(string plainText, string hashValue, Supported_HA hash)
            {
                byte[] hashBytes = Convert.FromBase64String(hashValue);
                int hashSize = 0;
                switch(hash)
                {
                    case Supported_HA.SHA256:
                        hashSize = 32;
                        break;
                    case Supported_HA.SHA384:
                        hashSize = 48;
                        break;
                    case Supported_HA.SHA512:
                        hashSize = 64;
                        break;
                }
                byte[] saltBytes = new byte[hashBytes.Length - hashSize];
                for (int x = 0; x < saltBytes.Length; x++)
                    saltBytes[x] = hashBytes[hashSize + x];
                string NewHash = ComputeHash(plainText, hash, saltBytes);

                return (hashValue == NewHash);
            }
Example #7
0
        public static string ComputeHash(string plainText, Supported_HA hash, byte[] salt)
        {
            int minSaltLength = 4;
            int maxSaltLength = 16;

            byte[] SaltBytes = null;

            if (salt != null)
            {
                SaltBytes = salt;
            }
            else
            {
                Random r          = new Random();
                int    SaltLength = r.Next(minSaltLength, maxSaltLength + 1);
                SaltBytes = new byte[SaltLength];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetNonZeroBytes(SaltBytes);
                rng.Dispose();
            }

            byte[] plainData        = ASCIIEncoding.UTF8.GetBytes(plainText);
            byte[] plainDataAndSalt = new byte[plainData.Length + SaltBytes.Length];

            for (int i = 0; i < plainData.Length; i++)
            {
                plainDataAndSalt[i] = plainData[i];
            }
            for (int i = 0; i < SaltBytes.Length; i++)
            {
                plainDataAndSalt[plainData.Length + i] = SaltBytes[i];
            }

            byte[] HashValue = null;

            switch (hash)
            {
            case Supported_HA.SHA256:
                SHA256Managed sha256 = new SHA256Managed();
                HashValue = sha256.ComputeHash(plainDataAndSalt);
                sha256.Dispose();
                break;

            case Supported_HA.SHA512:
                SHA512Managed sha512 = new SHA512Managed();
                HashValue = sha512.ComputeHash(plainDataAndSalt);
                sha512.Dispose();
                break;
            }

            byte[] result = new byte[HashValue.Length + SaltBytes.Length];

            for (int i = 0; i < HashValue.Length; i++)
            {
                result[i] = HashValue[i];
            }
            for (int i = 0; i < SaltBytes.Length; i++)
            {
                result[HashValue.Length + i] = SaltBytes[i];
            }

            return(Convert.ToBase64String(result));
        }
Example #8
0
            public static string ComputeHash(string plaintext, Supported_HA hash, byte[] salt)
            {
                int minSaltLength = 4;
                int maxSaltLenght = 6;

                byte[] SaltBytes = null;

                if (salt != null)
                {
                    SaltBytes = salt;
                }
                else
                {
                    Random r          = new Random();
                    int    SaltLenght = r.Next(minSaltLength, maxSaltLenght);
                    SaltBytes = new byte[SaltLenght];
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                    rng.GetNonZeroBytes(SaltBytes);
                    rng.Dispose();
                }

                byte[] plaintData       = ASCIIEncoding.UTF8.GetBytes(plaintext);
                byte[] plainDataAndSalt = new byte[plaintData.Length + SaltBytes.Length];

                for (int x = 0; x < plaintData.Length; x++)
                {
                    plainDataAndSalt[x] = plaintData[x];
                }
                for (int n = 0; n < SaltBytes.Length; n++)
                {
                    plainDataAndSalt[plaintData.Length + n] = SaltBytes[n];
                }

                byte[] hashValue = null;

                switch (hash)
                {
                case Supported_HA.SHA256:
                    SHA256Managed sha = new SHA256Managed();
                    hashValue = sha.ComputeHash(plainDataAndSalt);
                    sha.Dispose();
                    break;

                case Supported_HA.SHA384:
                    SHA384Managed sha1 = new SHA384Managed();
                    hashValue = sha1.ComputeHash(plainDataAndSalt);
                    sha1.Dispose();
                    break;

                case Supported_HA.SHA512:
                    SHA512Managed sha2 = new SHA512Managed();
                    hashValue = sha2.ComputeHash(plainDataAndSalt);
                    sha2.Dispose();
                    break;
                }

                byte[] resuflt = new byte[hashValue.Length + SaltBytes.Length];
                for (int x = 0; x < hashValue.Length; x++)
                {
                    resuflt[x] = hashValue[x];
                }
                for (int n = 0; n < SaltBytes.Length; n++)
                {
                    resuflt[hashValue.Length + n] = SaltBytes[n];
                }
                return(Convert.ToBase64String(resuflt));
            }
Example #9
0
        public static string ComputeHash(string plainText, Supported_HA hash, byte[] salt)
        {
            int minSaltLength = 4, maxSaltLength = 16;

            byte[] saltBytes = null;
            if (salt != null)
            {
                saltBytes = salt;
            }
            else
            {
                saltBytes = SALT_BYTES;
            }

            byte[] plainData         = ASCIIEncoding.UTF8.GetBytes(plainText);
            byte[] plainDataWithSalt = new byte[plainData.Length + saltBytes.Length];

            for (int x = 0; x < plainData.Length; x++)
            {
                plainDataWithSalt[x] = plainData[x];
            }
            for (int n = 0; n < saltBytes.Length; n++)
            {
                plainDataWithSalt[plainData.Length + n] = saltBytes[n];
            }

            byte[] hashValue = null;

            switch (hash)
            {
            case Supported_HA.SHA256:
                SHA256Managed sha = new SHA256Managed();
                hashValue = sha.ComputeHash(plainDataWithSalt);
                sha.Dispose();
                break;

            case Supported_HA.SHA384:
                SHA384Managed sha1 = new SHA384Managed();
                hashValue = sha1.ComputeHash(plainDataWithSalt);
                sha1.Dispose();
                break;

            case Supported_HA.SHA512:
                SHA512Managed sha2 = new SHA512Managed();
                hashValue = sha2.ComputeHash(plainDataWithSalt);
                sha2.Dispose();
                break;
            }

            byte[] result = new byte[hashValue.Length + saltBytes.Length];
            for (int x = 0; x < hashValue.Length; x++)
            {
                result[x] = hashValue[x];
            }
            for (int n = 0; n < saltBytes.Length; n++)
            {
                result[hashValue.Length + n] = saltBytes[n];
            }

            return(Convert.ToBase64String(result));
        }