github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/xhash/hash.go (about)

     1  package xhash
     2  
     3  import (
     4  	"crypto/hmac"
     5  	"crypto/md5"
     6  	"crypto/sha1"
     7  	"crypto/sha256"
     8  	"crypto/sha512"
     9  	"encoding/hex"
    10  	"hash"
    11  	"io"
    12  	"os"
    13  )
    14  
    15  // Sha256Hex string sha256
    16  func Sha256Hex(s string) string {
    17  	return hex.EncodeToString(Sha256([]byte(s)))
    18  }
    19  
    20  // Sha256 sha256
    21  func Sha256(b []byte) []byte {
    22  	return Hash(b, sha256.New())
    23  }
    24  
    25  // Sha512Hex string sha512
    26  func Sha512Hex(s string) string {
    27  	return hex.EncodeToString(Sha512([]byte(s)))
    28  }
    29  
    30  // Sha512 sha512
    31  func Sha512(b []byte) []byte {
    32  	return Hash(b, sha512.New())
    33  }
    34  
    35  // Sha1Hex string sha1
    36  func Sha1Hex(s string) string {
    37  	return hex.EncodeToString(Sha1([]byte(s)))
    38  }
    39  
    40  // Sha1 sha1
    41  func Sha1(b []byte) []byte {
    42  	return Hash(b, sha1.New())
    43  }
    44  
    45  // HmacSHA256Hex string hmac sha256
    46  func HmacSHA256Hex(s, key string) string {
    47  	return hex.EncodeToString(HmacSHA256([]byte(s), []byte(key)))
    48  }
    49  
    50  // HmacSHA256 hmac sha256
    51  func HmacSHA256(b, key []byte) []byte {
    52  	return Hmac(b, key, sha256.New)
    53  }
    54  
    55  // HmacSHA512Hex string hmac sha512
    56  func HmacSHA512Hex(s, key string) string {
    57  	return hex.EncodeToString(HmacSHA512([]byte(s), []byte(key)))
    58  }
    59  
    60  // HmacSHA512 hmac sha512
    61  func HmacSHA512(b, key []byte) []byte {
    62  	return Hmac(b, key, sha512.New)
    63  }
    64  
    65  // HmacSHA1Hex string hmac sha1
    66  func HmacSHA1Hex(s, key string) string {
    67  	return hex.EncodeToString(HmacSHA1([]byte(s), []byte(key)))
    68  }
    69  
    70  // HmacSHA1 hmac sha1
    71  func HmacSHA1(b, key []byte) []byte {
    72  	return Hmac(b, key, sha1.New)
    73  }
    74  
    75  // MD5Hex 字符串 md5
    76  func MD5Hex(s string) string {
    77  	return hex.EncodeToString(MD5([]byte(s)))
    78  }
    79  
    80  // MD5 计算 md5
    81  func MD5(b []byte) []byte {
    82  	return Hash(b, nil)
    83  }
    84  
    85  // MD5File 文件MD5
    86  func MD5File(filename string) (string, error) {
    87  	// check if file exists and is not a directory
    88  	info, err := os.Stat(filename)
    89  	if err != nil {
    90  		return "", err
    91  	}
    92  	if info.IsDir() {
    93  		return "", nil
    94  	}
    95  
    96  	// read file
    97  	file, err := os.Open(filename)
    98  	if err != nil {
    99  		return "", err
   100  	}
   101  	defer file.Close()
   102  
   103  	return MD5Reader(file)
   104  }
   105  
   106  // MD5Reader 计算 md5
   107  func MD5Reader(r io.Reader) (string, error) {
   108  	m := md5.New()
   109  	if _, err := io.Copy(m, r); err != nil {
   110  		return "", err
   111  	}
   112  	return hex.EncodeToString(m.Sum(nil)), nil
   113  }
   114  
   115  // Hash 计算hash
   116  func Hash(b []byte, h hash.Hash) []byte {
   117  	if h == nil {
   118  		h = md5.New()
   119  	}
   120  	h.Reset()
   121  	h.Write(b)
   122  
   123  	return h.Sum(nil)
   124  }
   125  
   126  // Hmac hmac
   127  func Hmac(b []byte, key []byte, h func() hash.Hash) []byte {
   128  	if h == nil {
   129  		h = md5.New
   130  	}
   131  	mac := hmac.New(h, key)
   132  	mac.Write(b)
   133  
   134  	return mac.Sum(nil)
   135  }