public byte[] CreateUsernamePasswordHash(String Username, String Password)
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password))
            {
                return(null);
            }
            this.Username = Username;
            SHA256       mySha    = SHA256Cng.Create();
            MemoryStream mYStream = new MemoryStream();

            byte[] ID = Encoding.ASCII.GetBytes(Username);
            mYStream.Write(ID, 0, ID.Length);
            ID = Encoding.ASCII.GetBytes(Password);
            mYStream.Write(ID, 0, ID.Length);
            byte[] byteReturn = mySha.ComputeHash(mYStream.ToArray());
            mYStream = new MemoryStream();
            Debug.WriteLine("Created " + BitConverter.ToString(byteReturn));
            ID = BitConverter.GetBytes(base.GetCreationTime());
            Debug.WriteLine("TIME STAMP " + BitConverter.ToString(ID));
            mYStream.Write(ID, 0, ID.Length);
            mYStream.Write(byteReturn, 0, byteReturn.Length);
            Debug.WriteLine("PREHASH " + BitConverter.ToString(mYStream.ToArray()));
            mySHAHash = mySha.ComputeHash(mYStream.ToArray());
            InternalSettings.UserID   = mySHAHash;
            base.UserID               = InternalSettings.UserID;
            InternalSettings.Username = Username;
            Debug.WriteLine("GENERATED HASH " + BitConverter.ToString(mySHAHash));
            return(byteReturn);
        }
Esempio n. 2
0
        /// <summary>
        /// Given a file path, compute the file hashes.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="md5Hash"></param>
        /// <param name="sha1Hash"></param>
        /// <param name="sha256Hash"></param>
        public static void ComputeHashes(string filePath, out byte[] md5Hash, out byte[] sha1Hash, out byte[] sha256Hash)
        {
            using (var md5 = MD5Cng.Create())
                using (var sha1 = SHA1Cng.Create())
                    using (var sha256 = SHA256Cng.Create())
                        using (var input = File.OpenRead(filePath))
                        {
                            byte[] buffer = new byte[8192];
                            int    bytesRead;
                            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                                sha1.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                                sha256.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                            }
                            // We have to call TransformFinalBlock, but we don't have any
                            // more data - just provide 0 bytes.
                            md5.TransformFinalBlock(buffer, 0, 0);
                            sha1.TransformFinalBlock(buffer, 0, 0);
                            sha256.TransformFinalBlock(buffer, 0, 0);

                            md5Hash    = md5.Hash;
                            sha1Hash   = sha1.Hash;
                            sha256Hash = sha256.Hash;
                        }
        }
Esempio n. 3
0
 public void ComputeHash(String filePath)
 {
     using (var md5 = SHA256Cng.Create())
     {
         this.md5String = md5.ComputeHash(File.ReadAllBytes(filePath));
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Computes SHA256 hash of a given input
        /// </summary>
        /// <param name="input">input byte array which needs to be hashed</param>
        /// <returns>Returns SHA256 hash in a string form</returns>
        internal static string GetSHA256Hash(byte[] input) {
            Debug.Assert(input != null);

            using (SHA256 sha256 = SHA256Cng.Create()) {
                byte[] hashValue = sha256.ComputeHash(input);
                return GetHexString(hashValue);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// CheckSum256 method implementation
 /// </summary>
 public static byte[] CheckSum256(byte[] value)
 {
     byte[] hash = null;
     using (SHA256 sha256 = SHA256Cng.Create())
     {
         hash = sha256.ComputeHash(value);
     }
     return(hash);
 }
Esempio n. 6
0
        public static string HashString(string value, string salt)
        {
            SHA256 mySHA256 = SHA256Cng.Create();

            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

            byte[] DataToEncrypt = encoding.GetBytes(value + salt);
            return(Convert.ToBase64String(mySHA256.ComputeHash(DataToEncrypt)));
        }
        public byte[] EncryptBinaryData(ref byte[] data)
        {
            byte[] defaultKey = EncodeKeyToByteArray(DefaultKey);
            var    hashAlg    = SHA256Cng.Create("SHA256");

            hashAlg.Initialize();
            defaultKey = hashAlg.ComputeHash(defaultKey);

            return(EncryptBinaryDataInternal(ref data, ref defaultKey, false));
        }
Esempio n. 8
0
        /// <summary>
        /// Sets the sha256 data.
        /// </summary>
        /// <param name="inputData">The input data.</param>
        /// <param name="record">The record.</param>
        private static void SetSha256Data(byte[] inputData, DSRecord record)
        {
            byte[] sha256Hash;
            using (SHA256 sha256 = SHA256Cng.Create())
            {
                sha256Hash = sha256.ComputeHash(inputData);
            }

            record.DigestField256Text = sha256Hash.ToHexString();
            record.DigestField256     = sha256Hash;
            record.DigestType256      = DigestType.Sha256;
        }
 public byte[] GetHash(byte[] sourceBuffer)
 {
     try
     {
         var sha256Cng = SHA256Cng.Create();
         return(sha256Cng.ComputeHash(sourceBuffer));
     }
     catch (Exception e)
     {
         Debug.WriteLine($"SHA256Cng service GetSHA256CngHash error: {e.Message}");
         return(default(byte[]));
     }
 }
Esempio n. 10
0
        /// <summary>
        /// 计算文件的 sha256 值
        /// </summary>
        /// <param name="path">要计算 MD5 值的文件名和路径</param>
        /// <returns>MD5 值16进制字符串</returns>
        public static string GetFileSHA256(string path)
        {
            if (System.IO.File.Exists(path) == false)
            {
                return(string.Empty);
            }

            FileStream fs     = new FileStream(path, FileMode.Open, FileAccess.Read);
            SHA256     sha256 = SHA256Cng.Create();

            byte[] hashByts = sha256.ComputeHash(fs);
            return(ConvertHashBytes(hashByts));
        }
        private string ConvertToBase32SerialNumber(byte[] hashBytes)
        {
            byte[] halfHashBytes = SHA256Cng.Create().ComputeHash(hashBytes);
            string base32Str     = Base32.ToBase32String(halfHashBytes);

            string key = "";

            for (int i = 0; i < base32Str.Length / 2; i += 4)
            {
                key += base32Str.Substring(i, 4) + "-";
            }

            return(key.TrimEnd('-'));
        }
Esempio n. 12
0
        public void AddUser(string username, string password)
        {
            SqlCommand myCommand = ConnectToDatabase.CreateCommand();

            myCommand.CommandText = "INSERT INTO UsersTable(Username, Hash) VALUES (@Username, @Hash)";
            myCommand.Parameters.Add(new SqlParameter("Username", username));
            SHA256       myInstance = SHA256Cng.Create();
            MemoryStream mYStream   = new MemoryStream();

            byte[] ID = Encoding.ASCII.GetBytes(username);
            mYStream.Write(ID, 0, ID.Length);
            ID = Encoding.ASCII.GetBytes(password);
            mYStream.Write(ID, 0, ID.Length);
            byte[] byteReturn = myInstance.ComputeHash(mYStream.ToArray());
            mYStream = new MemoryStream(byteReturn);
            myCommand.Parameters.Add(new SqlParameter("Hash", mYStream.ToArray()));
            Console.Write("Lines affected : " + myCommand.ExecuteNonQuery());
            Console.WriteLine("HASH: " + BitConverter.ToString(mYStream.ToArray()));
        }
Esempio n. 13
0
        public byte[] ComputeHash(byte[] input)
        {
            switch (SelectedAlgorithm)
            {
            case Algorithm.SHA1:
                return(SHA1Cng.Create().ComputeHash(input));

            case Algorithm.SHA256:
                return(SHA256Cng.Create().ComputeHash(input));

            case Algorithm.SHA384:
                return(SHA384Cng.Create().ComputeHash(input));

            case Algorithm.SHA512:
                return(SHA512Cng.Create().ComputeHash(input));

            default:
                throw new ArgumentException("Valid options are: SHA1, SHA256, SHA384 or SHA512");
            }
        }
Esempio n. 14
0
 public bool VerifyUser(string username, long dateTime, byte[] v)
 { //TODO Lock this
     lock (Command1)
     {
         SHA256 mySha        = SHA256Cng.Create();
         byte[] OriginalHash = GetUserPasswordHash(username);
         if (OriginalHash == null)
         {
             Console.WriteLine("Unregistered user attempted a connection!");
             return(false);
         }
         Console.WriteLine("SQL Server Hash Recv:" + BitConverter.ToString(OriginalHash));
         if (OriginalHash == null)
         {
             return(false);
         }
         long   myBinData = dateTime;
         byte[] myBytes   = BitConverter.GetBytes(myBinData);
         Console.WriteLine("CLIENT HASH:" + BitConverter.ToString(v));
         Console.WriteLine("TIME STAMP RECEIVED:" + BitConverter.ToString(myBytes));
         byte[] FinalBytes = new byte[OriginalHash.Length + myBytes.Length];
         Array.Copy(myBytes, FinalBytes, myBytes.Length);
         Array.Copy(OriginalHash, 0, FinalBytes, myBytes.Length, OriginalHash.Length);
         Console.WriteLine("PREHASH:" + BitConverter.ToString(FinalBytes));
         byte[] myFinal = mySha.ComputeHash(FinalBytes);
         Console.WriteLine("GENERATED HASH:" + BitConverter.ToString(myFinal));
         for (int i = 0; i < myFinal.Length; i++)
         {
             if (myFinal[i] != v[i])
             {
                 return(false);
             }
         }
         return(true);
     }
 }
Esempio n. 15
0
        // ReSharper disable AccessToStaticMemberViaDerivedType
        private void setHashAlgorithm()
        {
            switch (cbAlgo.SelectedIndex)
            {
            case 0:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = SHA256.Create();
                    break;

                case 1: _ha = SHA256Cng.Create();
                    break;

                case 2: _ha = HMACSHA256.Create();
                    break;
                }
                break;

            case 1:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = SHA512.Create();
                    break;

                case 1: _ha = SHA512Cng.Create();
                    break;

                case 2: _ha = HMACSHA512.Create();
                    break;
                }
                break;

            case 2:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = SHA1.Create();
                    break;

                case 1: _ha = SHA1Cng.Create();
                    break;

                case 2: _ha = HMACSHA1.Create();
                    break;
                }
                break;

            case 3:
                switch (cbOption.SelectedIndex)
                {
                case 0: _ha = MD5.Create();
                    break;

                case 1: _ha = MD5Cng.Create();
                    break;

                case 2: _ha = HMACMD5.Create();
                    break;
                }
                break;

            case 4:
                //stays null for Modbus-CRC16
                break;

            case 5:
                _ha = new Crc32();
                break;
            }
        }
Esempio n. 16
0
File: OAEP.cs Progetto: Unchuris/RSA
 static byte[] HashAlgorithm(byte[] input)
 {
     return(SHA256Cng.Create().ComputeHash(input));
 }
Esempio n. 17
0
        /// <summary>ハッシュ(キー無し)サービスプロバイダの生成</summary>
        /// <param name="eha">ハッシュ(キー無し)サービスプロバイダの列挙型</param>
        /// <returns>ハッシュ(キー無し)サービスプロバイダ</returns>
        /// <remarks>
        /// EnumHashAlgorithmから、HashAlgorithmを生成するために追加。
        /// HashAlgorithm.Create(HashNameConst.SHA256) は .NET Core 2 で動作せず。
        /// - KeyedHashAlgorithm.Create("HMACSHA1") throw PNSE (on .NET Core 2
        ///   https://github.com/dotnet/standard/issues/530#issuecomment-375043416
        /// </remarks>
        public static HashAlgorithm CreateHashAlgorithmSP(EnumHashAlgorithm eha)
        {
            // ハッシュ(キー無し)サービスプロバイダ
            HashAlgorithm ha = null;

            if (eha == EnumHashAlgorithm.Default)
            {
                // 既定の暗号化サービスプロバイダ
                ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703)
            }

            #region MD5
            else if (eha == EnumHashAlgorithm.MD5_CSP)
            {
                // MD5CryptoServiceProviderサービスプロバイダ
                ha = MD5CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.MD5_CNG)
            {
                // MD5Cngサービスプロバイダ
                ha = MD5Cng.Create(); // devps(1703)
            }
#endif
            #endregion

            #region RIPEMD160
            else if (eha == EnumHashAlgorithm.RIPEMD160_M)
            {
#if NETSTD
                ha = null; // BouncyCastleを使用する。
#else
                // RIPEMD160Managedサービスプロバイダ
                ha = RIPEMD160Managed.Create(); // devps(1703)
#endif
            }
            #endregion

            #region SHA1
            else if (eha == EnumHashAlgorithm.SHA1_CSP)
            {
                // SHA1CryptoServiceProviderサービスプロバイダ
                ha = SHA1CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA1_CNG)
            {
                // SHA1Cngサービスプロバイダ
                ha = SHA1Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA1_M)
            {
                // SHA1Managedサービスプロバイダ
                ha = SHA1Managed.Create(); // devps(1703)
            }
            #endregion

            #region SHA256
            else if (eha == EnumHashAlgorithm.SHA256_CSP)
            {
                // SHA256CryptoServiceProviderサービスプロバイダ
                ha = SHA256CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA256_CNG)
            {
                // SHA256Cngサービスプロバイダ
                ha = SHA256Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA256_M)
            {
                // SHA256Managedサービスプロバイダ
                ha = SHA256Managed.Create(); // devps(1703)
            }
            #endregion

            #region SHA384
            else if (eha == EnumHashAlgorithm.SHA384_CSP)
            {
                // SHA384CryptoServiceProviderサービスプロバイダ
                ha = SHA384CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA384_CNG)
            {
                // SHA384Cngサービスプロバイダ
                ha = SHA384Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA384_M)
            {
                // SHA384Managedサービスプロバイダ
                ha = SHA384Managed.Create(); // devps(1703)
            }
            #endregion

            #region SHA512
            else if (eha == EnumHashAlgorithm.SHA512_CSP)
            {
                // SHA512CryptoServiceProviderサービスプロバイダ
                ha = SHA512CryptoServiceProvider.Create(); // devps(1703)
            }
#if NETSTD
#else
            else if (eha == EnumHashAlgorithm.SHA512_CNG)
            {
                // SHA512Cngサービスプロバイダ
                ha = SHA512Cng.Create(); // devps(1703)
            }
#endif
            else if (eha == EnumHashAlgorithm.SHA512_M)
            {
                // SHA512Managedサービスプロバイダ
                ha = SHA512Managed.Create(); // devps(1703)
            }
            #endregion

            else
            {
                // 既定の暗号化サービスプロバイダ
                ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703)
            }

            return(ha);
        }