feat: add gravatar hash calculation
This commit is contained in:
parent
6f3961901e
commit
cebaac553c
|
@ -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>
|
/// <summary>
|
||||||
/// Represents an author of a blog post.
|
/// Represents an author of a blog post.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class Author : IEquatable<Author>
|
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>
|
/// <summary>
|
||||||
/// Gets or sets the email address of the author.
|
/// Gets or sets the email address of the author.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue