tags:

views:

754

answers:

7

I'm looking for one line code examples in various languages for getting a valid MD5 result (as a string, not a bytehash or what have you). For instance:

PHP: $token = md5($var1 . $var2);

I found VB especially troublesome to do in one line.

+2  A: 

Python

token = __import__('md5').new(var1 + var2).hexdigest()

or, if md5 is alrady imported:

token = md5.new(var1 + var2).hexdigest()

Thanks to Greg Hewgill

MizardX
Easier: token = md5.new(var1+var2).hexdigest()
Greg Hewgill
+2  A: 

There is a kind of universality in how this is to be accomplished. Typically, one defines a routine called md5_in_one_line (or Md5InOneLine) once, and uses it all over the place, just as one would use a library routine.

So for example, once one defines Md5InOneLine in C#, it's an easy one-liner to get the right results.

Justice
A: 

Does it really matter if you can do MD5 in one line. If it's that much trouble that you can't do it in VB in 1 line, then write your own function. Then, when you need to do MD5 in VB in one line, just call that function.

If doing it all in 1 line of code is all that important, here is 1 line of VB. that doesn't use the System.Web Namespace.

Dim MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider() : Dim HashBytes() As Byte : Dim MD5Str As String = "" : HashBytes = MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes("MyString")) : For i As Integer = 0 To HashBytes.Length - 1 : MD5Str &= HashBytes(i).ToString("x").PadLeft(2, "0") : Next

This will hash "MyString" and store the MD5 sum in MD5Str.

Kibbee
Why the loop? Use Convert.ToBase64String();
FlySwat
Because MD5 isn't usually represented in base 64.
Kibbee
Why not? A byte array can be represented however you want.
FlySwat
Didn't say you couldn't. Just saying it's not usually represented that way.
Kibbee
A: 

C#:

string hash = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5");

VB is virtually the same.

Here it is not using the System.Web namespace:

string hash = Convert.ToBase64String(newSystem.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));

Or in readable form:

string hash =
     Convert.ToBase64String
     (new System.Security.Cryptography.MD5CryptoServiceProvider()
          .ComputeHash
              (System.Text.Encoding.UTF8.GetBytes
                  (input)
              )
     );
FlySwat
Wow, that's an awful function name.
Greg Hewgill
Agreed, I don't understand why they implemented SHA1,SHA2 and MD5, yet chose that method name, and decided against using a strongly typed enumeration for the hash method...Must have been written on a friday.
FlySwat
Microsoft's developers seem to like building deep hierarchies. I've seen this madness before in VBA... :-)
staticsan
I would use the one with MD5CryptoServiceProvider (and the ancillary encoding classes).
Justice
+1  A: 

Aren't you really just asking "what languages have std. library support for MD5?" As Justice said, in any language that supports it, it'll just be a function call storing the result in a string variable. Even without built-in support, you could write that function in any language!

Drew Hall
It's for a document's examples section. Where possible I'd like to inform the audience "here's how you get the MD5 of a string", but didn't want to put in pages and pages of code examples.
Kyle Hodgson
+1  A: 

Just in case you need VBScript: download the MD5 class from webdevbros and then with one line:

hash = (new MD5).hash("some value")
Michal
A: 

Coldfusion has a bunch of hashing algorithms, MD5 is the default.

cfset var md5hashresult = hash("string to hash")