encoderutil.go 535 B

12345678910111213141516171819202122232425262728293031
  1. package util
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "encoding/hex"
  6. "fmt"
  7. "io"
  8. "os"
  9. )
  10. func CountBase64Val(path string) string {
  11. h := md5.New()
  12. f, err := os.Open(path)
  13. if err != nil {
  14. return ""
  15. }
  16. io.Copy(h, f)
  17. re := h.Sum(nil) //算MD5值
  18. fmt.Printf("%x\n", re)
  19. mdHex := base64.StdEncoding.EncodeToString(h.Sum(nil)[:]) //MD5先转二进制数组再转base64编码
  20. fmt.Println(mdHex)
  21. f.Close()
  22. return mdHex
  23. }
  24. func GetMd5Val(str string) string {
  25. hash := md5.Sum([]byte(str))
  26. return hex.EncodeToString(hash[:])
  27. }