本文整理汇总了Golang中golang.org/x/crypto/bcrypt.GenerateFromPassword函数的典型用法代码示例。如果您正苦于以下问题:Golang GenerateFromPassword函数的具体用法?Golang GenerateFromPassword怎么用?Golang GenerateFromPassword使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GenerateFromPassword函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: tryCrypto
func tryCrypto() {
pwd := "pwd"
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
if err != nil {
P("bcrypt err: ", err)
}
P("first, pwd and hansh: \n", pwd, string(hash), len(string(hash)))
hash2, _ := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
P("second, pwd and hash: \n", pwd, string(hash2), len(string(hash2)))
P("check pwd..")
P("check hash1: ")
err = bcrypt.CompareHashAndPassword(hash, []byte(pwd))
P(err == nil)
err = bcrypt.CompareHashAndPassword(hash, []byte("pwds"))
P(err == nil)
P("check has2:")
P("hash1 != hash2: ", string(hash) != string(hash2))
err = bcrypt.CompareHashAndPassword(hash2, []byte(pwd))
P(err == nil)
u := uuid.New()
P("uuid: ", u, len(u), len(uuid.New()), len(uuid.New()))
unix := time.Now().Unix()
unixStr := fmt.Sprintf("%d", unix)
P("time: ", unix, len(unixStr))
}
示例2: main
func main() {
p := "todd"
bs, _ := bcrypt.GenerateFromPassword([]byte(p), bcrypt.MinCost)
fmt.Printf("PASSWORD ONE: %x \n", bs)
p2 := "todd"
bs2, _ := bcrypt.GenerateFromPassword([]byte(p2), bcrypt.MinCost)
fmt.Printf("PASSWORD TWO: %x \n", bs2)
}
示例3: main
func main() {
passwd := "archer"
crypted, _ := bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.DefaultCost)
fmt.Println(string(crypted))
crypteda, _ := bcrypt.GenerateFromPassword([]byte("Archer"), bcrypt.DefaultCost)
fmt.Println(string(crypteda))
if err := bcrypt.CompareHashAndPassword(crypted, []byte("Archer")); err != nil {
fmt.Println("not equal")
}
}
示例4: merge
// merge applies the properties of the passed-in User to the User on which it
// is called and returns a new User with these modifications applied. Think of
// all Users as immutable sets of data. Merge allows you to perform the set
// operations (desired grants and revokes) atomically
func (u User) merge(n User) (User, error) {
var out User
if u.User != n.User {
return out, authErr(http.StatusConflict, "Merging user data with conflicting usernames: %s %s", u.User, n.User)
}
out.User = u.User
if n.Password != "" {
hash, err := bcrypt.GenerateFromPassword([]byte(n.Password), bcrypt.DefaultCost)
if err != nil {
return User{}, err
}
out.Password = string(hash)
} else {
out.Password = u.Password
}
currentRoles := types.NewUnsafeSet(u.Roles...)
for _, g := range n.Grant {
if currentRoles.Contains(g) {
plog.Noticef("granting duplicate role %s for user %s", g, n.User)
return User{}, authErr(http.StatusConflict, fmt.Sprintf("Granting duplicate role %s for user %s", g, n.User))
}
currentRoles.Add(g)
}
for _, r := range n.Revoke {
if !currentRoles.Contains(r) {
plog.Noticef("revoking ungranted role %s for user %s", r, n.User)
return User{}, authErr(http.StatusConflict, fmt.Sprintf("Revoking ungranted role %s for user %s", r, n.User))
}
currentRoles.Remove(r)
}
out.Roles = currentRoles.Values()
sort.Strings(out.Roles)
return out, nil
}
示例5: DefaultPasswordHasher
func DefaultPasswordHasher(s string) ([]byte, error) {
pwHash, err := bcrypt.GenerateFromPassword([]byte(s), bcryptHashCost)
if err != nil {
return nil, err
}
return Password(pwHash), nil
}
示例6: CreateUser
func (c *Client) CreateUser(name, password string, admin bool) (*UserInfo, error) {
c.mu.Lock()
defer c.mu.Unlock()
data := c.cacheData.Clone()
// See if the user already exists.
if u := data.User(name); u != nil {
if err := bcrypt.CompareHashAndPassword([]byte(u.Hash), []byte(password)); err != nil || u.Admin != admin {
return nil, ErrUserExists
}
return u, nil
}
// Hash the password before serializing it.
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
if err != nil {
return nil, err
}
if err := data.CreateUser(name, string(hash), admin); err != nil {
return nil, err
}
u := data.User(name)
if err := c.commit(data); err != nil {
return nil, err
}
return u, nil
}
示例7: authGenerate
func authGenerate(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(hash), nil
}
示例8: UserAdd
func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error) {
plog.Noticef("adding a new user: %s", r.Name)
hashed, err := bcrypt.GenerateFromPassword([]byte(r.Password), bcrypt.DefaultCost)
if err != nil {
plog.Errorf("failed to hash password: %s", err)
return nil, err
}
tx := as.be.BatchTx()
tx.Lock()
defer tx.Unlock()
_, vs := tx.UnsafeRange(authUsersBucketName, []byte(r.Name), nil, 0)
if len(vs) != 0 {
return &pb.AuthUserAddResponse{}, ErrUserAlreadyExist
}
newUser := authpb.User{
Name: []byte(r.Name),
Password: hashed,
}
marshaledUser, merr := newUser.Marshal()
if merr != nil {
plog.Errorf("failed to marshal a new user data: %s", merr)
return nil, merr
}
tx.UnsafePut(authUsersBucketName, []byte(r.Name), marshaledUser)
plog.Noticef("added a new user: %s", r.Name)
return &pb.AuthUserAddResponse{}, nil
}
示例9: Signup
// Signup create a new record of user.
func (u *User) Signup(tx *sqlx.Tx, email, password, passwordAgain string) (*UserRow, error) {
if email == "" {
return nil, errors.New("Email cannot be blank.")
}
if password == "" {
return nil, errors.New("Password cannot be blank.")
}
if password != passwordAgain {
return nil, errors.New("Password is invalid.")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 5)
if err != nil {
return nil, err
}
data := make(map[string]interface{})
data["email"] = email
data["password"] = hashedPassword
sqlResult, err := u.InsertIntoTable(tx, data)
if err != nil {
return nil, err
}
return u.userRowFromSqlResult(tx, sqlResult)
}
示例10: SetPassword
func (u *User) SetPassword(password string) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), -1)
if err != nil {
panic(err) // Luke says this is OK
}
u.PwHash = hash
}
示例11: newCryptPasswd
//Hash le password envoyé en parametre et le met dans la variable hashedPassword dans la structure
func (user *User) newCryptPasswd(password []byte) {
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
panic(err)
}
user.hashedPassword = hashedPassword
}
示例12: CreateUser
func CreateUser(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
ctx := appengine.NewContext(req)
// Form validation skeleton
//
// var PassVerify = req.FormValue("passWordVerify")
// var Password = req.FormValue("passWord")
//
// if(PassVerify != Password){
//
// }
var password, _ = bcrypt.GenerateFromPassword([]byte(req.FormValue("passWord")), bcrypt.DefaultCost)
user := User{
FirstName: req.FormValue("firstName"),
LastName: req.FormValue("lastName"),
UserName: req.FormValue("userName"),
Email: req.FormValue("email"),
Password: string(password),
}
key := datastore.NewKey(ctx, "Users", user.UserName, 0, nil)
key, err := datastore.Put(ctx, key, &user)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
var sd SessionData
sd.LoggedIn = true
tpl.ExecuteTemplate(res, "homePage.html", &sd)
}
示例13: initAuth
func initAuth() {
var err error
// authFile must exist in site home.
// could intruduce a way to dynamically create one or just default install it
backend, err = httpauth.NewGobFileAuthBackend(authFile)
if err != nil {
panic(err)
}
// create some default roles
roles = make(map[string]httpauth.Role)
roles["user"] = 30
roles["admin"] = 80
aaa, err = httpauth.NewAuthorizer(backend, []byte("cookie-encryption-key"), "user", roles)
// create a default user
hash, err := bcrypt.GenerateFromPassword([]byte("adminadmin"), bcrypt.DefaultCost)
if err != nil {
panic(err)
}
defaultUser := httpauth.UserData{Username: "admin", Email: "[email protected]", Hash: hash, Role: "admin"}
err = backend.SaveUser(defaultUser)
if err != nil {
panic(err)
}
}
示例14: HashPassword
//HashPassword takes a plaintext password and hashes it with bcrypt and sets the
//password field to the hash.
func (u *User) HashPassword(password string) {
hpass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
panic(err) //this is a panic because bcrypt errors on invalid costs
}
u.Password = string(hpass)
}
示例15: HashPass
/*
HashPass apply hashing
*/
func HashPass(pass string) (string, error) {
if hash, err := bcrypt.GenerateFromPassword([]byte(pass), cost); err != nil {
return "", err
} else {
return string(hash), nil
}
}