-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
33 lines (29 loc) · 892 Bytes
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package wirepusher
type User struct {
Username string // a username
password []byte // sha1-hashed password for message encryption
ID string // wirepusherID
}
// NewUser creates a new user and hashes the (optional) password when it is provided.
func NewUser(userName string, id string, password string) *User {
usr := &User{Username: userName, ID: id}
if len(password) > 0 {
hPass, err := hash(password)
if err != nil {
panic(err)
}
usr.password = hPass
}
return usr
}
// Password returns the first AES_KEY_SIZE bytes of the password hash and converts it to a hex-encoded string
func (u *User) Password() string {
if len(u.password) == 0 {
return ""
}
return bin2hex(u.password[:AES_KEY_SIZE])
}
// CanEncrypt returns true when a password is provided and the message can therefore be encrypted
func (u *User) CanEncrypt() bool {
return len(u.password) > 0
}