]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
aebc4d36 JH |
5 | "golang.org/x/crypto/bcrypt" |
6 | "log" | |
6988a057 JH |
7 | ) |
8 | ||
9 | // User flags are stored as a 2 byte bitmap with the following values: | |
10 | const ( | |
11 | userFlagAway = 0 // User is away | |
12 | userFlagAdmin = 1 // User is admin | |
13 | userFlagRefusePM = 2 // User refuses private messages | |
14 | userFLagRefusePChat = 3 // User refuses private chat | |
15 | ) | |
16 | ||
003a743e JH |
17 | // fieldOptions flags are sent from v1.5+ clients as part of tranAgreed |
18 | const ( | |
19 | refusePM = 0 // User has "Refuse private messages" pref set | |
20 | refuseChat = 1 // User has "Refuse private chat" pref set | |
21 | autoResponse = 2 // User has "Automatic response" pref set | |
22 | ) | |
23 | ||
6988a057 JH |
24 | type User struct { |
25 | ID []byte // Size 2 | |
26 | Icon []byte // Size 2 | |
27 | Flags []byte // Size 2 | |
28 | Name string // Variable length user name | |
29 | } | |
30 | ||
31 | func (u User) Payload() []byte { | |
32 | nameLen := make([]byte, 2) | |
33 | binary.BigEndian.PutUint16(nameLen, uint16(len(u.Name))) | |
34 | ||
35 | if len(u.Icon) == 4 { | |
36 | u.Icon = u.Icon[2:] | |
37 | } | |
38 | ||
39 | if len(u.Flags) == 4 { | |
40 | u.Flags = u.Flags[2:] | |
41 | } | |
42 | ||
43 | out := append(u.ID[:2], u.Icon[:2]...) | |
44 | out = append(out, u.Flags[:2]...) | |
45 | out = append(out, nameLen...) | |
46 | out = append(out, u.Name...) | |
47 | ||
48 | return out | |
49 | } | |
50 | ||
51 | func ReadUser(b []byte) (*User, error) { | |
52 | u := &User{ | |
53 | ID: b[0:2], | |
54 | Icon: b[2:4], | |
55 | Flags: b[4:6], | |
56 | Name: string(b[8:]), | |
57 | } | |
58 | return u, nil | |
59 | } | |
60 | ||
61 | // DecodeUserString decodes an obfuscated user string from a client | |
62 | // e.g. 98 8a 9a 8c 8b => "guest" | |
b25c4a19 JH |
63 | func DecodeUserString(obfuText []byte) (clearText string) { |
64 | for _, char := range obfuText { | |
65 | clearText += string(rune(255 - uint(char))) | |
6988a057 | 66 | } |
b25c4a19 | 67 | return clearText |
6988a057 JH |
68 | } |
69 | ||
b25c4a19 JH |
70 | // negateString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext. |
71 | // The Hotline protocol uses this format for sending passwords over network. | |
72 | // Not secure, but hey, it was the 90s! | |
73 | func negateString(clearText []byte) []byte { | |
74 | obfuText := make([]byte, len(clearText)) | |
75 | for i := 0; i < len(clearText); i++ { | |
76 | obfuText[i] = 255 - clearText[i] | |
6988a057 | 77 | } |
b25c4a19 | 78 | return obfuText |
6988a057 | 79 | } |
aebc4d36 JH |
80 | |
81 | func hashAndSalt(pwd []byte) string { | |
82 | // Use GenerateFromPassword to hash & salt pwd. | |
83 | // MinCost is just an integer constant provided by the bcrypt | |
84 | // package along with DefaultCost & MaxCost. | |
85 | // The cost can be any value you want provided it isn't lower | |
86 | // than the MinCost (4) | |
87 | hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) | |
88 | if err != nil { | |
89 | log.Println(err) | |
90 | } | |
91 | // GenerateFromPassword returns a byte slice so we need to | |
92 | // convert the bytes to a string and return it | |
93 | return string(hash) | |
94 | } |