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


C# Cryptography.HMAC类代码示例

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


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


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