Revision 313c88f3e1fbf009d67d2e6578deb9b9fa4e4f08 authored by Guilherme Caulada on 18 October 2022, 01:11:59 UTC, committed by GitHub on 18 October 2022, 01:11:59 UTC
1 parent 7f5914f
Raw File
md5.go
package util

import (
	"crypto/md5"
	"encoding/hex"
	"io"
	"strings"
)

// Md5Sum calculates the md5sum of a stream
func Md5Sum(reader io.Reader) (string, error) {
	var returnMD5String string
	hash := md5.New()
	if _, err := io.Copy(hash, reader); err != nil {
		return returnMD5String, err
	}
	hashInBytes := hash.Sum(nil)[:16]
	returnMD5String = hex.EncodeToString(hashInBytes)
	return returnMD5String, nil
}

// Md5SumString calculates the md5sum of a string
func Md5SumString(input string) (string, error) {
	buffer := strings.NewReader(input)
	return Md5Sum(buffer)
}
back to top