KeyedHashAlgorithm 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
显示所有加密哈希算法实现均必须从中派生的抽象类。
public ref class KeyedHashAlgorithm abstract : System::Security::Cryptography::HashAlgorithm
public abstract class KeyedHashAlgorithm : System.Security.Cryptography.HashAlgorithm
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class KeyedHashAlgorithm : System.Security.Cryptography.HashAlgorithm
type KeyedHashAlgorithm = class
inherit HashAlgorithm
[<System.Runtime.InteropServices.ComVisible(true)>]
type KeyedHashAlgorithm = class
inherit HashAlgorithm
Public MustInherit Class KeyedHashAlgorithm
Inherits HashAlgorithm
- 继承
- 派生
- 属性
示例
下面的代码示例演示如何从 KeyedHashAlgorithm 类派生。
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;
}
}
Imports System.Security.Cryptography
_
Public Class TestHMACMD5
Private Shared Sub PrintByteArray(ByVal arr() As [Byte])
Dim i As Integer
Console.WriteLine(("Length: " + arr.Length.ToString()))
For i = 0 To arr.Length - 1
Console.Write("{0:X}", arr(i))
Console.Write(" ")
If (i + 9) Mod 8 = 0 Then
Console.WriteLine()
End If
Next i
If i Mod 8 <> 0 Then
Console.WriteLine()
End If
End Sub
Public Shared Sub Main()
' Create a key.
Dim key1 As Byte() = {&HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB, &HB}
' Pass the key to the constructor of the HMACMD5 class.
Dim hmac1 As New HMACMD5(key1)
' Create another key.
Dim key2 As Byte() = System.Text.Encoding.ASCII.GetBytes("KeyString")
' Pass the key to the constructor of the HMACMD5 class.
Dim hmac2 As New HMACMD5(key2)
' Encode a string into a byte array, create a hash of the array,
' and print the hash to the screen.
Dim data1 As Byte() = 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.
Dim data2 As Byte() = System.Text.Encoding.ASCII.GetBytes("This data will be hashed.")
PrintByteArray(hmac2.ComputeHash(data2))
End Sub
End Class
_
Public Class HMACMD5
Inherits KeyedHashAlgorithm
Private hash1 As MD5
Private hash2 As MD5
Private bHashing As Boolean = False
Private rgbInner(64) As Byte
Private rgbOuter(64) As Byte
Public Sub New(ByVal rgbKey() As Byte)
HashSizeValue = 128
' Create the hash algorithms.
hash1 = MD5.Create()
hash2 = MD5.Create()
' Get the key.
If rgbKey.Length > 64 Then
KeyValue = hash1.ComputeHash(rgbKey)
' No need to call Initialize; ComputeHash does it automatically.
Else
KeyValue = CType(rgbKey.Clone(), Byte())
End If
' Compute rgbInner and rgbOuter.
Dim i As Integer = 0
For i = 0 To 63
rgbInner(i) = &H36
rgbOuter(i) = &H5C
Next i
i = 0
For i = 0 To KeyValue.Length - 1
rgbInner(i) = rgbInner(i) Xor KeyValue(i)
rgbOuter(i) = rgbOuter(i) Xor KeyValue(i)
Next i
End Sub
Public Overrides Property Key() As Byte()
Get
Return CType(KeyValue.Clone(), Byte())
End Get
Set(ByVal Value As Byte())
If bHashing Then
Throw New Exception("Cannot change key during hash operation")
End If
If value.Length > 64 Then
KeyValue = hash1.ComputeHash(value)
' No need to call Initialize; ComputeHash does it automatically.
Else
KeyValue = CType(value.Clone(), Byte())
End If
' Compute rgbInner and rgbOuter.
Dim i As Integer = 0
For i = 0 To 63
rgbInner(i) = &H36
rgbOuter(i) = &H5C
Next i
For i = 0 To KeyValue.Length - 1
rgbInner(i) ^= KeyValue(i)
rgbOuter(i) ^= KeyValue(i)
Next i
End Set
End Property
Public Overrides Sub Initialize()
hash1.Initialize()
hash2.Initialize()
bHashing = False
End Sub
Protected Overrides Sub HashCore(ByVal rgb() As Byte, ByVal ib As Integer, ByVal cb As Integer)
If bHashing = False Then
hash1.TransformBlock(rgbInner, 0, 64, rgbInner, 0)
bHashing = True
End If
hash1.TransformBlock(rgb, ib, cb, rgb, ib)
End Sub
Protected Overrides Function HashFinal() As Byte()
If bHashing = False Then
hash1.TransformBlock(rgbInner, 0, 64, rgbInner, 0)
bHashing = True
End If
' 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
End Function
End Class
注解
哈希函数将任意长度的二进制字符串映射到固定长度的小型二进制字符串。 加密哈希函数具有 属性,在计算上无法查找哈希为同一值的两个不同输入。 对数据的小更改会导致哈希中出现不可预测的大型更改。
键控哈希算法是一种依赖于密钥的单向哈希函数,用作消息身份验证代码。 只有知道密钥的人才能验证哈希。 键控哈希算法在不保密的情况下提供真实性。
哈希函数通常与数字签名一起使用并用来保持数据的完整性。 类 HMACSHA1 是键控哈希算法的一个示例。
由于 SHA1 出现冲突问题,Microsoft 建议使用基于 SHA256 或更好的安全模型。
构造函数
KeyedHashAlgorithm() |
初始化 KeyedHashAlgorithm 类的新实例。 |
字段
HashSizeValue |
表示计算所得的哈希代码的大小(以位为单位)。 (继承自 HashAlgorithm) |
HashValue |
表示计算所得的哈希代码的值。 (继承自 HashAlgorithm) |
KeyValue |
用于哈希算法的密钥。 |
State |
表示哈希计算的状态。 (继承自 HashAlgorithm) |
属性
CanReuseTransform |
获取一个值,该值指示是否可重复使用当前转换。 (继承自 HashAlgorithm) |
CanTransformMultipleBlocks |
当在派生类中重写时,获取一个值,该值指示是否可以转换多个块。 (继承自 HashAlgorithm) |
Hash |
获取计算所得的哈希代码的值。 (继承自 HashAlgorithm) |
HashSize |
获取计算所得的哈希代码的大小(以位为单位)。 (继承自 HashAlgorithm) |
InputBlockSize |
当在派生类中重写时,获取输入块的大小。 (继承自 HashAlgorithm) |
Key |
获取或设置用于哈希算法的密钥。 |
OutputBlockSize |
当在派生类中重写时,获取输出块的大小。 (继承自 HashAlgorithm) |
方法
显式接口实现
IDisposable.Dispose() |
释放由 HashAlgorithm 占用的非托管资源,还可以另外再释放托管资源。 (继承自 HashAlgorithm) |