Everything is getting “JavaScripted”! Here’s one! MD5 hash function

As far as I know, all web developers are familiar (or even expert) in JavaScript these days; at least, since the release of the first version of jQuery. Most people were forgetting it; but, plugins like jQuery, MooTools and others gave it a new life. Now all web developers are using it and making love with it.

I was surfing the web and suddenly found this very smart guy name Joseph Myers. He has done a lot of great jobs on JavaScript and one of them is a function which hash your string using the MD5 algorithm.

You know, I’m not going to go through details about what he has done; as a result, you can download this function from here and add to your project if you want to use it.

How it works

It’s very simple! After you’ve added this file to your project, the “mdf()” function can be used in order hash any string. Here’s an example:

Please enter your email address:<br />
<input type="email" id="userEmail" />
<button type="button" id="makeHash">Create</button>
<p id="result">

</p>
<script src="/Scripts/jquery-1.8.2.min.js"></script>
<script src="/Scripts/md5.js"></script>
<script>
    $(document).ready(function () {
        $("#makeHash").click(function () {
            $("#result").text(md5($("#userEmail").val()));
        });
    });
</script>

Simple but useful: Gravatar Html Helper for ASP.NET MVC

To be honest, I’m a big fan of Globally Recognized Avatar (aka Gravatar). As you may know, this company has been acquired by WordPress recently. Here’s the description of Gravatar, if you don’t know about it:

Your Gravatar is an image that follows you from site to site appearing beside your name when you do things like comment or post on a blog. Avatars help identify your posts on blogs and web forums, so why not on any site?

This service is getting more and more popular every day. Most developers around the world are using this service in their project especially in comment sections; when people comment on a post, news, and etcetera.

Some of these websites are developed using ASP.NET MVC these days; and, in this post I want to show you a very easy way to use this service in your ASP.NET MVC applications.

The first time I decided to use this service in one of my project, I developed a very lightweight class library which contains a MVC HTML helper that returns a HTML image element. You can use this helper wherever you want. In order to use this library just follow these steps:

  1. Add Reference the library (You can download it using the link at the bottom of this post).
  2. Use the “using” statement at the top of the view you want to use this helper to tell the MVC view engine to recognize this helper. The helper is in “MMR.Web.Mvc.Gravatar” namespace:
  3. Simply use @Html.Gravatar(“[email address]”) helper to display your Gravatar.

It’s that simple!

This Gravatar helper accepts three more optional parameters for more customizations stuff:

  • Size: this parameter indicates the size of requested avatar in pixels. The default value for this parameter is 48.
    Note: Gravatar images are square images. So whatever size you select will be applied to both width and height of the image.
  • CssClass: The CSS class for the image element for more flexibility. The default value is “” means no class is applied to the image.
  • Secure: This parameter is very cool! If you’re running your application under SSL protocol, you can turn this option on and the request will be sent to Gravatar server securely.

You can download the compiled version here: MMR.Web.Mvc.Gravatar.zip (2.45 kb)

If you have any question regarding this library, don’t hesitate a moment and ask it in the comments section.

Hope it helps.