2

Hi I'm woking on a C# project for school, I'm creating a basic login/register system in C# and use MySQL for my database. My Question is how do I hash the passwords with md5 and insert the hashed password in to the database?

I don't have any example code since i haven't yet started this project. The due is not until after spring break.

Halonic
  • 405
  • 2
  • 12

1 Answers1

0

Here is a simple sample of an extension method... Usage... string.Sha256() like "password".Sha256()

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

namespace Test
{
    /// <summary>
    /// Extension methods for hashing strings
    /// </summary>
    public static class HashExtensions
    {
        /// <summary>
        /// Creates a SHA256 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>A hash</returns>
        public static string Sha256(this string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                using (var sha = SHA256.Create())
                {
                    var bytes = Encoding.UTF8.GetBytes(input);
                    var hash = sha.ComputeHash(bytes);

                    return Convert.ToBase64String(hash);
                }
            }
            else
            {
                return string.Empty;
            }
        }
    }
}