forked from intenthq/anon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymisations.go
149 lines (135 loc) · 3.83 KB
/
anonymisations.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
package main
import (
"crypto/sha1"
"errors"
"fmt"
"io"
"math/rand"
"strconv"
"strings"
"time"
)
// Anonymisation is a function that transforms a string into another one
type Anonymisation func(string) (string, error)
// DateConfig stores the format (layout) of an input date
type DateConfig struct {
Format string
}
// RangeConfig stores configuration to define a range of values
type RangeConfig struct {
Gt *float64
Gte *float64
Lt *float64
Lte *float64
Output *string
}
// ActionConfig stores the config of an anonymisation action
type ActionConfig struct {
Name string
Salt *string
DateConfig DateConfig
RangeConfig []RangeConfig
CustomConfig []CustomConfig
}
// Returns an array of anonymisations according to the config
func anonymisations(configs *[]ActionConfig) ([]Anonymisation, error) {
var err error
res := make([]Anonymisation, len(*configs))
for i, config := range *configs {
if res[i], err = config.create(); err != nil {
return nil, err
}
}
return res, nil
}
// Returns the configured salt or a random one
// if it's not set.
func (ac *ActionConfig) saltOrRandom() string {
if ac.Salt != nil {
return *ac.Salt
}
return strconv.Itoa(rand.Int())
}
func (ac *ActionConfig) create() (Anonymisation, error) {
switch ac.Name {
case "nothing":
return identity, nil
case "outcode":
return outcode, nil
case "hash":
return hash(ac.saltOrRandom()), nil
case "year":
return year(ac.DateConfig.Format)
case "ranges":
return ranges(ac.RangeConfig)
case "custom":
return custom(ac.CustomConfig)
}
return nil, fmt.Errorf("can't create an action with name %s", ac.Name)
}
// The no-op, returns the input unchanged.
func identity(s string) (string, error) {
return s, nil
}
// Hashes (SHA1) the input.
func hash(salt string) Anonymisation {
return func(s string) (string, error) {
h := sha1.New()
io.WriteString(h, s)
io.WriteString(h, salt)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
}
// Takes a UK format postcode (eg. W1W 8BE) and just keeps
// the outcode (eg. W1W).
// i.e. returns the prefix of the input until it finds a space
func outcode(s string) (string, error) {
return strings.Split(s, " ")[0], nil
}
// Given a date format/layout, it returns a function that
// given a date in that format, just keeps the year.
// If either the format is invalid or the year doesn't
// match that format, it will return an error and
// the input unchanged
func year(format string) (Anonymisation, error) {
if _, err := time.Parse(format, format); err != nil {
return nil, err
}
return func(s string) (string, error) {
t, err := time.Parse(format, s)
if err != nil {
return s, err
}
return strconv.Itoa(t.Year()), nil
}, nil
}
// Given a list of ranges, it will summarise numeric
// values into groups of values, each group defined
// by a range and an output
func ranges(ranges []RangeConfig) (Anonymisation, error) {
for _, rc := range ranges {
if rc.Gt != nil && rc.Gte != nil || rc.Lt != nil && rc.Lte != nil {
return nil, errors.New("you can only specify one of (gt, gte) and (lt, lte)")
} else if rc.Gt == nil && rc.Gte == nil && rc.Lt == nil && rc.Lte == nil {
return nil, errors.New("you need to specify at least one of gt, gte, lt, lte")
} else if rc.Output == nil {
return nil, errors.New("you need to specify the output for a range")
}
}
return func(s string) (string, error) {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return s, err
}
for _, rang := range ranges {
if rang.contains(v) {
return *rang.Output, nil
}
}
return s, errors.New("No range defined for value")
}, nil
}
func (r *RangeConfig) contains(v float64) bool {
return (r.Gt == nil && r.Gte == nil || r.Gt != nil && *r.Gt < v || r.Gte != nil && *r.Gte <= v) &&
(r.Lt == nil && r.Lte == nil || r.Lt != nil && *r.Lt > v || r.Lte != nil && *r.Lte >= v)
}