Last active
May 20, 2018 16:14
-
-
Save kasuken/48a7eb4ac4a787ce0f226c691d3bc570 to your computer and use it in GitHub Desktop.
Create a Gravatar Link with C#
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 class GravatarHelper | |
{ | |
private string CalculateHash(string email) | |
{ | |
MD5 md5Hasher = MD5.Create(); | |
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(email)); | |
StringBuilder sBuilder = new StringBuilder(); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
sBuilder.Append(data[i].ToString("x2")); | |
} | |
return sBuilder.ToString(); | |
} | |
public string CreateGravatarLink(string email, bool isHttps, int size = 120) | |
{ | |
string hash = CalculateHash(email); | |
string protocol = "http"; | |
if (isHttps) protocol = "https"; | |
return $"{protocol}://www.gravatar.com/avatar/{hash}?size={size}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment