-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHMACBuildInAdapter.cs
80 lines (69 loc) · 1.93 KB
/
HMACBuildInAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Diagnostics;
namespace HashLib
{
internal class HMACBuildInAdapter : Hash, IHMACBuildIn
{
protected static readonly byte[] EMPTY = new byte[0];
protected System.Security.Cryptography.HMAC m_hmac;
private byte[] m_key;
public byte[] Key
{
get
{
return (byte[])m_key.Clone();
}
set
{
if (m_key == null)
{
m_key = new byte[0];
}
else
{
m_key = (byte[])value.Clone();
}
}
}
public int? KeyLength
{
get
{
return null;
}
}
public HMACBuildInAdapter(System.Security.Cryptography.HMAC a_hmac, int a_block_size)
: base(a_hmac.HashSize / 8, a_block_size)
{
m_hmac = a_hmac;
m_key = new byte[0];
}
public override void Initialize()
{
m_hmac.Initialize();
m_hmac.Key = Key;
}
public override HashResult TransformFinal()
{
m_hmac.TransformFinalBlock(EMPTY, 0, 0);
byte[] result = m_hmac.Hash;
Debug.Assert(result.Length == HashSize);
Initialize();
return new HashResult(result);
}
public override void TransformBytes(byte[] a_data, int a_index, int a_length)
{
Debug.Assert(a_index >= 0);
Debug.Assert(a_length >= 0);
Debug.Assert(a_index + a_length <= a_data.Length);
m_hmac.TransformBlock(a_data, a_index, a_length, null, 0);
}
public override string Name
{
get
{
return String.Format("{0}({1})", GetType().Name, m_hmac.GetType().Name);
}
}
}
}