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


C# HMACMD5.Clear方法代码示例

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


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

示例1: EncodeFile

 // Computes a keyed hash for a source file, creates a target file with the keyed hash
 // prepended to the contents of the source file, then decrypts the file and compares
 // the source and the decrypted files.
 /// <summary>
 ///  文件加密
 /// </summary>
 /// <param name="key"></param>
 /// <param name="sourceFile">待加密的文件</param>
 /// <param name="destFile">加密后的文件</param>
 public static void EncodeFile(byte[] key, String sourceFile, String destFile)
 {
     // Initialize the keyed hash object.
     HMACMD5 myhmacMD5 = new HMACMD5(key);
     FileStream inStream = new FileStream(sourceFile, FileMode.Open);
     FileStream outStream = new FileStream(destFile, FileMode.Create);
     // Compute the hash of the input file.
     byte[] hashValue = myhmacMD5.ComputeHash(inStream);
     // Reset inStream to the beginning of the file.
     inStream.Position = 0;
     // Write the computed hash value to the output file.
     outStream.Write(hashValue, 0, hashValue.Length);
     // Copy the contents of the sourceFile to the destFile.
     int bytesRead;
     // read 1K at a time
     byte[] buffer = new byte[1024];
     do
     {
         // Read from the wrapping CryptoStream.
         bytesRead = inStream.Read(buffer, 0, 1024);
         outStream.Write(buffer, 0, bytesRead);
     } while (bytesRead > 0);
     myhmacMD5.Clear();
     // Close the streams
     inStream.Close();
     outStream.Close();
     return;
 }
开发者ID:shi5588,项目名称:shi5588,代码行数:37,代码来源:Program.cs

示例2: Compute_NTLMv2

		static byte[] Compute_NTLMv2 (Type2Message type2, string username, string password, string domain)
		{
			var ntlm_hash = Compute_NTLM_Password (password);

			var ubytes = Encoding.Unicode.GetBytes (username.ToUpperInvariant ());
			var tbytes = Encoding.Unicode.GetBytes (domain);

			var bytes = new byte [ubytes.Length + tbytes.Length];
			ubytes.CopyTo (bytes, 0);
			Array.Copy (tbytes, 0, bytes, ubytes.Length, tbytes.Length);

			var md5 = new HMACMD5 (ntlm_hash);
			var ntlm_v2_hash = md5.ComputeHash (bytes);

			Array.Clear (ntlm_hash, 0, ntlm_hash.Length);
			md5.Clear ();

			var ntlm_v2_md5 = new HMACMD5 (ntlm_v2_hash);

			var now = DateTime.Now;
			var timestamp = now.Ticks - 504911232000000000;
			
			var nonce = new byte [8];
			var rng = RandomNumberGenerator.Create ();
			rng.GetBytes (nonce);
			
			byte[] blob = new byte [28 + type2.TargetInfo.Length];
			blob[0] = 0x01;
			blob[1] = 0x01;

			Buffer.BlockCopy (BitConverterLE.GetBytes (timestamp), 0, blob, 8, 8);

			Buffer.BlockCopy (nonce, 0, blob, 16, 8);
			Buffer.BlockCopy (type2.TargetInfo, 0, blob, 28, type2.TargetInfo.Length);

			var challenge = type2.Nonce;

			var hashInput = new byte [challenge.Length + blob.Length];
			challenge.CopyTo (hashInput, 0);
			blob.CopyTo (hashInput, challenge.Length);

			var blobHash = ntlm_v2_md5.ComputeHash (hashInput);

			var response = new byte [blob.Length + blobHash.Length];
			blobHash.CopyTo (response, 0);
			blob.CopyTo (response, blobHash.Length);

			Array.Clear (ntlm_v2_hash, 0, ntlm_v2_hash.Length);
			ntlm_v2_md5.Clear ();
			Array.Clear (nonce, 0, nonce.Length);
			Array.Clear (blob, 0, blob.Length);
			Array.Clear (hashInput, 0, hashInput.Length);
			Array.Clear (blobHash, 0, blobHash.Length);

			return response;
		}
开发者ID:ItsVeryWindy,项目名称:mono,代码行数:56,代码来源:ChallengeResponse2.cs

