// NewUser creates a user func NewUser(username string, password string, init2FA string) (*UserInformation, error) { hmacKey := []byte(viper.GetString("hmackey")) // Create new instace of scryptauth with strength factor 12 and hmac_key pwhash, err := scryptauth.New(12, hmacKey) if err != nil { logrus.Error(err) return nil, err } hash, salt, err := pwhash.Gen([]byte(password)) if err != nil { logrus.Error(err) return nil, err } str := scryptauth.EncodeBase64(pwhash.PwCost, hash, salt) //fmt.Printf("hash=%x salt=%x\n", hash, salt) //fmt.Printf("base64ed: %s\n", str) u := &UserInformation{ Username: username, ScryptPassword: str, Init2FA: init2FA, LastConnection: time.Now(), ResetRequired: false, } return u, nil }
func checkPassword(username, password string) bool { db := database.GetDB() hmacKey := []byte(viper.GetString("hmackey")) pwhash, err := scryptauth.New(12, hmacKey) if err != nil { logrus.Error(err) return false } // Find the user for _, item := range db.Users { if item.Username == username { // found ! pwCost, hash, salt, err := scryptauth.DecodeBase64(item.ScryptPassword) if err != nil { logrus.Error(err) return false } ok, err := pwhash.Check(pwCost, hash, []byte(password), salt) return ok } } logrus.Infof("Username %s not found in the database", username) return false }