How to Generate an MD5 Hash in Code
Here are examples of generating an MD5 hash using various programming languages:
PHP
<?php
$input = 'Hello, world!';
$hash = md5($input);
echo $hash;
?>
C#
using System.Security.Cryptography;
using System.Text;
string input = "Hello, world!";
using var md5 = MD5.Create();
byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
string hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
Console.WriteLine(hash);
Java
import java.security.MessageDigest;
public class MD5Example {
public static void main(String[] args) throws Exception {
String input = "Hello, world!";
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hashBytes = md.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
System.out.println(sb.toString());
}
}
Python
import hashlib
input = "Hello, world!"
hash = hashlib.md5(input.encode()).hexdigest()
print(hash)