/
friendcode.go
174 lines (142 loc) · 2.89 KB
/
friendcode.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package csfriendcode
import (
"crypto/md5"
"encoding/binary"
"errors"
"math/bits"
)
const (
alnum = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
defaultSteamID = 0x110000100000000
)
var (
ralnum = map[byte]uint64{
'A': 0,
'B': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7,
'J': 8,
'K': 9,
'L': 10,
'M': 11,
'N': 12,
'P': 13,
'Q': 14,
'R': 15,
'S': 16,
'T': 17,
'U': 18,
'V': 19,
'W': 20,
'X': 21,
'Y': 22,
'Z': 23,
'2': 24,
'3': 25,
'4': 26,
'5': 27,
'6': 28,
'7': 29,
'8': 30,
'9': 31,
}
)
// b32 is some kind of base32 that is used
// to encode the value that is created from the initial
// function. For some reason that I cant figure out
// it creates a different result to using go's base32 function
// even though they should basically do exactly the same thing
func b32(input uint64) string {
result := []byte{}
// big endian the number
input = bits.ReverseBytes64(input)
for i := 0; i < 13; i++ {
if i == 4 || i == 9 {
result = append(result, '-')
}
result = append(result, alnum[input&0x1F])
input >>= 5
}
return string(result)
}
func rb32(input string) uint64 {
result := uint64(0)
for i := 0; i < 13; i++ {
if i == 4 || i == 9 {
input = input[1:]
}
result |= ralnum[input[0]] << (5 * i)
input = input[1:]
}
return bits.ReverseBytes64(result)
}
func hashSteamID(id uint64) (uint32, error) {
accountID := uint32(id)
strangeSteamID := uint64(accountID) | 0x4353474F00000000
steamIDBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(steamIDBytes, strangeSteamID)
h := md5.New()
n, err := h.Write(steamIDBytes)
if err != nil {
return 0, err
}
if n != 8 {
return 0, errors.New("Couldnt hash steamid")
}
hashSteamID := h.Sum(nil)
return binary.LittleEndian.Uint32(hashSteamID), nil
}
// makeU64 takes a high and low uint32
// and makes a uint64 out of them
func makeU64(hi uint32, lo uint32) uint64 {
return uint64((uint64(hi) << 32) | uint64(lo))
}
func friendCode(id uint64) string {
h, err := hashSteamID(id)
if err != nil {
return ""
}
r := uint64(0)
for i := 0; i < 8; i++ {
idNibble := byte(id & 0xF)
id >>= 4
hashNibble := (h >> i) & 1
a := uint32(r<<4) | uint32(idNibble)
r = makeU64(uint32(r>>28), a)
r = makeU64(uint32(r>>31), a<<1|hashNibble)
}
return b32(r)
}
// Encode gets a friend code based on a provided steamid64
func Encode(id uint64) string {
fc := friendCode(id)
if fc[:5] == "AAAA-" {
fc = fc[5:]
}
return fc
}
func steamID(fc string) uint64 {
val := rb32(fc)
id := uint32(0)
for i := 0; i < 8; i++ {
// hashBit := val & 0x1
val >>= 1
idNibble := uint32(val & 0xF)
val >>= 4
id <<= 4
id |= idNibble
}
return uint64(id) | defaultSteamID
}
// Decode gets a steamid from a friendcode
func Decode(friendCode string) uint64 {
if len(friendCode) != 10 {
return 0
}
friendCode = "AAAA-" + friendCode
return steamID(friendCode)
}