Created
November 15, 2018 08:17
-
-
Save cleftheris/dd38c52a22487b41c0603429c0bbcea4 to your computer and use it in GitHub Desktop.
xUnit test demonstrating MySQL generated sha1 password hashing in c#
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; | |
using Xunit; | |
namespace Identity.Tests | |
{ | |
public class PasswordTests | |
{ | |
[InlineData("test", "*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29")] | |
[Theory] | |
public void MySqlHash(string password, string hash) { | |
var keyArray = Encoding.UTF8.GetBytes(password); | |
var enc = new SHA1Managed(); | |
var encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray)); | |
var builder = new StringBuilder(encodedKey.Length); | |
foreach (var b in encodedKey) { | |
builder.Append(b.ToString("X2")); | |
} | |
var generatedHash = "*" + builder.ToString(); | |
Assert.Equal(hash, generatedHash); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment