1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2025-07-08 01:48:29 +00:00

beginning to move sub functions into there own import paths

This commit is contained in:
Vincent Batts 2013-05-09 09:41:06 -04:00
parent 75ee3c520c
commit 8aa7278da8
4 changed files with 13 additions and 11 deletions

37
hash/hash.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"crypto/md5"
"fmt"
"hash/adler32"
"io"
"math/rand"
"time"
)
func Rand64() int64 {
return rand.Int63()
}
/* 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)
}
/* 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)
}
/* get a small, decently unique hash */
func GetSmallHash() (small_hash string) {
h := adler32.New()
io.WriteString(h, fmt.Sprintf("%d", time.Now().Unix()))
return fmt.Sprintf("%X", h.Sum(nil))
}

39
hash/hash_test.go Normal file
View file

@ -0,0 +1,39 @@
package main
import (
"fmt"
"testing"
)
func TestRand64(t *testing.T) {
var i interface{}
i = Rand64()
v, ok := i.(int64)
if !ok {
t.Errorf("Rand64 returned wrong type")
}
if v < 0 {
t.Errorf("Rand64 returned a too small number [%d]", v)
}
}
func TestMd5Bytes(t *testing.T) {
var blob = []byte("Hurp til you Derp")
var expected = "3ef08fa896a154eee3c97f037c9d6dfc"
var actual = fmt.Sprintf("%x", GetMd5FromBytes(blob))
if actual != expected {
t.Errorf("Md5FromBytes sum did not match! %s != %s", actual, expected)
}
}
func TestMd5String(t *testing.T) {
var blob = "Hurp til you Derp"
var expected = "3ef08fa896a154eee3c97f037c9d6dfc"
var actual = fmt.Sprintf("%x", GetMd5FromString(blob))
if actual != expected {
t.Errorf("Md5FromString sum did not match! %s != %s", actual, expected)
}
}
func TestHash(t *testing.T) {
}