// Create a new APIKey func (a *APIKey) Create(userID int) error { a.UserID = userID // Generate API pubkey using a random SHA1 hash sha := sha1.New() if _, err := sha.Write([]byte(common.RandString())); err != nil { return err } a.Pubkey = fmt.Sprintf("%x", sha.Sum(nil)) // Generate API secret using a random SHA1 hash sha2 := sha1.New() if _, err := sha2.Write([]byte(common.RandString())); err != nil { return err } a.Secret = fmt.Sprintf("%x", sha2.Sum(nil)) // Set key to expire one week from now a.Expire = time.Now().Add(7 * 24 * time.Hour).Unix() return nil }
// Create a UserRecord, using defined parameters func (u UserRecord) Create(username string, torrentLimit int) UserRecord { // Set username and torrent limit u.Username = username u.TorrentLimit = torrentLimit // Randomly generate a new passkey sha := sha1.New() if _, err := sha.Write([]byte(common.RandString())); err != nil { log.Println(err.Error()) return UserRecord{} } u.Passkey = fmt.Sprintf("%x", sha.Sum(nil)) return u }
// Create a UserRecord, using defined parameters func (u *UserRecord) Create(username string, password string, torrentLimit int) error { // Set username and torrent limit u.Username = username u.TorrentLimit = torrentLimit // Generate password hash using bcrypt hash, err := bcrypt.GenerateFromPassword([]byte(password), 12) if err != nil { return err } u.Password = string(hash) // Randomly generate a new passkey sha := sha1.New() if _, err := sha.Write([]byte(common.RandString())); err != nil { return err } u.Passkey = fmt.Sprintf("%x", sha.Sum(nil)) return nil }