• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Cryptography.HMAC类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



HMAC类属于System.Security.Cryptography命名空间,在下文中一共展示了HMAC类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: MerchantTerminal

 public MerchantTerminal(string GatewayID, string TerminalPassword, string HMACKeyID, string HMACKey)
 {
     mGatewayID = GatewayID;
     mTerminalPassword = TerminalPassword;
     mHMACKeyID = HMACKeyID;
     mHMACEncryptionClient = new HMACSHA1(Encoding.UTF8.GetBytes(HMACKey));
 }
开发者ID:cgatno,项目名称:GlobalGatewaye4Wrapper,代码行数:7,代码来源:MerchantTerminal.cs


示例2: Compute_PHash

        static byte[] Compute_PHash(int bytes, byte[][] seeds, HMAC hmac, int blockSize)
        {
            int blocks = (bytes / blockSize) + (bytes % blockSize == 0 ? 0 : 1);
            byte[] ret = new byte[blockSize * blocks];
            byte[] prev = null;

            for (int i = 0; i < blocks; i++) {
                hmac.Initialize ();
                if (prev == null) {
                    for (int q = 0; q < seeds.Length; q ++)
                        hmac.TransformBlock (seeds[q], 0, seeds[q].Length, seeds[q], 0);
                } else {
                    hmac.TransformBlock (prev, 0, prev.Length, prev, 0);
                }
                hmac.TransformFinalBlock (Utility.EmptyByteArray, 0, 0);
                prev = hmac.Hash;
                hmac.Initialize ();
                hmac.TransformBlock (prev, 0, prev.Length, prev, 0);
                for (int q = 0; q < seeds.Length; q++)
                    hmac.TransformBlock (seeds[q], 0, seeds[q].Length, seeds[q], 0);
                hmac.TransformFinalBlock (Utility.EmptyByteArray, 0, 0);
                for (int q = 0; q < blockSize; q++)
                    ret[i * blockSize + q] = hmac.Hash[q];
            }
            return ret;
        }
开发者ID:kazuki,项目名称:opencrypto-tls,代码行数:26,代码来源:MD5_AND_SHA1.cs


示例3: DeriveKey

		}// DeriveKey()

		internal static void DeriveKey(HMAC keyedHmac, ArraySegment<byte> bufferSegment, ArraySegment<byte> derivedOutput, uint counter = 1)
		{
			int derivedOutputCount = derivedOutput.Count, derivedOutputOffset = derivedOutput.Offset;
			byte[] K_i = null;
			HMAC2 keyedHmac2 = keyedHmac as HMAC2;
			checked
			{
				// Calculate each K_i value and copy the leftmost bits to the output buffer as appropriate.
				for (var counterStruct = new Utils.IntStruct { UintValue = counter }; derivedOutputCount > 0; ++counterStruct.UintValue)
				{
					counterStruct.ToBEBytes(bufferSegment.Array, bufferSegment.Offset); // update the counter within the buffer

					if (keyedHmac2 == null)
					{
						K_i = keyedHmac.ComputeHash(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count);
					}
					else
					{
						keyedHmac2.TransformBlock(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count, null, 0);
						keyedHmac2.TransformFinalBlock(bufferSegment.Array, 0, 0);
						K_i = keyedHmac2.HashInner;
					}

					// copy the leftmost bits of K_i into the output buffer
					int numBytesToCopy = derivedOutputCount > K_i.Length ? K_i.Length : derivedOutputCount;//Math.Min(derivedOutputCount, K_i.Length);
					Utils.BlockCopy(K_i, 0, derivedOutput.Array, derivedOutputOffset, numBytesToCopy);
					derivedOutputOffset += numBytesToCopy;
					derivedOutputCount -= numBytesToCopy;
				}// for
			}// checked
			if (keyedHmac2 == null && K_i != null) Array.Clear(K_i, 0, K_i.Length); /* clean up needed only when HMAC implementation is not HMAC2 */
		}// DeriveKey()
