Tuesday, 24 March 2015

Traditional MD5 And Salted MD5 Encoding In C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

public class md5_crypt
{
    public static string GenerateMD5(string input)
    {
        MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
        byte[] hash = md5.ComputeHash(inputBytes);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
    }

    public static bool CheckPassword(string plaintext, string hash)
    {
        string hashOfInput = GenerateMD5(plaintext);

        StringComparer comparer = StringComparer.OrdinalIgnoreCase;

        if (0 == comparer.Compare(hashOfInput, hash))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

public class joomla_crypt
{
    public static bool CheckPassword(string plaintext, string hash_pass)
    {
        bool result = false;
        string[] hashparts = hash_pass.Split(':');

        if (hashparts.Length > 1)
        {
            string str_user_hash = Ex3_md5_crypt.GenerateMD5(plaintext + hashparts[1]);

            if (str_user_hash == hashparts[0])
            {
                result = true;
            }
        }
      
        return result;
    }

    public static string JoomlaPassword(string plaintext)
    {
        /*
            From joomla Forum, that's what happen behind:
            Generate a password
            Generate 32 random characters
            Concatenate 1 and 2
            md5(3)
            store 4:2
            -------------------------------------------------
            Generate a password - we'll use 'password'
            Generate 32 random characters - we'll use 'WnvTroeiBmd5bjGmmsVUnNjppadH7giK'
            Concatenate 1 and 2 - passwordWnvTroeiBmd5bjGmmsVUnNjppadH7giK
            md5(3) - 3c57ebfec712312f30c3fd1981979f58
            store 4:2 - 3c57ebfec712312f30c3fd1981979f58:WnvTroeiBmd5bjGmmsVUnNjppadH7giK
         */

        string result = "";
        string salt = RandomString(32);

        result = plaintext + salt;

        result = Ex3_md5_crypt.GenerateMD5(result);

        result = result + ":" + salt;

        return result;
    }
    private static string RandomString(int length)
    {
        Random _random = new Random(Environment.TickCount);
        string chars = "0123456789abcdefghijklmnopqrstuvwxyz";
        StringBuilder builder = new StringBuilder(length);

        for (int i = 0; i < length; ++i)
            builder.Append(chars[_random.Next(chars.Length)]);

        return builder.ToString();
    }
}

No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...