-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathHashedAuthenticationMechanism.cs
More file actions
73 lines (64 loc) · 2.01 KB
/
HashedAuthenticationMechanism.cs
File metadata and controls
73 lines (64 loc) · 2.01 KB
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
using System;
namespace Waher.Networking.SASL
{
/// <summary>
/// Base class for all hashed authentication mechanisms.
/// </summary>
public abstract class HashedAuthenticationMechanism : AuthenticationMechanism
{
/// <summary>
/// Base class for all hashed authentication mechanisms.
/// </summary>
public HashedAuthenticationMechanism()
{
}
/// <summary>
/// Hash function
/// </summary>
/// <param name="Data">Data to hash.</param>
/// <returns>Hash of data.</returns>
public abstract byte[] H(byte[] Data);
/// <summary>
/// Hash function
/// </summary>
/// <param name="s">String to hash.</param>
/// <returns>Hash of string.</returns>
public virtual byte[] H(string s)
{
return this.H(System.Text.Encoding.UTF8.GetBytes(s));
}
/// <summary>
/// See RFC 2104 for a description of the HMAC algorithm:
/// http://tools.ietf.org/html/rfc2104
/// </summary>
/// <param name="Key">Key</param>
/// <param name="Text">Text</param>
/// <returns>HMAC(Key,Text)</returns>
protected byte[] HMAC(byte[] Key, byte[] Text)
{
byte[] A1 = Key;
if (A1.Length > 64)
A1 = this.H(A1);
if (A1.Length < 64)
Array.Resize(ref A1, 64);
byte[] A3 = (byte[])A1.Clone();
int i;
for (i = 0; i < 64; i++)
{
A1[i] ^= 0x5c; // opad
A3[i] ^= 0x36; // ipad
}
return this.H(CONCAT(A1, this.H(CONCAT(A3, Text))));
}
/// <summary>
/// H(CONCAT(k:s))
/// </summary>
/// <param name="k">k</param>
/// <param name="s">s</param>
/// <returns>KD(k,s)</returns>
protected byte[] KD(string k, string s)
{
return this.H(CONCAT(k, ":", s));
}
}
}