开发者ID:sdrapkin,项目名称:SecurityDriven.Inferno,代码行数:34,代码来源:SP800_108_Ctr.cs


示例4: HMACBuildInAdapter

        public HMACBuildInAdapter(HMAC a_hmac, int a_blockSize)
            : base(a_hmac.HashSize / 8, a_blockSize)
        {
            Debug.Assert(a_hmac != null);

            m_hmac = a_hmac;
        }
开发者ID:art-drobanov,项目名称:FBICRY,代码行数:7,代码来源:HMACBuildInAdapter.cs


示例5: NistSP800108DeriveBytes

        /// <summary>
        /// Initializes a new instance of the NistSP800108DeriveBytes using specified algorithm.
        /// </summary>
        /// <param name="masterKey">The master key to derive from.</param>
        /// <param name="label">The primary purpose string.</param>
        /// <param name="context">The secondary purpose strings.</param>
        /// <param name="pseudoRandomFunction">The HMAC function to use as PRF.</param>
        public NistSP800108DeriveBytes(byte[] masterKey, string label, string[] context, HMAC pseudoRandomFunction) {
            // Validate arguments
            if (masterKey == null) throw new ArgumentNullException("masterKey");
            if (masterKey.Length == 0) throw new ArgumentException("The argument cannot be empty.", "masterKey");
            if (label == null) throw new ArgumentNullException("label");
            if (string.IsNullOrWhiteSpace(label)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "label");
            if (pseudoRandomFunction == null) throw new ArgumentNullException("pseudoRandomFunction");

            // Setup internal parameters
            this.pseudoRandomFunction = pseudoRandomFunction;
            this.pseudoRandomFunction.Key = masterKey;

            // Convert label and context to byte arrays
            var safeUtf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
            this.labelBytes = safeUtf8.GetBytes(label);
            if(context== null || context.Length > 0) {
                this.contextBytes = new byte[0];
            }
            else {
                using (MemoryStream stream = new MemoryStream())
                using (BinaryWriter writer = new BinaryWriter(stream, safeUtf8)) {
                    foreach (string item in context) {
                        if (string.IsNullOrWhiteSpace(item)) continue;  // Skip empty context item
                        writer.Write(item);
                    }
                    this.contextBytes = stream.ToArray();
                }
            }
        }
开发者ID:holajan,项目名称:dotvvm,代码行数:36,代码来源:NistSP800108DeriveBytes.cs


示例6: HKDF

 public HKDF(Func<HMAC> hmacFactory, byte[] ikm, byte[] salt = null, byte[] context = null)
 {
     hmac = hmacFactory();
     hashLength = hmac.OutputBlockSize;
     hmac.Key = salt ?? new byte[hashLength];
     hmac.Key = hmac.ComputeHash(ikm); // re-keying hmac with PRK
     this.context = context;
     Reset();
 }
开发者ID:PerplexInternetmarketing,项目名称:PerplexMail-for-Umbraco,代码行数:9,代码来源:HKDF.cs


示例7: PBKDF2

		}//ctor

		/// <summary>
		/// ctor
		/// </summary>
		/// <param name="password"></param>
		/// <param name="salt"></param>
		/// <param name="iterations"></param>
		public PBKDF2(Func<HMAC> hmacFactory, byte[] password, byte[] salt, int iterations)
		{
			this.Salt = salt;
			this.IterationCount = iterations;
			this.hmac = hmacFactory();
			this.hmac.Key = password;
			this.BlockSize = hmac.HashSize / 8;
			this.Initialize();
		}//ctor
开发者ID:henning-krause,项目名称:SecurityDriven.Inferno,代码行数:17,代码来源:PBKDF2.cs


