md5.go 574 B

1234567891011121314151617181920212223242526
  1. package util
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "io"
  6. "strings"
  7. )
  8. // Md5Sum calculates the md5sum of a stream
  9. func Md5Sum(reader io.Reader) (string, error) {
  10. var returnMD5String string
  11. hash := md5.New()
  12. if _, err := io.Copy(hash, reader); err != nil {
  13. return returnMD5String, err
  14. }
  15. hashInBytes := hash.Sum(nil)[:16]
  16. returnMD5String = hex.EncodeToString(hashInBytes)
  17. return returnMD5String, nil
  18. }
  19. // Md5SumString calculates the md5sum of a string
  20. func Md5SumString(input string) (string, error) {
  21. buffer := strings.NewReader(input)
  22. return Md5Sum(buffer)
  23. }