This site is deprecated; docs have moved to docs.inspircd.org!

SHA256 HMAC In CSharp

From the makers of InspIRCd.
Revision as of 16:07, 24 February 2008 by Brain (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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
<span class="keyword">using</span> System;
<span class="keyword">using</span> System.Collections.Generic;
<span class="keyword">using</span> System.Text;
<span class="keyword">using</span> System.Security.Cryptography;
 
<span class="keyword">namespace</span> hmac_sha256
{
    <span class="keyword">class </span>Program
    {
        <span class="keyword">static</span> <span class="keyword">void</span> Main(<span class="keyword">string</span>[] args)
        {
            Console.Write(<span class="literal">"Password: "</span>);
            <span class="keyword">string</span> pass = Console.ReadLine();
            Console.Write(<span class="literal">"Challenge: "</span>);
            <span class="keyword">string</span> challenge = Console.ReadLine();

 
            Console.WriteLine(<span class="literal">"HMAC-SHA256:"</span> + hmac(pass, challenge));
            Console.ReadKey();
        }
 
        <span class="comment">/// <summary>
</span>        <span class="comment">/// Hash the given string with sha256
</span>        <span class="comment">/// </summary>
</span>        <span class="comment">/// <param name="password">the string to hash</param>

</span>        <span class="comment">/// <returns>The hex representation of the hash</returns>
</span>        <span class="keyword">static</span> <span class="keyword">string</span> sha256(<span class="keyword">string</span> password)
        {
            SHA256Managed crypt = <span class="keyword">new</span> SHA256Managed();
            <span class="keyword">string</span> hash = String.Empty;
            <span class="keyword">byte</span>[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password));
            <span class="keyword">foreach</span> (<span class="keyword">byte</span> bit <span class="keyword">in</span> crypto)
            {
                hash += bit.ToString(<span class="literal">"x2"</span>);
            }
            <span class="keyword">return</span> hash;
        }

 
        <span class="comment">/// <summary>
</span>        <span class="comment">/// XOR every byte in data with the specified byte
</span>        <span class="comment">/// </summary>
</span>        <span class="comment">/// <param name="data">the data to XOR</param>
</span>        <span class="comment">/// <param name="xor">the byte to XOR with</param>

</span>        <span class="comment">/// <returns>the XORed byte array</returns>
</span>        <span class="keyword">static</span> <span class="keyword">byte</span>[] xor(<span class="keyword">byte</span>[] data, <span class="keyword">byte</span> xor)
        {
            <span class="keyword">byte</span>[] buffer = <span class="keyword">new</span> Byte[data.Length];

 
            <span class="keyword">for</span> (<span class="keyword">int</span> i = 0; i < data.Length; i++)
                buffer[i] = Convert.ToByte(Convert.ToInt32(data[i]) ^ Convert.ToInt32(xor));
 
            <span class="keyword">return</span> buffer;
        }
 
        <span class="comment">/// <summary>

</span>        <span class="comment">/// This function creates the proper HMAC-SHA256 response
</span>        <span class="comment">/// </summary>
</span>        <span class="comment">/// <param name="password">the password</param>
</span>        <span class="comment">/// <param name="challenge">the challenge</param>
</span>        <span class="comment">/// <returns>the hmac-sha256 response to send to InspIRCd</returns>

</span>        <span class="keyword">static</span> <span class="keyword">string</span> hmac(<span class="keyword">string</span> password, <span class="keyword">string</span> challenge)
        {
            <span class="keyword">byte</span>[] pass = Encoding.ASCII.GetBytes(password);
            <span class="keyword">string</span> xor1 = Encoding.ASCII.GetString(xor(pass, (<span class="keyword">byte</span>)0x5C));
            <span class="keyword">string</span> xor2 = Encoding.ASCII.GetString(xor(pass, (<span class="keyword">byte</span>)0x36));
            <span class="keyword">string</span> sha2 = sha256(xor2 + challenge);
            <span class="keyword">string</span> sha1 = sha256(xor1 + sha2);
            <span class="keyword">return</span> sha1;
        }
    }
}