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

SHA256 HMAC In CSharp

From the makers of InspIRCd.
Revision as of 10:49, 31 December 2011 by SaberUK (Talk | contribs)

Jump to: navigation, search
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 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.

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
 
namespace hmac_sha256
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Password: ");
            string pass = Console.ReadLine();
            Console.Write("Challenge: ");
            string challenge = Console.ReadLine(); 

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

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

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

        /// <returns>the XORed byte array</returns>
        static byte[] xor(byte[] data, byte xor)
        {
            byte[] buffer = new Byte[data.Length]; 
 
            for (int i = 0; i < data.Length; i++)
                buffer[i] = Convert.ToByte(Convert.ToInt32(data[i]) ^ Convert.ToInt32(xor));
 
            return buffer;
        }
 
        /// <summary> 
        /// This function creates the proper HMAC-SHA256 response
        /// </summary>
        /// <param name="password">the password</param>
        /// <param name="challenge">the challenge</param >
        /// <returns>the hmac-sha256 response to send to InspIRCd</returns> 

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

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("=", "");
}