Created
January 29, 2020 16:00
-
-
Save xxott/273470495e6551207902b3d136834782 to your computer and use it in GitHub Desktop.
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
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