MD5 hashing in a bunch of languages, featuring Ruby, Python, Node.js, Java, Rust, and the terminal (macOS, Windows, Linux)
What is MD5?
MD5 is a hashing function. It allows you to convert data into a hex string representation. Given the same input it should always return the same output. This algorithm can be used across a variety of programming languages and operating systems.
Wikipedia describes MD5 as follows:
The MD5 message-digest algorithm is a cryptographically broken but still widely used hash function producing a 128-bit hash value.
So that's not exactly the most flattering description of MD5, but it's still useful for generating data that does not need to be secure. One of the most popular services that uses MD5 is Gravatar, which uses an MD5 hash of email addresses.
You can generate an MD5 hash of any string on macOS, Windows and Linux in a variety of ways.
Here is an MD5 hash of the string techygrrrl
:
49a7d6fdbc5aa48c3f0b3b674693e2dc
Generating an MD5 hash
You can generate an MD5 hash with any input you like. For the purposes of this blog post, I'll be using the same string techygrrrl
as the example.
Below are a number of examples from the command line as well as different programming languages.
macOS terminal
To generate an MD5 hash in the macOS terminal, input the following:
md5 -s 'techygrrrl'
You should get the following output:
MD5 ("techygrrrl") = 49a7d6fdbc5aa48c3f0b3b674693e2dc
Windows terminal
There's no easy way to do this in the Windows shell environments (that I know of).
You can follow the instructions below for Ubuntu if you have Windows Subsystem for Linux (WSL) installed. It's fairly quick to install and free, if you were curious about it.
Ubuntu terminal
To generate an MD5 hash in the Ubuntu terminal, input the following:
echo -n 'techygrrrl' | md5sum --tag
You should get the following output:
MD5 (-) = 49a7d6fdbc5aa48c3f0b3b674693e2dc
The -n
flag removes new lines, and the --tag
flag returns the value without the -
that normally gets appended to the end.
Ruby
Ruby is a programming language that is supported on all operating systems. If you have Ruby installed, you can generate it in the REPL or in a script.
To enter the Ruby REPL, run irb
.
Then run the following commands:
require 'digest'
Digest::MD5.hexdigest 'techygrrrl'
You should get the following output:
=> "49a7d6fdbc5aa48c3f0b3b674693e2dc"
Python
Python is a programming language that is supported on all operating systems. If you have Python installed, you can generate it in the REPL or in a script.
To enter the Python REPL, run python
(or python3
if the first one fails).
Then run the following commands:
import hashlib
encoded = 'techygrrrl'.encode()
hash = hashlib.md5(encoded)
hash.hexdigest()
You should get the following output:
'49a7d6fdbc5aa48c3f0b3b674693e2dc'
JavaScript in Node.js
Node.js is a JavaScript runtime that is supported on all operating systems. If you have Node.js installed, you can generate it in the REPL or in a script.
To enter the Node.js REPL, run node
.
Then run the following commands:
const { createHash } = require('node:crypto');
createHash('md5').update('techygrrrl').digest('hex');
You should get the following output:
'49a7d6fdbc5aa48c3f0b3b674693e2dc'
Java using jshell
jshell is included with any installation of JDK 9 or higher.
To enter the Java REPL, run jshell
.
Then run the following commands:
import java.security.MessageDigest;
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest("techygrrrl".getBytes());
BigInteger n = new BigInteger(1, messageDigest);
String hashText = n.toString(16);
You should get the following output:
hashText ==> "49a7d6fdbc5aa48c3f0b3b674693e2dc"
Rust
At the time of writing, I did not find an official REPL for Rust. You can create a project using cargo init
in an empty directory.
Add the md5 crate to your Cargo.toml
, e.g. md5 = "0.7.0"
.
Then replace the generated print statement with the following:
fn main() {
let input = "techygrrrl";
let digest = md5::compute(input);
println!("md5({:?}) = {:?}", input, digest);
}
After you've changed both Cargo.toml
to add the dependencies, and the main.rs
file to include the code, you can run cargo run
.
You should get the following output:
md5("techygrrrl") = 49a7d6fdbc5aa48c3f0b3b674693e2dc
You can get the Rust version (CLI tool) on Github.
About me
If you liked this, you can find me sometimes coding on Twitch.
Subscribe to my newsletter
Read articles from techygrrrl directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
techygrrrl
techygrrrl
software developer && gamer passionate about software development && games ... catch my twitch streams on twitch where I stream: https://twitch.tv/techygrrrl