github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/util/md5.go (about)

     1  package util
     2  
     3  import (
     4  	"crypto/md5"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  )
     9  
    10  func FileMD5(path string) (string, error) {
    11  	f, err := os.Open(path)
    12  	if nil != err {
    13  		return "", err
    14  	}
    15  	defer f.Close()
    16  
    17  	hash := md5.New()
    18  	_, err = io.Copy(hash, f)
    19  	if nil != err {
    20  		return "", err
    21  	}
    22  	sum := hash.Sum(nil)
    23  	return fmt.Sprintf("%x", sum), nil
    24  }
    25  
    26  func ByteMD5(bytes []byte) (string, error) {
    27  	sum := md5.Sum(bytes)
    28  	return fmt.Sprintf("%x", sum), nil
    29  }