Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- import "net/http"
- import "encoding/json"
- import "code.google.com/p/go.crypto/bcrypt"
- const LEN_PASSWORD = 8
- type Password struct {
- Hash string `json:"hash"`
- Plain string `json:"plain"`
- }
- func main() {
- http.HandleFunc("/compare",
- func(res http.ResponseWriter, req *http.Request) {
- var comparison Password
- err := json.NewDecoder(req.Body).Decode(&comparison)
- if err != nil {
- http.Error(res, err.Error(), http.StatusBadRequest)
- return
- }
- })
- http.HandleFunc("/hash",
- func(res http.ResponseWriter, req *http.Request) {
- var hashable Password
- err := json.NewDecoder(req.Body).Decode(&hashable)
- if err != nil {
- http.Error(res, err.Error(), http.StatusBadRequest)
- return
- }
- if len(hashable.Plain) < LEN_PASSWORD {
- http.Error(res,
- fmt.Sprintf(
- "Password must be at least %d characters!",
- LEN_PASSWORD),
- http.StatusBadRequest)
- return
- }
- hash, err := bcrypt.GenerateFromPassword(
- []byte(hashable.Plain),
- bcrypt.DefaultCost)
- if err != nil {
- http.Error(res, err.Error(), http.StatusInternalServerError)
- return
- }
- hashable.Hash = string(hash)
- jsonData, err := json.Marshal(hashable)
- if err != nil {
- http.Error(res, err.Error(), http.StatusInternalServerError)
- return
- }
- res.Write(jsonData)
- })
- err := http.ListenAndServe(":3000", nil)
- if err != nil {
- fmt.Println("voi vittu", err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement