Skip to content

Instantly share code, notes, and snippets.

@krakiun
Created January 29, 2020 16:00
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 krakiun/273470495e6551207902b3d136834782 to your computer and use it in GitHub Desktop.
Save krakiun/273470495e6551207902b3d136834782 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
)
func hash_file_md5(filePath string) (string, error) {
var returnMD5String string
file, err := os.Open(filePath)
if err != nil {
return returnMD5String, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return returnMD5String, err
}
hashInBytes := hash.Sum(nil)[:16]
returnMD5String = hex.EncodeToString(hashInBytes)
return returnMD5String, nil
}
func main() {
hash, err := hash_file_md5(os.Args[0])
if err == nil {
fmt.Println(hash)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment