21
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ Of course the guaranteed way to get a pretty gravatar is to explicitly set it to a pretty picture. :-) $\endgroup$ Commented Oct 23, 2012 at 15:40
  • $\begingroup$ @celtschk Not everyone might want to sign up to Gravatar. $\endgroup$ Commented Oct 23, 2012 at 15:42
  • $\begingroup$ 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. $\endgroup$ Commented Oct 23, 2012 at 17:33
  • $\begingroup$ This is exactly how I picked my autogenerated gravatar (for those that remember) before I became the hypnotoad $\endgroup$ Commented Oct 23, 2012 at 19:03

1 Answer 1

24
$\begingroup$

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}]] 

identicons

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}] 
$\endgroup$
14
  • $\begingroup$ I wish I had thought of this. Nice work! $\endgroup$ Commented Oct 23, 2012 at 15:36
  • $\begingroup$ 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. $\endgroup$ Commented Oct 23, 2012 at 15:49
  • $\begingroup$ @Mr.Wizard I've tried several gmail addresses and they always matched. I'll try to see where the problem could be. $\endgroup$ Commented Oct 23, 2012 at 16:00
  • $\begingroup$ Then either it's my problem or v7 hash works differently. I'll look again. $\endgroup$ Commented Oct 23, 2012 at 16:03
  • $\begingroup$ 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? $\endgroup$ Commented Oct 23, 2012 at 16:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.