Skip to content

Instantly share code, notes, and snippets.

@martinabrahams
Last active August 16, 2021 12:15
Show Gist options
  • Save martinabrahams/c273a525eec2d02fa5ff to your computer and use it in GitHub Desktop.
Save martinabrahams/c273a525eec2d02fa5ff to your computer and use it in GitHub Desktop.
Simple C# method to create the hash of the email address and return Gravatar image URL for a supplied email address. As per official documentation - https://en.gravatar.com/site/implement/images/
public static string GetGravatarImageUrl(string emailAddress)
{
// Create MD5 Hash of email address
var md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(emailAddress.Trim().ToLower());
byte[] hash = md5.ComputeHash(inputBytes);
// Create lower-case hex string
var sb = new System.Text.StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
// Build URL
return string.Concat("http://www.gravatar.com/avatar/", sb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment