|
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
|
Tradução
Original
|
Classe KeyedHashAlgorithm
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.KeyedHashAlgorithm
System.Security.Cryptography.HMAC
System.Security.Cryptography.MACTripleDES
Assembly: mscorlib (em mscorlib.dll)
O tipo KeyedHashAlgorithm expõe os membros a seguir.
| Nome | Descrição | |
|---|---|---|
![]() | CanReuseTransform | |
![]() | CanTransformMultipleBlocks | |
![]() | Hash | |
![]() | HashSize | |
![]() | InputBlockSize | |
![]() | Key | |
![]() | OutputBlockSize |
| Nome | Descrição | |
|---|---|---|
![]() | Clear | |
![]() | ComputeHash(Byte[]) | |
![]() | ComputeHash(Stream) | |
![]() | ComputeHash(Byte[], Int32, Int32) | |
![]() ![]() | Create() | |
![]() ![]() | Create(String) | |
![]() | Dispose() | |
![]() | Dispose(Boolean) | |
![]() | Equals(Object) | |
![]() | Finalize | |
![]() | GetHashCode | |
![]() | GetType | |
![]() | HashCore | |
![]() | HashFinal | |
![]() | Initialize | |
![]() | MemberwiseClone | |
![]() | ToString | |
![]() | TransformBlock | |
![]() | TransformFinalBlock |
| Nome | Descrição | |
|---|---|---|
![]() | HashSizeValue | |
![]() | HashValue | |
![]() | KeyValue | |
![]() | State |
using System; using System.Security.Cryptography; public class TestHMACMD5 { static private void PrintByteArray(Byte[] arr) { int i; Console.WriteLine("Length: " + arr.Length); for (i = 0; i < arr.Length; i++) { Console.Write("{0:X}", arr[i]); Console.Write(" "); if ((i + 9) % 8 == 0) Console.WriteLine(); } if (i % 8 != 0) Console.WriteLine(); } public static void Main() { // Create a key. byte[] key1 = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; // Pass the key to the constructor of the HMACMD5 class. HMACMD5 hmac1 = new HMACMD5(key1); // Create another key. byte[] key2 = System.Text.Encoding.ASCII.GetBytes("KeyString"); // Pass the key to the constructor of the HMACMD5 class. HMACMD5 hmac2 = new HMACMD5(key2); // Encode a string into a byte array, create a hash of the array, // and print the hash to the screen. byte[] data1 = System.Text.Encoding.ASCII.GetBytes("Hi There"); PrintByteArray(hmac1.ComputeHash(data1)); // Encode a string into a byte array, create a hash of the array, // and print the hash to the screen. byte[] data2 = System.Text.Encoding.ASCII.GetBytes("This data will be hashed."); PrintByteArray(hmac2.ComputeHash(data2)); } } public class HMACMD5 : KeyedHashAlgorithm { private MD5 hash1; private MD5 hash2; private bool bHashing = false; private byte[] rgbInner = new byte[64]; private byte[] rgbOuter = new byte[64]; public HMACMD5(byte[] rgbKey) { HashSizeValue = 128; // Create the hash algorithms. hash1 = MD5.Create(); hash2 = MD5.Create(); // Get the key. if (rgbKey.Length > 64) { KeyValue = hash1.ComputeHash(rgbKey); // No need to call Initialize; ComputeHash does it automatically. } else { KeyValue = (byte[])rgbKey.Clone(); } // Compute rgbInner and rgbOuter. int i = 0; for (i = 0; i < 64; i++) { rgbInner[i] = 0x36; rgbOuter[i] = 0x5C; } for (i = 0; i < KeyValue.Length; i++) { rgbInner[i] ^= KeyValue[i]; rgbOuter[i] ^= KeyValue[i]; } } public override byte[] Key { get { return (byte[])KeyValue.Clone(); } set { if (bHashing) { throw new Exception("Cannot change key during hash operation"); } if (value.Length > 64) { KeyValue = hash1.ComputeHash(value); // No need to call Initialize; ComputeHash does it automatically. } else { KeyValue = (byte[])value.Clone(); } // Compute rgbInner and rgbOuter. int i = 0; for (i = 0; i < 64; i++) { rgbInner[i] = 0x36; rgbOuter[i] = 0x5C; } for (i = 0; i < KeyValue.Length; i++) { rgbInner[i] ^= KeyValue[i]; rgbOuter[i] ^= KeyValue[i]; } } } public override void Initialize() { hash1.Initialize(); hash2.Initialize(); bHashing = false; } protected override void HashCore(byte[] rgb, int ib, int cb) { if (bHashing == false) { hash1.TransformBlock(rgbInner, 0, 64, rgbInner, 0); bHashing = true; } hash1.TransformBlock(rgb, ib, cb, rgb, ib); } protected override byte[] HashFinal() { if (bHashing == false) { hash1.TransformBlock(rgbInner, 0, 64, rgbInner, 0); bHashing = true; } // Finalize the original hash. hash1.TransformFinalBlock(new byte[0], 0, 0); // Write the outer array. hash2.TransformBlock(rgbOuter, 0, 64, rgbOuter, 0); // Write the inner hash and finalize the hash. hash2.TransformFinalBlock(hash1.Hash, 0, hash1.Hash.Length); bHashing = false; return hash2.Hash; } }
Windows 7, Windows Vista SP1 ou posterior, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core não compatível), Windows Server 2008 R2 (Server Core não compatível com SP1 ou posterior), Windows Server 2003 SP2
O .NET Framework não oferece suporte a todas as versões de cada plataforma. Para obter uma lista das versões com suporte, consulte Requisitos de sistema do .NET Framework.
