forked from felipemsantana/trumail
/
address.go
37 lines (31 loc) · 976 Bytes
/
address.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
package verifier
import (
"crypto/md5"
"encoding/hex"
"net/mail"
"strings"
)
// Address stores all information about an email Address
type Address struct{ Username, Domain, Address string }
// ParseAddress attempts to parse an email address and return it in the form
// of an Address struct pointer
func ParseAddress(email string) (*Address, error) {
// Parses the address with the internal go mail address parser
a, err := mail.ParseAddress(strings.ToLower(email))
if err != nil {
return nil, err
}
// Find the last occurrence of an @ sign
index := strings.LastIndex(a.Address, "@")
// Returns the address with the username and domain split out
return &Address{
Username: a.Address[:index],
Domain: a.Address[index+1:],
Address: a.Address,
}, nil
}
// MD5 takes a calculation of the md5 Hash and returns a string representation
func (a *Address) MD5() string {
hashBytes := md5.Sum([]byte(a.Address))
return hex.EncodeToString(hashBytes[:])
}