/
obfuscators.go
135 lines (120 loc) · 3.13 KB
/
obfuscators.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
// Scramble functions.
// Input `s []byte` is required to be not nil.
package main
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"net"
)
var Salt []byte
const emailDomain = "@example.com"
func GenScrambleBytes(maxLength uint) func([]byte) []byte {
return func(s []byte) []byte {
// TODO: pad or extend s to maxLength
return ScrambleBytes(s)[:maxLength]
}
}
var bytesOutputAlphabetLength = byte(len(bytesOutputAlphabet))
var bytesKeep = []byte("',\\{}")
var bytesOutputAlphabet = []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-_")
// Modifies `s` in-place.
func ScrambleBytes(s []byte) []byte {
isArray := len(s) >= 2 && s[0] == '{' && s[len(s)-1] == '}'
hash := sha256.New()
// Hard-coding this constant wins less than 3% in BenchmarkScrambleBytes
const sumLength = 32 // SHA256/8
hash.Write(Salt)
hash.Write(s)
sumBytes := hash.Sum(nil)
reader := bytes.NewReader(s)
var r rune
var err error
for i := 0; ; i++ {
r, _, err = reader.ReadRune()
if err != nil {
s = s[:i]
break
}
if !isArray || bytes.IndexRune(bytesKeep, r) == -1 {
// Do not insert, so should not obstruct reader.
s[i] = bytesOutputAlphabet[(sumBytes[i%sumLength]+byte(r))%bytesOutputAlphabetLength]
} else {
// Possibly shift bytes to beginning of s.
s[i] = byte(r)
}
}
return s
}
func ScrambleDigits(s []byte) []byte {
hash := sha256.New()
const sumLength = 32 // SHA256/8
hash.Write(Salt)
hash.Write(s)
sumBytes := hash.Sum(nil)
for i, b := range s {
if b >= '0' && b <= '9' {
s[i] = '0' + (sumBytes[i%sumLength]+b)%10
}
}
return s
}
func scrambleOneEmail(s []byte) []byte {
atIndex := bytes.IndexRune(s, '@')
mailbox := Salt
if atIndex != -1 {
mailbox = s[:atIndex]
}
s = make([]byte, len(mailbox)+len(emailDomain))
copy(s, mailbox)
// ScrambleBytes is in-place, but may return string shorter than input.
mailbox = ScrambleBytes(s[:len(mailbox)])
copy(s[len(mailbox):], emailDomain)
// So final len(mailbox) may be shorter than whole allocated string.
return s[:len(mailbox)+len(emailDomain)]
}
// Supports array of emails in format {email1,email2}
func ScrambleEmail(s []byte) []byte {
if len(s) < 2 {
// panic("ScrambleEmail: input is too small: '" + string(s) + "'")
return s
}
if s[0] != '{' && s[len(s)-1] != '}' {
return scrambleOneEmail(s)
}
parts := bytes.Split(s[1:len(s)-1], []byte{','})
partsNew := make([][]byte, len(parts))
outLength := 2 + len(parts) - 1
for i, bs := range parts {
partsNew[i] = scrambleOneEmail(bs)
outLength += len(partsNew[i])
}
s = make([]byte, outLength)
s[0] = '{'
s[len(s)-1] = '}'
copy(s[1:len(s)-1], bytes.Join(partsNew, []byte{','}))
return s
}
func ScrambleInet(s []byte) []byte {
hash := sha256.New()
const sumLength = 32 // SHA256/8
hash.Write(Salt)
hash.Write(s)
sumBytes := hash.Sum(nil)
var ip net.IP
// Decide to output IPv4 or IPv6 based on first bit of hash sum.
// Gives about 50% chance to each option.
if sumBytes[0]&0x80 != 0 {
ip = net.IP(sumBytes[:16])
} else {
ip = net.IP(sumBytes[:4])
}
return []byte(ip.String())
}
func init() {
Salt = make([]byte, 16)
_, err := rand.Read(Salt)
if err != nil {
panic(err)
}
}