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$
    – celtschk
    Oct 23, 2012 at 15:40
  • $\begingroup$ @celtschk Not everyone might want to sign up to Gravatar. $\endgroup$
    – VLC
    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$
    – Mr.Wizard
    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$
    – rm -rf
    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$
    – Mr.Wizard
    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$
    – Mr.Wizard
    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$
    – VLC
    Oct 23, 2012 at 16:00
  • $\begingroup$ Then either it's my problem or v7 hash works differently. I'll look again. $\endgroup$
    – Mr.Wizard
    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$
    – Mr.Wizard
    Oct 23, 2012 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.