A simple HTMLHelper for displaying Gravatars

UncategorizedPublished November 2, 2009 at 8:44 PM No Comments

Practically, The whole world knows about Gravatar(Globally Recognized Avatar).
I wanted to use this awesome service in one of my ASP.NET MVC applications, So I went to it’s documentation to see what should I do. [Well there is a great HTMLHelper in order to perform this, but I have a point!]

The big thing in constructing a Gravatar URL is how to get a string’s hexadecimal MD5 Hash. [as you may or may not know, you can do lots of stuff with MD5 Hash of anything!]
So … This is the answer to this question:

[code:vb]

Function GetHash(ByVal text As String) As String
        Dim myhash As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()

        Dim hashed = _
            myhash.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(text))

        Dim sb As StringBuilder = New StringBuilder()

        For Each b In hashed
            sb.Append(b.ToString("x2"))
        Next

        Return sb.ToString()
End Function

[/code]

Leave a Reply

(required)

(required)


Premium Wordpress Plugin