// register create user func RegisterUser(user *models.User, username, email, password string, locale i18n.Locale) error { // use random salt encode password salt := models.GetUserSalt() pwd := utils.EncodePassword(password, salt) user.UserName = strings.ToLower(username) user.Email = strings.ToLower(email) // save salt and encode password, use $ as split char user.Password = fmt.Sprintf("%s$%s", salt, pwd) // save md5 email value for gravatar user.GrEmail = utils.EncodeMd5(user.Email) // Use username as default nickname. user.NickName = user.UserName //set default language if locale.Lang == "en-US" { user.Lang = setting.LangEnUS } else { user.Lang = setting.LangZhCN } //set default avatar user.AvatarType = setting.AvatarTypeGravatar return models.Insert(user) }
// compare raw password and encoded password func VerifyPassword(rawPwd, encodedPwd string) bool { // for discuz accounts if len(encodedPwd) == 39 { salt := encodedPwd[:6] encoded := encodedPwd[7:] return encoded == utils.EncodeMd5(utils.EncodeMd5(rawPwd)+salt) } // split var salt, encoded string if len(encodedPwd) > 11 { salt = encodedPwd[:10] encoded = encodedPwd[11:] } return utils.EncodePassword(rawPwd, salt) == encoded }
// set a new password to user func SaveNewPassword(user *models.User, password string) error { salt := models.GetUserSalt() user.Password = fmt.Sprintf("%s$%s", salt, utils.EncodePassword(password, salt)) return models.UpdateById(user.Id, user, "password", "rands", "updated") }