diff options
| author | Horus_Arch | 2015-02-15 17:59:51 +0100 |
|---|---|---|
| committer | Horus_Arch | 2015-02-15 17:59:51 +0100 |
| commit | 3c9bdbc66998075278f7d79fa10709e7fab5deb6 (patch) | |
| tree | a5bf32512080ed5fa57cadf0b9ad76ef9b113424 | |
| parent | 26f781239bfcda867f19262becee3b9687a7c79d (diff) | |
| download | freemail-revel.tar.gz | |
Add utilities.gorevel
| -rw-r--r-- | app/controllers/utilities.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/app/controllers/utilities.go b/app/controllers/utilities.go new file mode 100644 index 0000000..693a459 --- /dev/null +++ b/app/controllers/utilities.go @@ -0,0 +1,98 @@ +package controllers + +import ( + "crypto/md5" + "fmt" + // "github.com/garyburd/redigo/redis" + "github.com/revel/revel" + "golang.org/x/crypto/bcrypt" + "io" + "io/ioutil" + "math/rand" + "net/http" + // "time" +) + +// Returns the content of a webpage as string +func HttpGet(url string) (http.Header, string, error) { + response, err := http.Get(url) + if err != nil { + return nil, "Get request failed.", err + } + + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, "Reading body failed.", err + } + + return response.Header, string(contents), nil +} + +// Hashs and returns a string (md5) +func Hash(content string) string { + h := md5.New() + io.WriteString(h, content) + hash := fmt.Sprintf("%x", h.Sum(nil)) + + return hash +} + +// Creates a random string +func RandomKey() string { + letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + key := make([]rune, 40) + for i := range key { + key[i] = letters[rand.Intn(len(letters))] + } + + return string(key) +} + +//var pool = newPool() + +/* +// Creates a pool with connections to Redis +func newPool() *redis.Pool { + return &redis.Pool{ + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + Dial: func() (redis.Conn, error) { + //c, err := redis.Dial("tcp", ":6379") + c, err := redis.Dial("tcp", revel.Config.StringDefault("redis.server", "127.0.0.1")+":"+revel.Config.StringDefault("redis.port", "6379")) + if err != nil { + return nil, err + } + return c, err + }, + TestOnBorrow: func(c redis.Conn, t time.Time) error { + _, err := c.Do("PING") + return err + }, + } +} +*/ + +// Hashs password with bcrypt and returns the string +func HashPassword(password string) string { + if password == "" { + return "" + } + p := []byte(password) + hash, err := bcrypt.GenerateFromPassword(p, 10) + if err != nil { + revel.ERROR.Printf("%s \n", err) + return "" + } + return string(hash) +} + +// Verify password and hash +func VerifyPassword(password, hash string) bool { + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) + if err != nil { + revel.ERROR.Printf("%s \n", err) + return false + } + return true +} |
