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


C# HMAC.ComputeHash方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: 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;
			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
					K_i = keyedHmac.ComputeHash(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count);

					// copy the leftmost bits of K_i into the output buffer
					int numBytesToCopy = Math.Min(derivedOutputCount, K_i.Length);
					Utils.BlockCopy(K_i, 0, derivedOutput.Array, derivedOutputOffset, numBytesToCopy);
					derivedOutputOffset += numBytesToCopy;
					derivedOutputCount -= numBytesToCopy;
				}// for
			}// checked
			if (K_i != null) Array.Clear(K_i, 0, K_i.Length);
		}// DeriveKey()
开发者ID:zpaav,项目名称:SecurityDriven.Inferno,代码行数:23,代码来源:SP800_108_Ctr.cs

示例7: HMACSanityCheck

        private void HMACSanityCheck(HMAC hmac, params byte[] expected)
        {
            // test_case = 2
            string key = "Jefe";
            byte[] keyBytes = Encoding.ASCII.GetBytes(key);

            string data = "what do ya want for nothing?";
            byte[] dataBytes = Encoding.ASCII.GetBytes(data);

            hmac.Key = keyBytes;
            byte[] digest = hmac.ComputeHash(dataBytes);

            CollectionAssert.AreEqual(expected, digest);
        }
开发者ID:modulexcite,项目名称:TLS-1.0-Analyzer,代码行数:14,代码来源:Prf10Tests.cs

示例8: CheckC

		public void CheckC (string testName, HMAC algo, byte[] data, byte[] result)
		{
			using (MemoryStream ms = new MemoryStream (data)) {
				byte[] hmac = algo.ComputeHash (ms);
				Compare (result, hmac, testName + "c1");
				Compare (result, algo.Hash, testName + "c2");
			}
		}
开发者ID:nlhepler,项目名称:mono,代码行数:8,代码来源:HMACSHA384Test.cs

示例9: CheckB

		public void CheckB (string testName, HMAC algo, byte[] data, byte[] result)
		{
			byte[] hmac = algo.ComputeHash (data, 0, data.Length);
			Compare (result, hmac, testName + "b1");
			Compare (result, algo.Hash, testName + "b2");
		}
开发者ID:nlhepler,项目名称:mono,代码行数:6,代码来源:HMACSHA384Test.cs

示例10: CheckA

		public void CheckA (string testName, HMAC algo, byte[] data, byte[] result)
		{
			byte[] hmac = algo.ComputeHash (data);
			Compare (result, hmac, testName + "a1");
			Compare (result, algo.Hash, testName + "a2");
		}
开发者ID:nlhepler,项目名称:mono,代码行数:6,代码来源:HMACSHA384Test.cs

示例11: PRF

        /// <summary>
        /// hmac should be initialized with the secret key
        /// </summary>
        /// <param name="hmac"></param>
        /// <param name="label"></param>
        /// <param name="seed"></param>
        /// <param name="bytesNeeded"></param>
        /// <returns></returns>
        public static byte[] PRF(HMAC hmac, string label, byte[] seed, int bytesNeeded)
        {
            var blockSize = hmac.HashSize / 8;
            var rounds = (bytesNeeded + (blockSize - 1)) / blockSize;

            var labelLen = Encoding.ASCII.GetByteCount(label);
            var a = new byte[labelLen + seed.Length];
            Encoding.ASCII.GetBytes(label, 0, label.Length, a, 0);
            Buffer.BlockCopy(seed, 0, a, labelLen, seed.Length);

            byte[] ret = new byte[rounds * blockSize];
            byte[] input = new byte[blockSize + a.Length];
            Buffer.BlockCopy(a, 0, input, blockSize, a.Length);

            for (var i = 0; i < rounds; i++)
            {
                var aNew = hmac.ComputeHash(a);
                ClearArray(a);
                a = aNew;
                Buffer.BlockCopy(a, 0, input, 0, blockSize);
                byte[] temp = hmac.ComputeHash(input);
                Buffer.BlockCopy(temp, 0, ret, i * blockSize, blockSize);
                ClearArray(temp);
            }
            ClearArray(a);
            ClearArray(input);
            if (bytesNeeded == ret.Length)
                return ret;
            byte[] retTruncated = new byte[bytesNeeded];
            Buffer.BlockCopy(ret, 0, retTruncated, 0, bytesNeeded);
            ClearArray(ret);
            return retTruncated;
        }
开发者ID:Emill,项目名称:Npgsql,代码行数:41,代码来源:Utils.cs

示例12: PHash

        // From section 5 of RFC 2246
        // P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
        //                        HMAC_hash(secret, A(2) + seed) +
        //                        HMAC_hash(secret, A(3) + seed) + ...
        //   A() is defined as:
        //       A(0) = seed
        //       A(i) = HMAC_hash(secret, A(i-1))
        private static byte[] PHash(HMAC hmac, byte[] seed, int bytesToGenerate)
        {
            using (MemoryStream bytesToHashBuffer = new MemoryStream())
            using (MemoryStream output = new MemoryStream())
            {
                byte[] previousA = seed;

                while (output.Length < bytesToGenerate)
                {
                    bytesToHashBuffer.SetLength(0);

                    byte[] currentA = A(hmac, previousA);
                    bytesToHashBuffer.Write(currentA, 0, currentA.Length);
                    bytesToHashBuffer.Write(seed, 0, seed.Length);

                    byte[] currentBuffer = bytesToHashBuffer.GetBuffer();

                    byte[] currentRoundResult = hmac.ComputeHash(currentBuffer, 0, (int)bytesToHashBuffer.Length);
                    output.Write(currentRoundResult, 0, currentRoundResult.Length);
                    previousA = currentA;
                }

                output.SetLength(bytesToGenerate);

                return output.ToArray();
            }
        }
开发者ID:ronghaopger,项目名称:TlsClient,代码行数:34,代码来源:Prf10.cs

示例13: A

 private static byte[] A(HMAC hmac, byte[] aMinus1Result)
 {
     return hmac.ComputeHash(aMinus1Result);
 }
开发者ID:ronghaopger,项目名称:TlsClient,代码行数:4,代码来源:Prf10.cs

示例14: DeriveKeyImpl

        // NOTE: This method also exists in Win8 (as BCryptKeyDerivation) and QTD (as DeriveKeySP800_108).
        // However, the QTD implementation is currently incorrect, so we can't depend on it here. The below
        // is a correct implementation. When we take a Win8 dependency, we can call into BCryptKeyDerivation.
        private static byte[] DeriveKeyImpl(HMAC hmac, byte[] label, byte[] context, int keyLengthInBits) {
            // This entire method is checked because according to SP800-108 it is an error
            // for any single operation to result in overflow.
            checked {

                // Make a buffer which is ____ || label || 0x00 || context || [l]_2.
                // We can reuse this buffer during each round.

                int labelLength = (label != null) ? label.Length : 0;
                int contextLength = (context != null) ? context.Length : 0;
                byte[] buffer = new byte[4 /* [i]_2 */ + labelLength /* label */ + 1 /* 0x00 */ + contextLength /* context */ + 4 /* [L]_2 */];

                if (labelLength != 0) {
                    Buffer.BlockCopy(label, 0, buffer, 4, labelLength); // the 4 accounts for the [i]_2 length
                }
                if (contextLength != 0) {
                    Buffer.BlockCopy(context, 0, buffer, 5 + labelLength, contextLength); // the '5 +' accounts for the [i]_2 length, the label, and the 0x00 byte
                }
                WriteUInt32ToByteArrayBigEndian((uint)keyLengthInBits, buffer, 5 + labelLength + contextLength); // the '5 +' accounts for the [i]_2 length, the label, the 0x00 byte, and the context

                // Initialization

                int numBytesWritten = 0;
                int numBytesRemaining = keyLengthInBits / 8;
                byte[] output = new byte[numBytesRemaining];

                // Calculate each K_i value and copy the leftmost bits to the output buffer as appropriate.

                for (uint i = 1; numBytesRemaining > 0; i++) {
                    WriteUInt32ToByteArrayBigEndian(i, buffer, 0); // set the first 32 bits of the buffer to be the current iteration value
                    byte[] K_i = hmac.ComputeHash(buffer);

                    // copy the leftmost bits of K_i into the output buffer
                    int numBytesToCopy = Math.Min(numBytesRemaining, K_i.Length);
                    Buffer.BlockCopy(K_i, 0, output, numBytesWritten, numBytesToCopy);
                    numBytesWritten += numBytesToCopy;
                    numBytesRemaining -= numBytesToCopy;
                }

                // finished
                return output;
            }
        }
开发者ID:iskiselev,项目名称:JSIL.NetFramework,代码行数:46,代码来源:SP800_108.cs

示例15: HashCounter

        /// <summary>
        /// Hashes the counter using the hmac and secretkey
        /// </summary>
        private static byte[] HashCounter(HMAC hmacGenerator, byte[] secretKey, ulong counter)
        {
            hmacGenerator.Key = secretKey;

            //Spec says 8byte, array
            byte[] counterBytes = new byte[COUNTER_LEGNTH];

            //Bit converter may return an arry with less than 8 bytes
            byte[] intermediateBytes = BitConverter.GetBytes(counter);

            //Copy number-bytes into padded buffer.
            Array.Copy(intermediateBytes, counterBytes, counterBytes.Length);

            if (BitConverter.IsLittleEndian)
            {
                counterBytes = counterBytes.Reverse().ToArray();
            }

            return hmacGenerator.ComputeHash(counterBytes);
        }
开发者ID:flashcurd,项目名称:Shared.Utilities,代码行数:23,代码来源:HmacOneTimePassword.cs


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