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

Difference between revisions of "SHA256 HMAC In CSharp"

From the makers of InspIRCd.
Jump to: navigation, search
m (Typo fix.)
(Remove pre-2.0 documentation.)
 
Line 1: Line 1:
 
{{Template:Development}}
 
{{Template:Development}}
 
The code below is a working example of the InspIRCd m_spanningtree SHA256 HMAC algorithm for protocols before 1202 in C#, written by Aleksi.
 
Scroll down for an example of the SHA256 HMAC algorithm used in protocol 1202 and after.
 
 
<span style="color:blue">using</span> System;
 
<span style="color:blue">using</span> System.Collections.Generic;
 
<span style="color:blue">using</span> System.Text;
 
<span style="color:blue">using</span> System.Security.Cryptography;
 
&nbsp;
 
<span style="color:blue">namespace</span> hmac_sha256
 
{
 
    <span style="color:blue">class </span>Program
 
    {
 
        <span style="color:blue">static</span> <span style="color:blue">void</span> Main(<span style="color:blue">string</span>[] args)
 
        {
 
            Console.Write(<span class="literal">"Password: "</span>);
 
            <span style="color:blue">string</span> pass = Console.ReadLine();
 
            Console.Write(<span class="literal">"Challenge: "</span>);
 
            <span style="color:blue">string</span> challenge = Console.ReadLine();
 
 
&nbsp;
 
            Console.WriteLine(<span class="literal">"HMAC-SHA256:"</span> + hmac(pass, challenge));
 
            Console.ReadKey();
 
        }
 
&nbsp;
 
        <span style="color:green">/// &lt;summary>
 
</span>        <span style="color:green">/// Hash the given string with sha256
 
</span>        <span style="color:green">/// &lt;/summary>
 
</span>        <span style="color:green">/// &lt;param name="password">the string to hash&lt;/param>
 
 
</span>        <span style="color:green">/// &lt;returns>The hex representation of the hash&lt;/returns>
 
</span>        <span style="color:blue">static</span> <span style="color:blue">string</span> sha256(<span style="color:blue">string</span> password)
 
        {
 
            SHA256Managed crypt = <span style="color:blue">new</span> SHA256Managed();
 
            <span style="color:blue">string</span> hash = String.Empty;
 
            <span style="color:blue">byte</span>[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password));
 
            <span style="color:blue">foreach</span> (<span style="color:blue">byte</span> bit <span style="color:blue">in</span> crypto)
 
            {
 
                hash += bit.ToString(<span class="literal">"x2"</span>);
 
            }
 
            <span style="color:blue">return</span> hash;
 
        }
 
 
&nbsp;
 
        <span style="color:green">/// &lt;summary>
 
</span>        <span style="color:green">/// XOR every byte in data with the specified byte
 
</span>        <span style="color:green">/// &lt;/summary>
 
</span>        <span style="color:green">/// &lt;param name="data">the data to XOR&lt;/param>
 
</span>        <span style="color:green">/// &lt;param name="xor">the byte to XOR with&lt;/param>
 
 
</span>        <span style="color:green">/// &lt;returns>the XORed byte array&lt;/returns>
 
</span>        <span style="color:blue">static</span> <span style="color:blue">byte</span>[] xor(<span style="color:blue">byte</span>[] data, <span style="color:blue">byte</span> xor)
 
        {
 
            <span style="color:blue">byte</span>[] buffer = <span style="color:blue">new</span> Byte[data.Length];
 
&nbsp;
 
            <span style="color:blue">for</span> (<span style="color:blue">int</span> i = 0; i &lt; data.Length; i++)
 
                buffer[i] = Convert.ToByte(Convert.ToInt32(data[i]) ^ Convert.ToInt32(xor));
 
&nbsp;
 
            <span style="color:blue">return</span> buffer;
 
        }
 
&nbsp;
 
        <span style="color:green">/// &lt;summary>
 
</span>        <span style="color:green">/// This function creates the proper HMAC-SHA256 response
 
</span>        <span style="color:green">/// &lt;/summary>
 
</span>        <span style="color:green">/// &lt;param name="password">the password&lt;/param>
 
</span>        <span style="color:green">/// &lt;param name="challenge">the challenge&lt;/param >
 
</span>        <span style="color:green">/// &lt;returns>the hmac-sha256 response to send to InspIRCd&lt;/returns>
 
 
</span>        <span style="color:blue">static</span> <span style="color:blue">string</span> hmac(<span style="color:blue">string</span> password, <span style="color:blue">string</span> challenge)
 
        {
 
            <span style="color:blue">byte</span>[] pass = Encoding.ASCII.GetBytes(password);
 
            <span style="color:blue">string</span> xor1 = Encoding.ASCII.GetString(xor(pass, (<span style="color:blue">byte</span>)0x5C));
 
            <span style="color:blue">string</span> xor2 = Encoding.ASCII.GetString(xor(pass, (<span style="color:blue">byte</span>)0x36));
 
            <span style="color:blue">string</span> sha2 = sha256(xor2 + challenge);
 
            <span style="color:blue">string</span> sha1 = sha256(xor1 + sha2);
 
            <span style="color:blue">return</span> sha1;
 
        }
 
    }
 
}
 
  
 
The following is an implementation of the SHA256 HMAC authentication used in protocol versions 1202 and above:
 
The following is an implementation of the SHA256 HMAC authentication used in protocol versions 1202 and above:

Latest revision as of 06:41, 8 January 2015

Development Development Material - Information posted here is for developer reference only. This material is subject to possible change and will be technical in nature.

The following is an implementation of the SHA256 HMAC authentication used in protocol versions 1202 and above:

using System.Text;
using System.Security.Cryptography;

public static string GenerateNewHMAC(string password, string challenge)
{
       HMACSHA256 hmc = new HMACSHA256(Encoding.ASCII.GetBytes(password));
       byte[] hmres = hmc.ComputeHash(Encoding.ASCII.GetBytes(challenge));
       return Convert.ToBase64String(hmres).Replace("=", "");
}