# Gravatars/identicons with Mathematica

The default avatar/identicon is based on an MD5 hash of your e-mail address. With some e-mail providers (e.g., GMail) the address [email protected] is still delivered to [email protected], so this trick can be used to change the hash of your e-mail and thus get a different identicon, but there is no guarantee that it looks better than the original.

How can we semi-automate the search for your preferred identicon using Mathematica?

• Of course the guaranteed way to get a pretty gravatar is to explicitly set it to a pretty picture. :-) Oct 23, 2012 at 15:40
– VLC
Oct 23, 2012 at 15:42
• By the way, I think Leonid has one of the best generated Gravatars in the system and AFAIK it's for his real email address. Oct 23, 2012 at 17:33
• This is exactly how I picked my autogenerated gravatar (for those that remember) before I became the hypnotoad
– rm -rf
Oct 23, 2012 at 19:03

With this function a random integer is inserted in the e-mail address ([email protected] becomes [email protected]) and then the hash value is computed. The hash value is used to get the corresponding identicon from the Gravatar website. This approach can address also some privacy concerns.

generatePic[email_] :=
Module[{emailparts, randN, input, inputhash, img},
emailparts = StringSplit[email, "@"];
randN = StringJoin["+", ToString[RandomInteger[99999]], "@"];
input = emailparts[[1]] <> randN <> emailparts[[2]];
inputhash = IntegerString[Hash[ToLowerCase[input], "MD5"], 16, 32];
img = Import[
"http://www.gravatar.com/avatar/" <> inputhash <>
"?s=128&d=identicon&r=PG"];
{img, input}
]


If we want to generate some identicons based on a specific e-mail address we just do this:

Grid[Table[generatePic["[email protected]"], {3}]]


Once we find an identicon we like, we just need to copy/paste the corresponding e-mail address into the e-mail field of the Stack Exchange profile.

Update for Mathematica versions before 8

It seems that older Mathematica versions include the enclosing quotes "" when it generates the hash, but there seems to be a workaround.

StringHash[string_String, type_: "MD5"] :=
Module[{stream, file, hash}, stream = OpenWrite[];
WriteString[stream, string];
file = Close[stream];
hash = FileHash[file, type];
DeleteFile[file];
hash]


And then:

generatePic[email_] :=
Module[{emailparts, randN, input, inputhash, img},
emailparts = StringSplit[email, "@"];
randN = StringJoin["+", ToString[RandomInteger[99999]], "@"];
input = emailparts[[1]] <> randN <> emailparts[[2]];
inputhash =
IntegerString[StringHash[ToLowerCase[input], "MD5"], 16, 32];
img = Import[
"http://www.gravatar.com/avatar/" <> inputhash <>
"?s=128&d=identicon&r=PG"];
{img, input}]

• I wish I had thought of this. Nice work! Oct 23, 2012 at 15:36
• I've got a problem: The avatar produced by your function doesn't match the one I get when I enter the email address in the profile here on StackExchange. Oct 23, 2012 at 15:49
• @Mr.Wizard I've tried several gmail addresses and they always matched. I'll try to see where the problem could be.
– VLC
Oct 23, 2012 at 16:00
• Then either it's my problem or v7 hash works differently. I'll look again. Oct 23, 2012 at 16:03
• I tried another one and it also did not match. Then I tried one from the picture in your answer and it did. Would you please post the inputhash value you get for several addresses? Oct 23, 2012 at 16:07