/
capi_hash.go
92 lines (75 loc) · 2.18 KB
/
capi_hash.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
package businesssdk
import (
"crypto/sha256"
"fmt"
"regexp"
"strings"
"github.com/umahmood/soundex"
)
/*
How to Normalize Identifiers:
- Normalize email addresses by trimming leading and trailing whitespace and converting
all characters to lowercase before hashing
- Normalize phone numbers by including the country code, remove any double 0 in front
of the country code. if the number itself begins with a 0, this should be removed. Also
exclude any non-numeric characters such as whitespace, parentheses, '+', or '-'
*/
const pattern1 = `^((\+|00)(\d+)[\-\s])?0?(.+)`
const pattern2 = `[^\d.]+`
var p1regex = regexp.MustCompile(pattern1)
var p2regex = regexp.MustCompile(pattern2)
func Sha256(in string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(in)))
}
func Soundex(in string) string {
return soundex.Code(in)
}
// NormAndHashStr Returns a tuple with hashed and normalized string if it is valid.
// Nil otherwise and boolean to check if value is set
func NormAndHashStr(in string) (*string, bool) {
if in == "" || strings.TrimSpace(in) == "" {
return nil, false
}
strHash := Sha256(strings.ToLower(strings.TrimSpace(in)))
return &strHash, true
}
func NormAndSoundexStr(in string) (*string, bool) {
if in == "" || strings.TrimSpace(in) == "" {
return nil, false
}
strSdx := Soundex(strings.ToLower(strings.TrimSpace(in)))
return &strSdx, true
}
// NormAndHashPhoneNum Normalizes and hashes phone number. Returns a tuple with hashed
// normalized phone number if set. Nil otherwise and boolean to check if value is set
func NormAndHashPhoneNum(in string) (*string, bool) {
num := normalizePhoneNum(in)
if num == "" {
return nil, false
}
numHash := Sha256(num)
return &numHash, true
}
func normalizePhoneNum(in string) string {
if in == "" {
return in
}
results := p1regex.FindStringSubmatch(in)
if len(results) > 0 {
countryCode := results[3]
numstr := results[4]
if countryCode != "" {
countryCode = p2regex.ReplaceAllString(countryCode, "")
}
if numstr != "" {
numstr = p2regex.ReplaceAllString(numstr, "")
}
if countryCode == "" {
countryCode = "1"
}
if numstr != "" {
return countryCode + numstr
}
}
return ""
}