本文整理匯總了Golang中github.com/keep94/vsafe.User.InitWithKey方法的典型用法代碼示例。如果您正苦於以下問題:Golang User.InitWithKey方法的具體用法?Golang User.InitWithKey怎麽用?Golang User.InitWithKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/keep94/vsafe.User
的用法示例。
在下文中一共展示了User.InitWithKey方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestUser
func TestUser(t *testing.T) {
user := vsafe.User{Id: 1}
var err error
var key *vsafe.Key
var key2 *vsafe.Key
if err = user.Init("keep94", "somepassword"); err != nil {
t.Fatalf("Error creating user: %v", err)
}
if key, err = user.VerifyPassword("somepassword"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
if _, err = user.VerifyPassword("aardvark"); err != vsafe.ErrWrongPassword {
t.Errorf("Expected wrong password, got %v", err)
}
user2 := vsafe.User{Id: 2}
if err = user2.InitWithKey("keep94+admin", "foo", key); err != nil {
t.Fatalf("Error creating user: %v", err)
}
if key2, err = user2.VerifyPassword("foo"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
if !key2.Equal(key) {
t.Error("key and key2 should be equal.")
}
if key.Id != 1 {
t.Error("Expected key Id to be 1")
}
}
示例2: main
func main() {
flag.Parse()
if fDb == "" || fName == "" || fPassword == "" {
fmt.Println("Need to specify -db, -name, and -password flag.")
flag.Usage()
return
}
conn, err := sqlite.Open(fDb)
if err != nil {
fmt.Printf("Unable to open database - %s\n", fDb)
return
}
dbase := sqlite_db.New(conn)
defer dbase.Close()
err = dbase.Do(func(conn *sqlite.Conn) error {
return sqlite_setup.SetUpTables(conn)
})
if err != nil {
fmt.Printf("Unable to create tables - %v\n", err)
return
}
store := for_sqlite.New(dbase)
var user vsafe.User
if fMasterName == "" {
err = user.Init(fName, fPassword)
} else {
var master vsafe.User
if err = store.UserByName(nil, fMasterName, &master); err != nil {
fmt.Printf("Error retrieving master user - %v\n", err)
return
}
var key *vsafe.Key
if key, err = master.VerifyPassword(fMasterPassword); err != nil {
fmt.Printf("Error verifying master password - %v\n", err)
return
}
err = user.InitWithKey(fName, fPassword, key)
}
if err != nil {
fmt.Printf("Error initializing user - %v\n", err)
return
}
if err = store.AddUser(nil, &user); err != nil {
fmt.Printf("Error storing user in database - %v\n", err)
return
}
}