// Create a new storage using the given secret func NewStorage(secret []byte) (*SecureStorage, error) { err := isValidData(secret) if err != nil { return nil, err } saltData, _ := salt.GetRandomSalt(SaltLen) pass := getSaltedPass(secret, saltData) s := SecureStorage{Data: make(SecureDataMap), secret: pass, Salt: saltData} return &s, nil }
// Generate a new secure storage minimal file that includes the root user with // basic Account Management: the root user privilege and password func createBasicFile(stFilePath string, name string, pass string, key []byte) { saltStr, _ := salt.GetRandomSalt(saltLen) _, err := salt.GenerateSaltedPassword([]byte(pass), password.MinPasswordLength, password.MaxPasswordLength, saltStr, -1) if err != nil { log.Fatalf("Error: can't generate salted password for '%v' user, error: %v", name, err) } ul := en.NewEntityManager() ul.AddUser(name) amUser, _ := am.NewUserAm(am.SuperUserPermission, []byte(pass), saltStr) ul.AddPropertyToEntity(name, stc.AmPropertyName, amUser) ul.StoreInfo(stFilePath, key) }
func getRandomSaltExample(secret []byte, saltLen int, iter int, f func() hash.Hash, size int) ([]byte, []byte, error) { salting, _ := salt.GetRandomSalt(saltLen) mySalt, err := salt.NewSalt(secret, minSecretLen, maxSecretLen, salting) if err != nil { return nil, nil, err } mySalt.Iterations = iter mySalt.OutputLen = size mySalt.Digest = f res, err := mySalt.Generate(minSecretLen, maxSecretLen) if err != nil { return nil, nil, err } else { return res, salting, nil } }
func (l amRestful) restAddAm(request *restful.Request, response *restful.Response) { name := request.PathParameter(userIdParam) privilege := l.getPrivilegePwd(request, response) if privilege == nil { return } saltStr, _ := salt.GetRandomSalt(SaltLen) data, err := am.NewUserAm(privilege.Privilege, []byte(privilege.Password), saltStr) if err != nil { l.setError(response, http.StatusBadRequest, err) return } l.st.UsersList.AddPropertyToEntity(name, stc.AmPropertyName, data) response.WriteHeader(http.StatusCreated) response.WriteEntity(l.getUrlPath(request, name)) }
// This example shows how to generate a randomly salted secret contain 8 characters, using hash function of md5 and 3 iterations of calculations func ExampleGetRandomSalt() { iter := 3 f := md5.New size := 8 saltLen := 32 randSalt, _ := salt.GetRandomSalt(saltLen) mySalt, err := salt.NewSalt(BasicSecret, minSecretLen, maxSecretLen, randSalt) if err != nil { fmt.Println("Error while creating new salt structure:", err) } mySalt.Iterations = iter mySalt.OutputLen = size mySalt.Digest = f res, err := mySalt.Generate(minSecretLen, maxSecretLen) if err != nil { fmt.Println("GetRandomSaltExample failed, error:", err) } else { fmt.Println("* Salted password of secret key:", string(BasicSecret), ",random salt length:", saltLen, randSalt, "with", iter, "iterations, output password length:", size, "bytes and MD5 function is:", res) } }
// Example of how to use the reset password function: // This function resets the current password, // selects a new password with short expiration time // and lets the user use it exactly once func ExampleUserPwd_ResetPasword() { id := "User1" pwd := []byte("a1b2c3d") saltStr, _ := salt.GetRandomSalt(10) userPwd, _ := password.NewUserPwd(pwd, saltStr) tmpPwd, _ := userPwd.ResetPasword() tPwd, _ := salt.GenerateSaltedPassword(tmpPwd, 1, 100, saltStr, -1) newPwd := password.GetHashedPwd(tPwd) err := userPwd.IsPasswordMatch(newPwd) if err != nil { fmt.Printf("Check of newly generated password: '******' for user: %v failed, error: %v\n", newPwd, id, err) } else { fmt.Printf("Entity %v, after reseting password '%v' verified successfuly\n", id, newPwd) } err = userPwd.IsPasswordMatch(newPwd) if err == nil { fmt.Printf("Error: Newly generated password: '******' could be used only once\n", newPwd) } else { fmt.Printf("Newly generated password: '******', for entity: %v, can only be used once\n", newPwd, id) } }
// Example of how to use the password. // 1. Create a new password. // 2. Verify that the initial password is set correctly // 3. Change the user's password // 4. Verify that the old password is not valid anymore // 5. Verify that the new password is valid // 6. Verify that the old password can't be used any more // (at least not as long as it remains in the old passwords list) func ExampleUserPwd() { id := "User-1" pwd := []byte("a1b2c3d") saltStr, _ := salt.GetRandomSalt(8) userPwd, _ := password.NewUserPwd(pwd, saltStr) tPwd, _ := salt.GenerateSaltedPassword(pwd, minPasswordLength, maxPasswordLength, saltStr, -1) newPwd := password.GetHashedPwd(tPwd) err := userPwd.IsPasswordMatch(newPwd) if err != nil { fmt.Println("Ravid: error", err) } userNewPwd := []byte(string(pwd) + "a") newPwd, err = userPwd.UpdatePassword(userPwd.Password, userNewPwd) if err != nil { fmt.Printf("Password update for user %v to new password '%v' (%v) failed, error: %v\n", id, newPwd, string(userNewPwd), err) } else { fmt.Printf("User: '******', updated password to '%v' (%v)\n", id, newPwd, string(userNewPwd)) } err = userPwd.IsPasswordMatch(newPwd) if err != nil { fmt.Printf("Check of the new password: '******' (%v) for user: %v failed, error: %v\n", newPwd, string(userNewPwd), id, err) } else { fmt.Printf("User: '******', new password '%v' (%v) verified successfuly\n", id, newPwd, string(userNewPwd)) } err = userPwd.IsPasswordMatch(pwd) if err == nil { fmt.Printf("Error: Old password: '******' (%v) for user: %v accepted\n", pwd, string(pwd), id) } else { fmt.Printf("User: '******', Note that the old password '%v' (%v) can't be used anymore\n", id, pwd, string(pwd)) } newPwd, err = userPwd.UpdatePassword(userPwd.Password, pwd) if err == nil { fmt.Printf("Error: Password '%v' (typed password %v) for user %v was alredy used\n", newPwd, string(pwd), id) } else { fmt.Printf("Entity: '%v', Note that the old password (entered password) %v as it was already used\n", id, string(pwd)) } }
func NewPwdRestful() *pwdRestful { saltStr, _ := salt.GetRandomSalt(saltLen) return &pwdRestful{nil, saltStr} }