]>
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 | ||
17 | type User struct { | |
18 | ID []byte // Size 2 | |
19 | Icon []byte // Size 2 | |
20 | Flags []byte // Size 2 | |
21 | Name string // Variable length user name | |
22 | } | |
23 | ||
24 | func (u User) Payload() []byte { | |
25 | nameLen := make([]byte, 2) | |
26 | binary.BigEndian.PutUint16(nameLen, uint16(len(u.Name))) | |
27 | ||
28 | if len(u.Icon) == 4 { | |
29 | u.Icon = u.Icon[2:] | |
30 | } | |
31 | ||
32 | if len(u.Flags) == 4 { | |
33 | u.Flags = u.Flags[2:] | |
34 | } | |
35 | ||
36 | out := append(u.ID[:2], u.Icon[:2]...) | |
37 | out = append(out, u.Flags[:2]...) | |
38 | out = append(out, nameLen...) | |
39 | out = append(out, u.Name...) | |
40 | ||
41 | return out | |
42 | } | |
43 | ||
44 | func ReadUser(b []byte) (*User, error) { | |
45 | u := &User{ | |
46 | ID: b[0:2], | |
47 | Icon: b[2:4], | |
48 | Flags: b[4:6], | |
49 | Name: string(b[8:]), | |
50 | } | |
51 | return u, nil | |
52 | } | |
53 | ||
54 | // DecodeUserString decodes an obfuscated user string from a client | |
55 | // e.g. 98 8a 9a 8c 8b => "guest" | |
b25c4a19 JH |
56 | func DecodeUserString(obfuText []byte) (clearText string) { |
57 | for _, char := range obfuText { | |
58 | clearText += string(rune(255 - uint(char))) | |
6988a057 | 59 | } |
b25c4a19 | 60 | return clearText |
6988a057 JH |
61 | } |
62 | ||
b25c4a19 JH |
63 | // negateString takes []byte s containing cleartext and rotates by 255 into obfuscated cleartext. |
64 | // The Hotline protocol uses this format for sending passwords over network. | |
65 | // Not secure, but hey, it was the 90s! | |
66 | func negateString(clearText []byte) []byte { | |
67 | obfuText := make([]byte, len(clearText)) | |
68 | for i := 0; i < len(clearText); i++ { | |
69 | obfuText[i] = 255 - clearText[i] | |
6988a057 | 70 | } |
b25c4a19 | 71 | return obfuText |
6988a057 | 72 | } |
aebc4d36 JH |
73 | |
74 | func hashAndSalt(pwd []byte) string { | |
75 | // Use GenerateFromPassword to hash & salt pwd. | |
76 | // MinCost is just an integer constant provided by the bcrypt | |
77 | // package along with DefaultCost & MaxCost. | |
78 | // The cost can be any value you want provided it isn't lower | |
79 | // than the MinCost (4) | |
80 | hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost) | |
81 | if err != nil { | |
82 | log.Println(err) | |
83 | } | |
84 | // GenerateFromPassword returns a byte slice so we need to | |
85 | // convert the bytes to a string and return it | |
86 | return string(hash) | |
87 | } |