You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Security/Cryptography/CryptographyService.cs

48 lines
876 B

2 years ago
using System.Security.Cryptography;
using System.Text;
namespace Connected.Security.Cryptography
{
internal class CryptographyService : ICryptographyService
{
public byte[]? Hash(string value)
{
if (string.IsNullOrEmpty(value))
return null;
using var md = MD5.Create();
return GetHash(md, value);
}
private static byte[] GetHash(MD5 hash, string value)
{
return hash.ComputeHash(Encoding.UTF8.GetBytes(value));
}
public bool Verify(string value, byte[] existing)
{
if (value is null && existing is null)
return false;
if (!value.Any() && !existing.Any())
return false;
using var md = MD5.Create();
var hash = GetHash(md, value);
if (value.Length != hash.Length)
return false;
for (var i = 0; i < value.Length; i++)
{
if (value[i] != hash[i])
return false;
}
return true;
}
}
}