mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-01-10 17:19:56 +08:00
27 lines
432 B
Go
27 lines
432 B
Go
|
package md5
|
||
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
func FromBytes(data []byte) string {
|
||
|
result := md5.Sum(data)
|
||
|
return fmt.Sprintf("%x", result)
|
||
|
}
|
||
|
|
||
|
func FromString(str string) string {
|
||
|
data := []byte(str)
|
||
|
return FromBytes(data)
|
||
|
}
|
||
|
|
||
|
func FromReader(src io.Reader) (string, error) {
|
||
|
h := md5.New()
|
||
|
if _, err := io.Copy(h, src); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
checksum := h.Sum(nil)
|
||
|
return fmt.Sprintf("%x", checksum), nil
|
||
|
}
|