MD5 Sum

Kevin FOO
2 min readMay 29, 2020

How to perform MD5 sum of your string in Node.js, Ruby, Bash, CSharp/C# and Go/Golang.

Node.js

Assuming you already have npm installed in your Ubuntu 20.04. You’ll need to install MD5 package with the following command.

npm install md5

Once completed, create a file named md5sum.js and copy the contents below into the file.

const md5 = require('md5');
var str = process.argv[2];
console.log(md5(str));

Now to test if the md5sum.js file is working.

node md5sum.js 'hello world'

If it ran successfully, you should see the same output.

5eb63bbbe01eeed093cb22bb8f5acdc3

Bash in Ubuntu 20.04

MD5 sum is installed by default. To perform a MD5 sum, run the following command.

echo -n 'hello world'|md5sum|awk '{print $1}'

You should see the same output as below if it ran successfully.

5eb63bbbe01eeed093cb22bb8f5acdc3

Ruby

The digest gem is included in Ruby therefore no installation necessary. Create a file md5sum.rb and copy the contents below into the file.

require 'digest'
str = ARGV[0]
puts Digest::MD5.hexdigest(str)

To test if the file is working, run the following command.

ruby md5sum.rb 'hello world'

You should see the same output as below if it ran successfully.

5eb63bbbe01eeed093cb22bb8f5acdc3

Go/Golang

The imported package are all part of Go/Golang default installation. No additional package install with “go get” necessary. Create a file “md5sum.go” and copy the contents below into the file.

package mainimport (
"os"
"fmt"
"crypto/md5"
"encoding/hex"
)
func main() {
str := os.Args[1]
hasher := md5.New()
hasher.Write([]byte(str))
fmt.Println(hex.EncodeToString(hasher.Sum(nil)))
}

To test if the file is working, run the following command.

go run md5sum.go 'hello world'

You should see the same output as below if it ran successfully.

5eb63bbbe01eeed093cb22bb8f5acdc3

CSharp/C#

You’ll need to import these 2 libraries.

using System.Security.Cryptography;
using System.Text;

Function below will produce md5sum of the input string

public string Md5sum(string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++){
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString().ToLower();
}

< Back to all the stories I had written

--

--

Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.