feat: add gravatar hash calculation

This commit is contained in:
Oliver Booth 2023-08-08 00:20:52 +01:00
parent 6f3961901e
commit cebaac553c
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61

View File

@ -1,10 +1,43 @@
namespace OliverBooth.Data.Blog;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Cryptography;
using System.Text;
namespace OliverBooth.Data.Blog;
/// <summary>
/// Represents an author of a blog post.
/// </summary>
public sealed class Author : IEquatable<Author>
{
[NotMapped]
public string AvatarHash
{
get
{
if (EmailAddress is null)
{
return string.Empty;
}
using var md5 = MD5.Create();
ReadOnlySpan<char> span = EmailAddress.AsSpan();
int byteCount = Encoding.UTF8.GetByteCount(span);
Span<byte> bytes = stackalloc byte[byteCount];
Encoding.UTF8.GetBytes(span, bytes);
Span<byte> hash = stackalloc byte[16];
md5.TryComputeHash(bytes, hash, out _);
var builder = new StringBuilder();
foreach (byte b in hash)
{
builder.Append(b.ToString("x2"));
}
return builder.ToString();
}
}
/// <summary>
/// Gets or sets the email address of the author.
/// </summary>