Skip to content

Instantly share code, notes, and snippets.

@mikasjp
Created April 19, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikasjp/efbceb76aa20ac4264aa5ed1939c2a68 to your computer and use it in GitHub Desktop.
Save mikasjp/efbceb76aa20ac4264aa5ed1939c2a68 to your computer and use it in GitHub Desktop.
using System;
using System.Security.Cryptography;
using System.Text;
public class PasswordVerificator
{
public static void Main()
{
string AuthenticationResult = AuthenticateUser("admin", "supersecretpassword")?"User authenticated!":"Access denied!";
Console.WriteLine(AuthenticationResult);
}
public static string HexStringFromBytes(byte[] bytes)
{
var sb = new StringBuilder();
foreach (byte b in bytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
public static string SHA1Hash(string s)
{
byte[] bytes = Encoding.UTF8.GetBytes(s);
var sha1 = SHA1.Create();
byte[] hashBytes = sha1.ComputeHash(bytes);
return HexStringFromBytes(hashBytes);
}
public static bool AuthenticateUser(string user, string password)
{
return (user=="admin" && SHA1Hash(password)=="18960546905b75c869e7de63961dc185f9a0a7c9");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment