2019-06-13 21:31:37 -05:00
|
|
|
package shell
|
|
|
|
|
2024-01-25 00:39:14 -06:00
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
2019-06-13 21:31:37 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// hash thyself: hash_file_md5(os.Args[0])
|
|
|
|
func Md5sum(filename string) string {
|
2019-06-14 22:35:45 -05:00
|
|
|
filename = Path(filename)
|
|
|
|
log.Println("shell.Md5sum() START filename =", filename)
|
2019-06-13 21:31:37 -05:00
|
|
|
hash, err := hash_file_md5(filename)
|
|
|
|
if err == nil {
|
|
|
|
log.Println("shell.Md5sum() hash =", hash)
|
|
|
|
return hash
|
|
|
|
}
|
2019-06-15 02:27:15 -05:00
|
|
|
log.Println("shell.Md5sum() failed err =", err.Error())
|
2019-06-13 21:31:37 -05:00
|
|
|
return ""
|
|
|
|
}
|