/
ixhash.go
199 lines (175 loc) · 4.16 KB
/
ixhash.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Package ixhash implements the iXhash fuzzy e-mail hashing algorithms
// and provides facilities for checking iXhash digests against DNS lists.
package ixhash // import "cgt.name/pkg/ixhash"
import (
"bytes"
"crypto/md5"
"fmt"
"net"
"regexp"
"unicode"
)
// Apparently the unicode package and PCRE disagree on what constitutes a
// graph or print symbol.
var (
graphPCRE = []*unicode.RangeTable{
unicode.L,
unicode.M,
unicode.N,
unicode.P,
unicode.S,
unicode.Cf,
}
printPCRE = []*unicode.RangeTable{
unicode.L,
unicode.M,
unicode.N,
unicode.P,
unicode.S,
unicode.Cf,
unicode.Zs,
}
)
// md5hash calculates the md5 hash of a byte slice and returns a hex digest.
func md5hash(b []byte) string {
h := md5.New()
h.Write(b)
return fmt.Sprintf("%x", h.Sum(nil))
}
// squeezeSpaces squeezes multiple duplicate spaces in a row to a single space
// character. Like: $ tr -s '[:space:]'
func squeezeSpaces(s []byte) []byte {
buf := make([]byte, 0, len(s))
prev := s[0]
buf = append(buf, prev)
for i := 1; i < len(s); i++ {
b := s[i]
issp := unicode.IsSpace(rune(b))
if !issp || (issp && b != prev) {
buf = append(buf, b)
}
prev = b
}
return buf
}
// squeeze multiple duplicate characters in a row to a single character.
func squeeze(s []byte, unirange []*unicode.RangeTable) []byte {
buf := make([]byte, 0, len(s))
prev := s[0]
buf = append(buf, prev)
for i := 1; i < len(s); i++ {
b := s[i]
match := unicode.IsOneOf(unirange, rune(b))
if !match || (match && b != prev) {
buf = append(buf, b)
}
prev = b
}
return buf
}
// FirstHash computes the first iXhash digest.
// FirstHash mutates the provided msg []byte.
// FirstHash requires that the message contain at least 20 spaces/tabs
// and at least two newlines.
func FirstHash(msg []byte) string {
if msg == nil {
return ""
}
var (
spaces int
newlines int
)
for _, c := range msg {
switch c {
case ' ', '\t':
spaces++
case '\n':
newlines++
}
}
if spaces < 20 || newlines < 2 {
return ""
}
msg = bytes.Replace(msg, []byte{'\r', '\n'}, []byte{'\n'}, -1)
buf := msg // reuse msg byte slice, squeezeSpaces returns a new one
msg = squeezeSpaces(msg)
buf = buf[:0]
for _, b := range msg {
if !unicode.IsOneOf(graphPCRE, rune(b)) {
buf = append(buf, b)
}
}
return md5hash(buf)
}
var (
reSecondHashOK = regexp.MustCompile(`((([<>\(\)\|@\*'!?,])|(:\/)).*?){3,}`)
reRedundantStuff = regexp.MustCompile(`[[:cntrl:][:alnum:]%&#;=]+`)
)
// SecondHash computes the second iXhash digest.
// SecondHash mutates the provided msg []byte.
func SecondHash(msg []byte) string {
if msg == nil {
return ""
}
if match := reSecondHashOK.Find(msg); match == nil {
return ""
}
msg = reRedundantStuff.ReplaceAll(msg, []byte{})
msg = bytes.Replace(msg, []byte{'_'}, []byte{' '}, -1)
msg = squeeze(msg, printPCRE)
return md5hash(msg)
}
// ThirdHash computes the third iXhash digest.
// ThirdHash does not mutate the provided msg []byte.
// ThirdHash requires that the message contain at least eight consecutive
// non-space characters.
func ThirdHash(msg []byte) string {
if msg == nil {
return ""
}
var i int
for _, b := range msg {
if unicode.IsSpace(rune(b)) {
i = 0
continue
}
i++
if i >= 8 {
break
}
}
if i < 8 {
return ""
}
buf := make([]byte, 0, len(msg))
for _, b := range msg {
r := rune(b)
if !(unicode.IsControl(r) || unicode.IsSpace(r) || r == '=') {
buf = append(buf, b)
}
}
buf = squeeze(buf, graphPCRE)
return md5hash(buf)
}
// All computes all three hashes of the msg, which should be the body of an
// e-mail (no headers). All returns an array where the FirstHash result is in
// at index 0, the SecondHash is at index 1, and the ThirdHash is at index 2.
// If a hash could not be computed, it will be an empty string.
func All(msg []byte) [3]string {
var ret [3]string
b := make([]byte, len(msg))
copy(b, msg)
ret[0] = FirstHash(b)
copy(b, msg)
ret[1] = SecondHash(b)
// ThirdHash does not mutate the slice.
ret[2] = ThirdHash(msg)
return ret
}
func Lookup(zone, digest string) (bool, error) {
addrs, err := net.LookupHost(digest + "." + zone)
if err != nil {
return false, err
}
return len(addrs) > 0, nil
}