示例8: EnableReceiveCipher

 public void EnableReceiveCipher(ICryptoTransform decryptor, HMAC recvHMAC)
 {
     _decryptor = decryptor;
     _recvHMAC = recvHMAC;
     if (_recordType == RecordState.PlainText)
         _recordType = RecordState.CipherTextReceiveOnly;
     else
         _recordType = RecordState.CipherText;
 }
开发者ID:kazuki,项目名称:opencrypto-tls,代码行数:9,代码来源:RecordLayer.cs


示例9: HKDF

        /// <summary>
        /// Initializes a new instance of the <see cref="HKDF"/> class.
        /// </summary>
        /// <param name="hmac">The HMAC hash function to use.</param>
        /// <param name="ikm">input keying material.</param>
        /// <param name="salt">optional salt value (a non-secret random value); if not provided, it is set to a string of HMAC.HashSize/8 zeros.</param>
        public HKDF(HMAC hmac, byte[] ikm, byte[] salt = null)
        {
            this.hmac = hmac;
            this.hashLength = hmac.HashSize / 8;

            // now we compute the PRK
            hmac.Key = salt ?? new byte[this.hashLength];
            this.prk = hmac.ComputeHash(ikm);
        }
开发者ID:crowleym,项目名称:HKDF,代码行数:15,代码来源:HKDF.cs


示例10: GetAlgorithmParameters

        private static void GetAlgorithmParameters( string algorithm, byte[] key, out byte[] aes_key, out byte[] hmac_key, out HMAC hmac )
        {
            switch ( algorithm )
            {
                case Aes128CbcHmacSha256.AlgorithmName:
                    {
                        if ( ( key.Length << 3 ) < 256 )
                            throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length in bits {1} < 256", algorithm, key.Length << 3 ) );

                        hmac_key = new byte[128 >> 3];
                        aes_key  = new byte[128 >> 3];
                        Array.Copy( key, hmac_key, 128 >> 3 );
                        Array.Copy( key, 128 >> 3, aes_key, 0, 128 >> 3 );

                        hmac = new HMACSHA256( hmac_key );

                        break;
                    }

                case Aes192CbcHmacSha384.AlgorithmName:
                    {
                        if ( ( key.Length << 3 ) < 384 )
                            throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length in bits {1} < 384", algorithm, key.Length << 3 ) );

                        hmac_key = new byte[192 >> 3];
                        aes_key  = new byte[192 >> 3];
                        Array.Copy( key, hmac_key, 192 >> 3 );
                        Array.Copy( key, 192 >> 3, aes_key, 0, 192 >> 3 );

                        hmac = new HMACSHA384( hmac_key );

                        break;
                    }

                case Aes256CbcHmacSha512.AlgorithmName:
                    {
                        if ( ( key.Length << 3 ) < 512 )
                            throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length in bits {1} < 512", algorithm, key.Length << 3 ) );

                        hmac_key = new byte[256 >> 3];
                        aes_key  = new byte[256 >> 3];
                        Array.Copy( key, hmac_key, 256 >> 3 );
                        Array.Copy( key, 256 >> 3, aes_key, 0, 256 >> 3 );

                        hmac = new HMACSHA512( hmac_key );

                        break;
                    }

                default:
                    {
                        throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "Unsupported algorithm: {0}", algorithm ) );
                    }
            }
        }
开发者ID:theadriangreen,项目名称:azure-sdk-for-net,代码行数:55,代码来源:AesCbcHmacSha2.cs


示例11: PBKDF2

 /// <summary>
 /// Creates new instance.
 /// </summary>
 /// <param name="algorithm">HMAC algorithm to use.</param>
 /// <param name="input">The input used to derive the key.</param>
 /// <param name="salt">The key salt used to derive the key.</param>
 /// <param name="iterations">The number of iterations for the operation.</param>
 /// <exception cref="System.ArgumentNullException">Algorithm cannot be null - Password cannot be null. -or- Salt cannot be null.</exception>
 public PBKDF2(HMAC algorithm, Byte[] input, Byte[] salt, int iterations) {
     if (algorithm == null) { throw new ArgumentNullException("algorithm", "Algorithm cannot be null."); }
     if (salt == null) { throw new ArgumentNullException("salt", "Salt cannot be null."); }
     if (input == null) { throw new ArgumentNullException("input", "input cannot be null."); }
     this.Algorithm = algorithm;
     this.Algorithm.Key = input;
     this.Salt = salt;
     this.Iterations = iterations;
     this.BlockSize = 16;// this.Algorithm.HashSize / 8;
     this.BufferBytes = new byte[this.BlockSize];
 }
