2015-09-07 17:48:19 -04:00
|
|
|
package core
|
|
|
|
|
2015-09-08 07:18:55 -04:00
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
)
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
type UserSet struct {
|
|
|
|
validUserIds []ID
|
2015-09-08 07:18:55 -04:00
|
|
|
userIdsAskHash map[string]int
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func NewUserSet() *UserSet {
|
|
|
|
vuSet := new(UserSet)
|
|
|
|
vuSet.validUserIds = make([]ID, 0, 16)
|
2015-09-08 19:11:02 -04:00
|
|
|
vuSet.userIdsAskHash = make(map[string]int)
|
2015-09-08 07:18:55 -04:00
|
|
|
return vuSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashBytesToString(hash []byte) string {
|
|
|
|
return base64.StdEncoding.EncodeToString(hash)
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (us *UserSet) AddUser(user User) error {
|
2015-09-08 07:18:55 -04:00
|
|
|
id := user.Id
|
|
|
|
us.validUserIds = append(us.validUserIds, id)
|
|
|
|
|
|
|
|
idBase64 := hashBytesToString(id.Hash([]byte("ASK")))
|
|
|
|
us.userIdsAskHash[idBase64] = len(us.validUserIds) - 1
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func (us UserSet) IsValidUserId(askHash []byte) (*ID, bool) {
|
2015-09-08 07:18:55 -04:00
|
|
|
askBase64 := hashBytesToString(askHash)
|
|
|
|
idIndex, found := us.userIdsAskHash[askBase64]
|
|
|
|
if found {
|
|
|
|
return &us.validUserIds[idIndex], true
|
|
|
|
}
|
|
|
|
return nil, false
|
2015-09-07 17:48:19 -04:00
|
|
|
}
|