Last active
January 18, 2021 12:54
-
-
Save amscotti/2467512 to your computer and use it in GitHub Desktop.
MD5 hashing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:crypto'; | |
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}"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Yup, that seems to work and looks a lot better!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Regarding the scala version, wouldn't the following be enough?
digest.digest(text.getBytes).map("%02x".format(_)).mkString