1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-09-28 05:09:55 +00:00
imgsrv/hash/hash.go
Vincent Batts 416e4e85ab
hash: docs and argument
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2017-04-07 15:04:16 -04:00

43 lines
867 B
Go

package hash
import (
"crypto/md5"
"crypto/sha256"
"fmt"
"io"
"math/rand"
"strings"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// Rand64 is an int64 random number
func Rand64() int64 {
return rand.Int63()
}
// GetMd5FromString is a convinience method for getting md5 sum of a string
func GetMd5FromString(blob string) (sum []byte) {
h := md5.New()
defer h.Reset()
io.WriteString(h, blob)
return h.Sum(nil)
}
// GetMd5FromBytes is a convinience method for getting md5 sum of some bytes
func GetMd5FromBytes(blob []byte) (sum []byte) {
h := md5.New()
defer h.Reset()
h.Write(blob)
return h.Sum(nil)
}
// GetSmallHash get a small, decently unique hash
func GetSmallHash(num uint) string {
h := sha256.New()
io.WriteString(h, fmt.Sprintf("%d%d", Rand64(), Rand64()))
return strings.ToLower(fmt.Sprintf("%X", h.Sum(nil)[0:num]))
}