// Some code
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/pkg/errors"
)
type User struct {
Account string
Pass string
}
var m map[string]User
func main() {
m = make(map[string]User)
s := User{Account: "a", Pass: "b"}
if err := registryUser(s); err != nil {
fmt.Printf("registryUser failed %s", s.Account)
}
if err := registryUser(s); err != nil {
fmt.Printf("registryUser failed %s", s.Account)
}
ls := User{Account: "a", Pass: "b"}
err := loginUser(ls)
if err != nil {
fmt.Printf("User failed %s", ls.Account)
}
ls2 := User{Account: "aa", Pass: "bss"}
if err := loginUser(ls2); err != nil {
fmt.Printf("User failed %s", ls2.Account)
}
}
func registryUser(s User) error {
if _, ok := m[s.Account]; ok {
return errors.Errorf("User alread Used.")
}
su := s
su.Pass = hashBymd5(s.Pass)
m[s.Account] = su
return nil
}
func loginUser(u User) error {
u, ok := m[u.Account]
if !ok {
return errors.Errorf("User not found.")
}
if u.Pass != hashBymd5(u.Pass) {
return errors.Errorf("Pass not found.")
}
return nil
}
func hashBymd5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}