Created
May 13, 2018 02:44
-
-
Save drmcarvalho/80245ca1f1f2f73b96ca30eade96b961 to your computer and use it in GitHub Desktop.
Exemplo de Hash MD5
This file contains hidden or 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; | |
namespace HashExemplo | |
{ | |
public class HashMD5 | |
{ | |
public string GerarHashMD5(string entrada) | |
{ | |
using (MD5 md5Hash = MD5.Create()) | |
{ | |
byte[] array = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(entrada)); | |
StringBuilder builder = new StringBuilder(); | |
for (int i = 0; i < array.Length; i++) | |
{ | |
builder.Append(array[i].ToString("x2")); | |
} | |
return builder.ToString(); | |
} | |
} | |
public bool VerificarHashMD5(string entrada, string hash) | |
{ | |
var hashEntrada = GerarHashMD5(entrada); | |
StringComparer comparer = StringComparer.OrdinalIgnoreCase; | |
return (0 == comparer.Compare(hashEntrada, hash)); | |
} | |
} | |
} |
This file contains hidden or 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 HashExemplo; | |
using static System.Console; | |
namespace HashExample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var senha = "minhaSenha"; | |
var md5 = new HashMD5(); | |
var hash = md5.GerarHashMD5(senha); | |
WriteLine($"Valor (senha): {senha}\n\n"); | |
WriteLine($"Novo valor (hash): {hash}\n\n"); | |
if (md5.VerificarHashMD5("minhaSenha", hash)) | |
WriteLine("Senha válida."); | |
else | |
WriteLine("Senha inválida."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment