Last active
August 16, 2021 12:15
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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