Creating an MD5 hash to get profile images from Gravatar

The following example shows how you can use the Node.js crypto library to create an MD5 hash to get a user image from

#!/usr/bin/env node

var crypto = require("crypto");

console.log(getGravatarImage("", ".jpg?s=200"));

 * Gets a gravatar image for the specified email address and optional arguments.
 * @param  {String} email The email address to get a profile image from Gravatar.
 * @param  {String} args  Arguments to append to the end of the Gravatar URL. Optional, defaults to "".
 * @return {String}       A fully qualified HREF for a gravatar image.
function getGravatarImage(email, args) {
    args = args || "";
    var BASE_URL = "//";
    // IE: //
    return (BASE_URL + md5(email) + args).trim();

 * MD5 hashes the specified string.
 * @param  {String} str A string to hash.
 * @return {String}     The hashed string.
function md5(str) {
    str = str.toLowerCase().trim();
    var hash = crypto.createHash("md5");
    return hash.digest("hex");

Leave a Reply

Your email address will not be published.