Skip to content

Instantly share code, notes, and snippets.

@omeid
Last active August 29, 2015 14:04
Show Gist options
  • Save omeid/f29909fdc6611f0fa0f9 to your computer and use it in GitHub Desktop.
Save omeid/f29909fdc6611f0fa0f9 to your computer and use it in GitHub Desktop.
Golang utils for copypasta.
package main
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"encoding/json"
"io"
"net/http"
)
func RandomString() string {
id := make([]byte, 32)
_, err := io.ReadFull(rand.Reader, id)
if err != nil {
panic(err) // This shouldn't happen
}
return hex.EncodeToString(id)
}
func MD5(s string) string {
h := md5.Sum([]byte(s))
return string(hex.EncodeToString(h[:]))
}
Copy link

ghost commented Aug 26, 2014

h := md5.Sum([]byte(s))
return string(hex.EncodeToString(h[:]))

Could be replaced with

return string(hex.EncodeToString(md5.Sum([]byte(s)) [:]))

@omeid
Copy link
Author

omeid commented Aug 26, 2014

@eintw1ck

Well, it doesn't seem to work: http://play.golang.org/p/HyTY4aY87k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment