Skip to content

Instantly share code, notes, and snippets.

@neonguru
Created May 12, 2012 22:23
Show Gist options
  • Select an option

  • Save neonguru/2669478 to your computer and use it in GitHub Desktop.

Select an option

Save neonguru/2669478 to your computer and use it in GitHub Desktop.
Get Gravitar image from email - <img src="@Gravitar.profileImageUrl(email)" />
/**
* See https://en.gravatar.com/site/implement/images/java/ for java reference
*
* d=mm tells gravitar to return a mystery-man image if user is not found
* More options here: https://en.gravatar.com/site/implement/images/
*/
import java.security.{NoSuchAlgorithmException, MessageDigest}
import java.io.UnsupportedEncodingException
object Gravitar {
def profileImageUrl(email: String): String = {
"http://www.gravatar.com/avatar/" + md5Hex(email.trim.toLowerCase) + "?d=mm"
}
private def hex(array: Array[Byte]): String = {
val sb = for (i <- array) yield ((i & 0xFF) | 0x100).toHexString.substring(1,3)
sb.mkString
}
private def md5Hex(message: String): String = {
try {
val md = MessageDigest.getInstance("MD5")
hex(md.digest(message.getBytes("CP1252")))
}
catch {
case e: NoSuchAlgorithmException => ""
case u: UnsupportedEncodingException => ""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment