-
Notifications
You must be signed in to change notification settings - Fork 0
/
blacklist.go
51 lines (43 loc) · 1.08 KB
/
blacklist.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
package blacklist
import (
"crypto/sha1"
"encoding/gob"
"hash"
"io"
)
// Blacklist implements a blacklist wuth support for adding items and checking
// whether they exist.
type Blacklist struct {
items map[string]struct{}
hash hash.Hash
}
// New returns a new Blacklist
func New() *Blacklist {
return &Blacklist{
items: make(map[string]struct{}),
hash: sha1.New(),
}
}
// Add adds the given item to the blacklist.
func (b *Blacklist) Add(item []byte) {
b.items[string(b.hashItem(item))] = struct{}{}
}
// Has returns true if the given item is in the list, otherwise false.
func (b *Blacklist) Has(item []byte) bool {
_, ok := b.items[string(b.hashItem(item))]
return ok
}
func (b *Blacklist) hashItem(item []byte) []byte {
b.hash.Write(item)
h := b.hash.Sum(nil)
b.hash.Reset()
return h[:len(h)/2]
}
// ExportState exports the state of the list.
func (b *Blacklist) ExportState(w io.Writer) error {
return gob.NewEncoder(w).Encode(b.items)
}
// ImportState imports the given state.
func (b *Blacklist) ImportState(r io.Reader) error {
return gob.NewDecoder(r).Decode(&b.items)
}