github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zstring/md5.go (about)

     1  package zstring
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  )
    10  
    11  func ProjectMd5() string {
    12  	d, _ := Md5File(os.Args[0])
    13  	return d
    14  }
    15  
    16  func Md5(s string) string {
    17  	return Md5Byte(String2Bytes(s))
    18  }
    19  
    20  func Md5Byte(s []byte) string {
    21  	h := md5.New()
    22  	_, _ = h.Write(s)
    23  	return hex.EncodeToString(h.Sum(nil))
    24  }
    25  
    26  func Md5File(path string) (encrypt string, err error) {
    27  	f, err := os.Open(path)
    28  	if err != nil {
    29  		return "", err
    30  	}
    31  	// r := bufio.NewReader(f)
    32  	h := md5.New()
    33  	_, err = io.Copy(h, f)
    34  	_ = f.Close()
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    39  }