32 lines
911 B
C#
32 lines
911 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Utils
|
|
{
|
|
public class PasswordHash
|
|
{
|
|
// No es perfecto, pero me tengo un limite de 15 caracteres en la base de datos
|
|
// y realmente no queria guardar contraseñas en texto plano
|
|
public static string Hash(string password)
|
|
{
|
|
using (SHA1Managed sha1 = new SHA1Managed())
|
|
{
|
|
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(password));
|
|
return Convert.ToBase64String(hash).Truncate(15);
|
|
}
|
|
}
|
|
|
|
public static bool Compare(string password, string hashed)
|
|
{
|
|
if (password == null) return false;
|
|
if (hashed == null) return false;
|
|
|
|
return Hash(password).Equals(hashed);
|
|
}
|
|
}
|
|
}
|