1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-23 16:45:39 +00:00

hash: docs and argument

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2017-04-07 15:03:28 -04:00
parent b3f5e06c4d
commit 416e4e85ab
Signed by: vbatts
GPG key ID: 10937E57733F1362
2 changed files with 10 additions and 9 deletions

View file

@ -14,11 +14,12 @@ func init() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }
// Rand64 is an int64 random number
func Rand64() int64 { func Rand64() int64 {
return rand.Int63() return rand.Int63()
} }
/* Convinience method for getting md5 sum of a string */ // GetMd5FromString is a convinience method for getting md5 sum of a string
func GetMd5FromString(blob string) (sum []byte) { func GetMd5FromString(blob string) (sum []byte) {
h := md5.New() h := md5.New()
defer h.Reset() defer h.Reset()
@ -26,7 +27,7 @@ func GetMd5FromString(blob string) (sum []byte) {
return h.Sum(nil) return h.Sum(nil)
} }
/* Convinience method for getting md5 sum of some bytes */ // GetMd5FromBytes is a convinience method for getting md5 sum of some bytes
func GetMd5FromBytes(blob []byte) (sum []byte) { func GetMd5FromBytes(blob []byte) (sum []byte) {
h := md5.New() h := md5.New()
defer h.Reset() defer h.Reset()
@ -34,9 +35,9 @@ func GetMd5FromBytes(blob []byte) (sum []byte) {
return h.Sum(nil) return h.Sum(nil)
} }
/* get a small, decently unique hash */ // GetSmallHash get a small, decently unique hash
func GetSmallHash() (small_hash string) { func GetSmallHash(num uint) string {
h := sha256.New() h := sha256.New()
io.WriteString(h, fmt.Sprintf("%d%d", Rand64())) io.WriteString(h, fmt.Sprintf("%d%d", Rand64(), Rand64()))
return strings.ToLower(fmt.Sprintf("%X", h.Sum(nil)[0:4])) return strings.ToLower(fmt.Sprintf("%X", h.Sum(nil)[0:num]))
} }

View file

@ -274,7 +274,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
} }
if len(filename) == 0 { if len(filename) == 0 {
str := hash.GetSmallHash() str := hash.GetSmallHash(5)
if len(p_ext) == 0 { if len(p_ext) == 0 {
filename = fmt.Sprintf("%s.jpg", str) filename = fmt.Sprintf("%s.jpg", str)
} else { } else {
@ -673,7 +673,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
if exists || useRandName { if exists || useRandName {
ext := filepath.Ext(local_filename) ext := filepath.Ext(local_filename)
str := hash.GetSmallHash() str := hash.GetSmallHash(5)
stored_filename = fmt.Sprintf("%s%s", str, ext) stored_filename = fmt.Sprintf("%s%s", str, ext)
} else { } else {
stored_filename = filepath.Base(local_filename) stored_filename = filepath.Base(local_filename)
@ -765,7 +765,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
} }
if exists || useRandName { if exists || useRandName {
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
str := hash.GetSmallHash() str := hash.GetSmallHash(5)
filename = strings.ToLower(fmt.Sprintf("%s%s", str, ext)) filename = strings.ToLower(fmt.Sprintf("%s%s", str, ext))
} }