/
httpkey.go
62 lines (51 loc) · 1.49 KB
/
httpkey.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
package lib
import (
"io/ioutil"
"net"
"net/http"
)
// GenerateHttpKey reaches out to url, hashes the body and returns
// the sha512 hash
func GenerateHttpKey(url string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "GoGreen User Agent")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
httpKey, _ := GenerateHash(result, "sha512")
return httpKey, nil
}
// GenerateDNSAKey requests DNS A record, hashes the response and
// returns the DNS response and the sha512 hash
// Note: Only works with the first response and ignores multiple responses
func GenerateDNSAKey(hostname string) (string, string, error) {
resp, err := net.LookupHost(hostname)
if err != nil {
return "", "", err
}
result := resp[0]
hash, _ := GenerateHash([]byte(result), "sha512")
return result, hash, nil
}
// GenerateDNSTXTKey requests DNS A record, hashes the response and
// returns the DNS response and the sha512 hash
// Note: Only works with the first response and ignores multiple responses
func GenerateDNSTXTKey(hostname string) (string, string, error) {
resp, err := net.LookupTXT(hostname)
if err != nil {
return "", "", err
}
result := resp[0]
hash, _ := GenerateHash([]byte(result), "sha512")
return result, hash, nil
}