Skip to content

Instantly share code, notes, and snippets.

@tosh
Forked from amscotti/md5.coffee
Last active December 10, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tosh/f0e5be5e4cf9d9372a6f to your computer and use it in GitHub Desktop.
Save tosh/f0e5be5e4cf9d9372a6f to your computer and use it in GitHub Desktop.
MD5 hashing
crypto = require('crypto');
#Quick MD5 of text
text = "MD5 this text!"
md5hash1 = crypto.createHash('md5').update(text).digest("hex")
#MD5 of text with updates
m = crypto.createHash('md5')
m.update("MD5 ")
m.update("this ")
m.update("text!")
md5hash2 = m.digest("hex")
#Output
console.log "#{md5hash1} should be the same as #{md5hash2}"
import 'package:crypto/crypto.dart' show MD5, CryptoUtils;
void main() {
//Quick MD5 of text
var text = "MD5 this text!";
var md5hash1 = CryptoUtils.bytesToHex((new MD5()..add(text.codeUnits)).close());
//MD5 of text with updates
var m = new MD5();
m.add("MD5 ".codeUnits);
m.add("this ".codeUnits);
m.add("text!".codeUnits);
var md5hash2 = CryptoUtils.bytesToHex(m.close());
//Output
print("${md5hash1} should be the same as ${md5hash2}");
}
import java.security.MessageDigest
def digest = MessageDigest.getInstance("MD5")
//Quick MD5 of text
def text = "MD5 this text!"
def md5hash1 = new BigInteger(1,digest.digest(text.getBytes())).toString(16).padLeft(32,"0")
//MD5 of text with updates
digest.update("MD5 ".getBytes())
digest.update("this ".getBytes())
digest.update("text!".getBytes())
def md5hash2 = new BigInteger(1,digest.digest()).toString(16).padLeft(32,"0")
//Output
print "${md5hash1} should be the same as ${md5hash2}"
use Digest::MD5 qw(md5 md5_hex md5_base64);
#Quick MD5 of text
my $text = "MD5 this text!";
my $md5hash1 = md5_hex( $text );
#MD5 of text with updates
$md5 = Digest::MD5->new;
$md5->add("MD5 ");
$md5->add("this ");
$md5->add("text!");
$md5hash2 = $md5->hexdigest;
#Output
print "$md5hash1 should be the same as $md5hash2\n";
import hashlib
#Quick MD5 of text
text = "MD5 this text!"
md5hash1 = hashlib.md5(text).hexdigest()
#MD5 of text with updates
m = hashlib.md5()
m.update("MD5 ")
m.update("this ")
m.update("text!")
md5hash2 = m.hexdigest()
#Output
print("%s should be the same as %s" % (md5hash1, md5hash2))
require 'digest/md5'
#Quick MD5 of text
text = "MD5 this text!"
md5hash1 = Digest::MD5.hexdigest(text)
#MD5 of text with updates
m = Digest::MD5.new()
m << "MD5 "
m << "this "
m << "text!"
md5hash2 = m.hexdigest()
#Output
puts "#{md5hash1} should be the same as #{md5hash2}"
import java.security.MessageDigest
val digest = MessageDigest.getInstance("MD5")
//Quick MD5 of text
val text = "MD5 this text!"
val md5hash1 = digest.digest(text.getBytes).map("%02x".format(_)).mkString
//MD5 of text with updates
digest.update("MD5 ".getBytes())
digest.update("this ".getBytes())
digest.update("text!".getBytes())
val md5hash2 = digest.digest().map(0xFF & _).map("%02x".format(_)).mkString
//Output
println(md5hash1 + " should be the same as " + md5hash2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment