Created
April 19, 2017 08:29
-
-
Save mikasjp/efbceb76aa20ac4264aa5ed1939c2a68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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