2013-05-09 13:47:24 +00:00
|
|
|
package hash
|
2013-02-09 04:52:40 +00:00
|
|
|
|
|
|
|
import (
|
2013-03-27 20:30:24 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"hash/adler32"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
2013-02-09 04:52:40 +00:00
|
|
|
)
|
|
|
|
|
2013-02-14 03:30:04 +00:00
|
|
|
func Rand64() int64 {
|
2013-03-27 20:30:24 +00:00
|
|
|
return rand.Int63()
|
2013-02-14 03:30:04 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 04:52:40 +00:00
|
|
|
/* Convinience method for getting md5 sum of a string */
|
|
|
|
func GetMd5FromString(blob string) (sum []byte) {
|
2013-03-27 20:30:24 +00:00
|
|
|
h := md5.New()
|
|
|
|
defer h.Reset()
|
|
|
|
io.WriteString(h, blob)
|
|
|
|
return h.Sum(nil)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convinience method for getting md5 sum of some bytes */
|
|
|
|
func GetMd5FromBytes(blob []byte) (sum []byte) {
|
2013-03-27 20:30:24 +00:00
|
|
|
h := md5.New()
|
|
|
|
defer h.Reset()
|
|
|
|
h.Write(blob)
|
|
|
|
return h.Sum(nil)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get a small, decently unique hash */
|
|
|
|
func GetSmallHash() (small_hash string) {
|
2013-03-27 20:30:24 +00:00
|
|
|
h := adler32.New()
|
|
|
|
io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix()))
|
|
|
|
return fmt.Sprintf("%X", h.Sum(nil))
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|