示例3: ComputeHash

        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="data">The scope data.</param>
        /// <param name="privacy">The privacy provider.</param>
        /// <param name="length">The length bytes.</param>
        /// <returns></returns>
        public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy, byte[] length)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            
            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            var key = PasswordToKey(_password, parameters.EngineId.GetRaw());
            using (var md5 = new HMACMD5(key))
            {
                var hash = md5.ComputeHash(ByteTool.PackMessage(length, version, header, parameters, data).ToBytes());
                md5.Clear();
                var result = new byte[DigestLength];
                Buffer.BlockCopy(hash, 0, result, 0, result.Length);
                return new OctetString(result);
            }
        }
开发者ID:bleissem,项目名称:sharpsnmplib,代码行数:42,代码来源:MD5AuthenticationProvider.cs

示例4: EncodeFile

        /// <summary>
        /// Computes a keyed hash for a source file, creates a target file with the keyed hash
        /// prepended to the contents of the source file, then decrypts the file and compares
        /// the source and the decrypted files.
        /// </summary>
        /// <param name="key">The key to use to encode the file</param>
        /// <param name="sourceFile">The file to encrypt complete path</param>
        /// <param name="destFile">Destination file complete path. If the file doesn't exist, it creates it</param>
        public void EncodeFile(string key, String sourceFile, String destFile)
        {
            if (sourceFile.IsNullOrWhiteSpace() || !File.Exists(sourceFile))
                throw new FileNotFoundException("Cannot find the specified source file", sourceFile ?? "null");

            if (destFile.IsNullOrWhiteSpace())
                throw new ArgumentException("Please specify the path of the output path", nameof(destFile));

            if (string.IsNullOrEmpty(key))
                throw new ArgumentException("Please specify the key", nameof(key));

            // Create a key using a random number generator. This would be the
            //  secret key shared by sender and receiver.
            byte[] secretkey = key.ToByteArray();

            // Initialize the keyed hash object.
            HMACMD5 myhmacMD5 = new HMACMD5(secretkey);
            FileStream inStream = new FileStream(sourceFile, FileMode.Open);
            FileStream outStream = new FileStream(destFile, FileMode.Create);
            // Compute the hash of the input file.
            byte[] hashValue = myhmacMD5.ComputeHash(inStream);
            // Reset inStream to the beginning of the file.
            inStream.Position = 0;
            // Write the computed hash value to the output file.
            outStream.Write(hashValue, 0, hashValue.Length);
            // Copy the contents of the sourceFile to the destFile.
            int bytesRead;
            // read 1K at a time
            byte[] buffer = new byte[1024];
            do
            {
                // Read from the wrapping CryptoStream.
                bytesRead = inStream.Read(buffer, 0, 1024);
                outStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
            myhmacMD5.Clear();
            // Close the streams
            inStream.Close();
            outStream.Close();
        }
开发者ID:n3wt0n,项目名称:Crypto,代码行数:48,代码来源:HMACMD5.cs

示例5: ComputeHash

		/// <summary>
		/// Computes the hash.
		/// </summary>
		/// <param name="version">The version.</param>
		/// <param name="header">The header.</param>
		/// <param name="parameters">The parameters.</param>
		/// <param name="scopeBytes">The scope bytes.</param>
		/// <param name="privacy">The privacy provider.</param>
		/// <returns></returns>
		private OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData scopeBytes, IPrivacyProvider privacy)
		{
			if (scopeBytes == null)
			{
				throw new ArgumentNullException("scopeBytes");
			}

			byte[] key = PasswordToKey(_password, parameters.EngineId.GetRaw());
#if ! SILVERLIGHT  //mc++
			using (HMACMD5 md5 = new HMACMD5(key))
			{
				byte[] hash = md5.ComputeHash(SnmpMessageExtension.PackMessage(version, header, parameters, scopeBytes, privacy).ToBytes());
				md5.Clear();
				byte[] result = new byte[DigestLength];
				Array.Copy(hash, result, result.Length);
				return new OctetString(result);
			}
#endif
			return null;
		}
开发者ID:moljac,项目名称:MonoMobile.SharpSNMP,代码行数:29,代码来源:MD5AuthenticationProvider.cs


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