Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a MD5 hash from a string in Golang?

This is how I started to get a md5 hash from a string:

import "crypto/md5"  var original = "my string comes here" var hash = md5.New(original) 

But obviously this is not how it works. Can someone provide me a working sample for this?

like image 318
cringe Avatar asked Mar 04 '10 08:03

cringe


People also ask

How do you generate MD5 hash of a string?

Call MessageDigest. getInstance("MD5") to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one operation with md.

What is MD5 in Golang?

MD5 is an algorithm that is used to compute a hash value. Though it is cryptographically broken, it is still used widely. This algorithm produces a 128-bit hash value. In the Go language, there's a package available named crypto/md5 and with the help of this package, you may hash a string or a file input.

What is the MD5 hash of a string?

What is an MD5 hash? An MD5 hash is created by taking a string of an any length and encoding it into a 128-bit fingerprint. Encoding the same string using the MD5 algorithm will always result in the same 128-bit hash output.

How do I check if a string is an MD5?

You can check using the following function: function isValidMd5($md5 ='') { return preg_match('/^[a-f0-9]{32}$/', $md5); } echo isValidMd5('5d41402abc4b2a76b9719d911017c592'); The MD5 (Message-digest algorithm) Hash is typically expressed in text format as a 32 digit hexadecimal number.


2 Answers

Reference Sum,For me,following work well:

package main  import (     "crypto/md5"     "fmt" )  func main() {     data := []byte("hello")     fmt.Printf("%x", md5.Sum(data)) } 
like image 102
Alan Avatar answered Sep 20 '22 07:09

Alan


import (     "crypto/md5"     "encoding/hex" )  func GetMD5Hash(text string) string {    hash := md5.Sum([]byte(text))    return hex.EncodeToString(hash[:]) } 
like image 23
aviv Avatar answered Sep 23 '22 07:09

aviv