当前位置: 首页>>代码示例>>C#>>正文


C# SHA256Managed.Dispose方法代码示例

本文整理汇总了C#中System.Security.Cryptography.SHA256Managed.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# SHA256Managed.Dispose方法的具体用法?C# SHA256Managed.Dispose怎么用?C# SHA256Managed.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.SHA256Managed的用法示例。


在下文中一共展示了SHA256Managed.Dispose方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Encrypt

        public override string Encrypt(string data)
        {
            SHA256Managed sha256hasher = new SHA256Managed();

            try
            {
                UTF8Encoding encoder = new UTF8Encoding();

                byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(string.Format(base.hashFormat, data)));

                return Convert.ToBase64String(hashedDataBytes);
            }
            catch (Exception e)
            {
                var exception = new CryptologyException("SHA256Encryptor.Encrypt()", "An error occurred while encrypting.", e);
                exception.Data.Add("hashFormat", hashFormat);
                exception.Data.Add("data", data);

                throw exception;
            }
            finally
            {
                sha256hasher.Dispose();
            }
        }
开发者ID:btungut,项目名称:Cruder.Net,代码行数:25,代码来源:SHA256Cryptology.cs

示例2: FillRecord

    private static SqlDataRecord FillRecord(Int32 pk, SqlDataRecord record)
    {
        Int32 age = SlowRandom(16, 99);
        string sourceString = "Age: " + age.ToString();
        DateTime sourceDate = DateTime.UtcNow;

        var data = /*salt + */sourceString;
                
        string key = "Top Secret Key";

        var encData = AES.EncryptBytes(data, key);
        //var encDataBytes = Encoding.Unicode.GetBytes(encData);
        var decData = AES.DecryptBytes(encData, key);

        var sha = new SHA256Managed();
        byte[] dataSHA256 = sha.ComputeHash(encData/*Bytes*/);
        sha.Dispose();

        // конвертирую хеш из byte[16] в строку шестнадцатиричного формата
        // (вида «3C842B246BC74D28E59CCD92AF46F5DA»)
        // это опциональный этап, если вам хеш нужен в строковом виде
        // string sha512hex = BitConverter.ToString(dataSHA512).Replace("-", string.Empty); 

        record.SetInt32(0, pk);
        record.SetDateTime(1, sourceDate);        
        record.SetString(2, sourceString);
        record.SetString(3, Convert.ToBase64String(dataSHA256)); // sha256
        record.SetString(4, Convert.ToBase64String(encData)); // Encrypted
        record.SetString(5, decData); // Decrypted

        return record;
    }
开发者ID:CatBayun,项目名称:WCF.First-Steps,代码行数:32,代码来源:SQLExtentions.cs

示例3: EncryptSettingsProvider

        public EncryptSettingsProvider()
        {
            //read settings from configuration
            var useHashingString = ConfigurationManager.AppSettings["UseHashingForEncryption"];
            var useHashing = System.String.Compare(useHashingString, "false", System.StringComparison.OrdinalIgnoreCase) != 0;

            _encryptionPrefix = ConfigurationManager.AppSettings["EncryptionPrefix"];
            if (string.IsNullOrWhiteSpace(_encryptionPrefix))
            {
                _encryptionPrefix = "encryptedHidden_";
            }

            var key = ConfigurationManager.AppSettings["EncryptionKey"];
            if (useHashing)
            {
                var hash = new SHA256Managed();
                _encryptionKey = hash.ComputeHash(Encoding.UTF8.GetBytes(key));
                hash.Clear();
                hash.Dispose();
            }
            else
            {
                _encryptionKey = Encoding.UTF8.GetBytes(key);
            }
        }
开发者ID:rabbal,项目名称:Decision,代码行数:25,代码来源:EncryptSettingsProvider.cs

示例4: VerifySignedData

        /// <summary>
        /// A method is used to validate the signed data by using specified public key.
        /// </summary>
        /// <param name="signedData">A parameter represents the signed data which will be validate.</param>
        /// <param name="originalData">A parameter represents the original data which is used to execute the validation.</param>
        /// <param name="publicKeyBlob">A parameter represents the binaries data of the public key part of a unique key-pairs.</param>
        /// <returns>Return 'true' indicating the signed data pass the validation.</returns>
        private static bool VerifySignedData(byte[] signedData, byte[] originalData, byte[] publicKeyBlob)
        {
            if (null == signedData || 0 == signedData.Length)
            {
                throw new ArgumentNullException("signedData");
            }

            if (null == originalData || 0 == originalData.Length)
            {
                throw new ArgumentNullException("originalData");
            }

            if (null == publicKeyBlob || 0 == publicKeyBlob.Length)
            {
                throw new ArgumentNullException("publicKeyBlob");
            }

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                rsaProvider.ImportCspBlob(publicKeyBlob);
                SHA256Managed sha = new SHA256Managed();
                bool result = rsaProvider.VerifyData(originalData, sha, signedData);
                sha.Dispose();
                return result;
            }
        }
开发者ID:ClareMSYanGit,项目名称:Interop-TestSuites,代码行数:33,代码来源:RSACryptoContext.cs

示例5: SignData

        /// <summary>
        /// A method is used to sign the data with specified key-pairs. The signed data only pass the validation by using the public key part of the specified key-pairs.
        /// </summary>
        /// <param name="originalData">A parameter represents the binaries data which will be signed with old key-pairs.</param>
        /// <param name="fullkeyBlob">A parameter represents the binaries data of the unique key-pairs which is match the asymmetric encrypt algorithm.</param>
        /// <returns>A return value represents the signed data.</returns>
        private static byte[] SignData(byte[] originalData, byte[] fullkeyBlob)
        {
            if (null == originalData || 0 == originalData.Length)
            {
                throw new ArgumentNullException("originalData");
            }

            if (null == fullkeyBlob || 0 == fullkeyBlob.Length)
            {
                throw new ArgumentNullException("fullkeyBlob");
            }

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                rsaProvider.ImportCspBlob(fullkeyBlob);
                SHA256Managed sha = new SHA256Managed();
                byte[] signedData = rsaProvider.SignData(originalData, sha);
                sha.Dispose();
                return signedData;
            }
        }
开发者ID:ClareMSYanGit,项目名称:Interop-TestSuites,代码行数:27,代码来源:RSACryptoContext.cs

示例6: ComputeHash

            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);
            }
开发者ID:GNCPay,项目名称:backend,代码行数:60,代码来源:AccountController.cs


注:本文中的System.Security.Cryptography.SHA256Managed.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。