开发者ID:MagicWishMonkey,项目名称:aoi.net,代码行数:19,代码来源:PBKDF2.cs


示例12: MainForm

 public MainForm()
 {
     InitializeComponent();
     cmbProviders.Items.AddRange(cryptoServices);
     openFileDialog1.FileOk += openFileDialog1_FileOk;
     Load += MainForm_Load;
     FormClosing += MainForm_FormClosing;
     Application.ApplicationExit += Application_ApplicationExit;
     m_macAlgorithm = new HMACMD5();
     m_rsaHelper = new RSASignHelper();
 }
开发者ID:sviatsviatsviat,项目名称:Shields,代码行数:11,代码来源:MainForm.cs


示例13: init

 public void init(byte[] key)
 {
     if (key.Length > BSIZE)
     {
         byte[] tmp = new byte[BSIZE];
         Array.Copy(key, 0, tmp, 0, BSIZE);
         key = tmp;
     }
     mac = new SSC.HMACSHA1(key);
     cs = new SSC.CryptoStream(Stream.Null,mac,SSC.CryptoStreamMode.Write);
 }
开发者ID:JamesHagerman,项目名称:sftprelay,代码行数:11,代码来源:HMACSHA1.cs


示例14: HKDF

		public HKDF(Func<HMAC> hmacFactory, byte[] ikm, byte[] salt = null, byte[] context = null)
		{
			hmac = hmacFactory();
			hashLength = hmac.OutputBlockSize;

			// a malicious implementation of HMAC could conceivably mess up the shared static empty byte arrays, which are still writeable...
			hmac.Key = salt ?? (hashLength == 64 ? emptyArray64 : hashLength == 48 ? emptyArray48 : hashLength == 32 ? emptyArray32 : hashLength == 20 ? emptyArray20 : new byte[hashLength]);
			hmac.Key = hmac.ComputeHash(ikm); // re-keying hmac with PRK
			this.context = context;
			Reset();
		}
开发者ID:beachandbytes,项目名称:SecurityDriven.Inferno,代码行数:11,代码来源:HKDF.cs


示例15: init

        public void init(byte[] key)
        {
            if (key.Length > 20)
            {
                byte[] tmp = new byte[20];
                Array.Copy(key, 0, tmp, 0, 20);
                key = tmp;
            }

            mentalis_mac = new System.Security.Cryptography.HMACSHA1(key);
            cs = new CryptoStream(Stream.Null, mentalis_mac, CryptoStreamMode.Write);
        }
开发者ID:akrisiun,项目名称:SharpSSH,代码行数:12,代码来源:HMACSHA196.cs


示例16: Pbkdf2

 public Pbkdf2(HMAC algorithm, Byte[] password, Byte[] salt, Int32 iterations)
 {
     if (algorithm == null) { throw new ArgumentNullException("algorithm", "HMAC algorithm cannot be null."); }
     if (salt == null) { throw new ArgumentNullException("salt", "Salt cannot be null."); }
     if (password == null) { throw new ArgumentNullException("password", "Password cannot be null."); }
     this.hmacAlg = algorithm;
     this.hmacAlg.Key = password;
     this.salt = salt;
     this.itercount = iterations;
     this.blocksize = this.hmacAlg.HashSize / 8;
     this.baBuffer = new byte[this.blocksize];
 }
