45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Kreta.Framework.Security.PasswordCrypting
|
|
{
|
|
public static class BasePasswordCrypter
|
|
{
|
|
public static string EncodePasswordSHA1(string plainPassword, string salt)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(salt))
|
|
{
|
|
return plainPassword;
|
|
}
|
|
|
|
string saltedPlainPassword = plainPassword + salt;
|
|
using (HashAlgorithm hash = SHA1.Create())
|
|
{
|
|
byte[] passwordarray = Encoding.Unicode.GetBytes(saltedPlainPassword);
|
|
byte[] hasharray = hash.ComputeHash(passwordarray);
|
|
|
|
string encryptedPassword = Convert.ToBase64String(hasharray);
|
|
|
|
return encryptedPassword;
|
|
}
|
|
}
|
|
|
|
public static string EncodePasswordMD5(string plainPassword)
|
|
{
|
|
using (HashAlgorithm hash = MD5.Create())
|
|
{
|
|
byte[] passwordArray = hash.ComputeHash(Encoding.UTF8.GetBytes(plainPassword));
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
foreach (byte item in passwordArray)
|
|
{
|
|
stringBuilder.Append(item.ToString("x2"));
|
|
}
|
|
var result = stringBuilder.ToString();
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|