开发者ID:3kitkohls,项目名称:CS-SESAM,代码行数:12,代码来源:Pbkdf2.cs


示例17: GetHashAlgorithm

 /// <summary>
 /// Get the hash algorithm and hmac algorithm for V2.
 /// </summary>
 /// <param name="hashAlgo">The hash algorithm value</param>
 /// <returns>The hash algorithm and hmac algorithm</returns>
 public static void GetHashAlgorithm(dwHashAlgoV2_Values hashAlgo, out HashAlgorithm hashAlgorithm, out HMAC hmacAlgorithm)
 {
     switch (hashAlgo)
     {
         case dwHashAlgoV2_Values.TRUNCATED_SHA512:
             hashAlgorithm = HashAlgorithm.Create("SHA512");
             hmacAlgorithm = HMAC.Create("HMACSHA512");
             break;
         default:
             throw new NotImplementedException();
     }
 }
开发者ID:Microsoft,项目名称:WindowsProtocolTestSuites,代码行数:17,代码来源:PccrcUtility.cs


示例18: F

        private static byte[] F(byte[] salt, int iterationCount, int blockIndex, HMAC prf)
        {            
            byte[] U = prf.ComputeHash(Arrays.Concat(salt, Arrays.IntToBytes(blockIndex))); // U_1 = PRF (P, S || INT (i))
            byte[] result = U;

            for(int i=2;i<=iterationCount;i++)
            {
                U = prf.ComputeHash(U);                                                     // U_c = PRF (P, U_{c-1}) .                
                result = Arrays.Xor(result, U);                                             // U_1 \xor U_2 \xor ... \xor U_c
            }

            return result;
        }
开发者ID:XinicsInc,项目名称:jose-jwt,代码行数:13,代码来源:PBKDF2.cs


示例19: Pbkdf2

        /// <summary>
        /// Creates new instance.
        /// </summary>
        /// <param name="algorithm">HMAC algorithm to use.</param>
        /// <param name="password">The password used to derive the key.</param>
        /// <param name="salt">The key salt used to derive the key.</param>
        /// <param name="iterations">The number of iterations for the operation.</param>
        /// <exception cref="System.ArgumentNullException">Algorithm cannot be null - Password cannot be null. -or- Salt cannot be null.</exception>
        public Pbkdf2(HMAC algorithm, byte[] password, byte[] salt, int iterations)
        {
            if (algorithm == null) throw new ArgumentNullException(nameof(algorithm), "Algorithm cannot be null.");
            if (password == null) throw new ArgumentNullException(nameof(password), "Password cannot be null.");
            if (salt == null) throw new ArgumentNullException(nameof(salt), "Salt cannot be null.");

            this.Algorithm = algorithm;
            this.Algorithm.Key = password;
            this.Salt = salt;
            this.IterationCount = iterations;

            _blockSize = this.Algorithm.HashSize / 8;
        }
开发者ID:Alliance-Network,项目名称:Library,代码行数:21,代码来源:Pbkdf2.cs


示例20: PBKDF2

 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="password">password</param>
 /// <param name="saltSize">saltSize</param>
 /// <param name="iterations">iterations</param>
 public PBKDF2(Func<HMAC> hmacFactory, string password, int saltSize, int iterations)
 {
     if (saltSize < 0)
     {
         throw new ArgumentOutOfRangeException("saltSize");
     }
     byte[] data = new byte[saltSize];
     rng.NextBytes(data);
     this.Salt = data;
     this.IterationCount = iterations;
     this.hmac = hmacFactory();
     this.hmac.Key = password.SerializeToBytes();
     this.BlockSize = hmac.HashSize / 8;
     this.Initialize();
 }
开发者ID:PerplexInternetmarketing,项目名称:PerplexMail-for-Umbraco,代码行数:21,代码来源:PBKDF2.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Cryptography.HMACMD5类代码示例发布时间:2022-05-26
下一篇:
C# Cryptography.DSAParameters类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap