|
|
@@ -10,13 +10,15 @@ import (
|
|
10
|
10
|
|
|
11
|
11
|
const SecretKeyLength = 16
|
|
12
|
12
|
|
|
|
13
|
+var secretEmptyKey [SecretKeyLength]byte
|
|
|
14
|
+
|
|
13
|
15
|
type Secret struct {
|
|
14
|
|
- Key []byte
|
|
|
16
|
+ Key [SecretKeyLength]byte
|
|
15
|
17
|
Host string
|
|
16
|
18
|
}
|
|
17
|
19
|
|
|
18
|
20
|
func (s Secret) MarshalText() ([]byte, error) {
|
|
19
|
|
- if s.Key == nil {
|
|
|
21
|
+ if s.Key == secretEmptyKey {
|
|
20
|
22
|
return nil, nil
|
|
21
|
23
|
}
|
|
22
|
24
|
|
|
|
@@ -51,7 +53,7 @@ func (s *Secret) UnmarshalText(data []byte) error {
|
|
51
|
53
|
return fmt.Errorf("secret has incorrect length %d", len(text))
|
|
52
|
54
|
}
|
|
53
|
55
|
|
|
54
|
|
- s.Key = decoded[:SecretKeyLength]
|
|
|
56
|
+ copy(s.Key[:], decoded[:SecretKeyLength])
|
|
55
|
57
|
s.Host = string(decoded[SecretKeyLength:])
|
|
56
|
58
|
|
|
57
|
59
|
return nil
|
|
|
@@ -70,7 +72,7 @@ func (s Secret) Hex() string {
|
|
70
|
72
|
}
|
|
71
|
73
|
|
|
72
|
74
|
func (s *Secret) makeBytes() []byte {
|
|
73
|
|
- data := append([]byte{238}, s.Key...) // hex 'ee' = 238
|
|
|
75
|
+ data := append([]byte{238}, s.Key[:]...) // hex 'ee' = 238
|
|
74
|
76
|
data = append(data, s.Host...)
|
|
75
|
77
|
|
|
76
|
78
|
return data
|
|
|
@@ -78,13 +80,18 @@ func (s *Secret) makeBytes() []byte {
|
|
78
|
80
|
|
|
79
|
81
|
func GenerateSecret(hostname string) Secret {
|
|
80
|
82
|
s := Secret{
|
|
81
|
|
- Key: make([]byte, SecretKeyLength),
|
|
82
|
83
|
Host: hostname,
|
|
83
|
84
|
}
|
|
84
|
85
|
|
|
85
|
|
- if _, err := rand.Read(s.Key); err != nil {
|
|
|
86
|
+ if _, err := rand.Read(s.Key[:]); err != nil {
|
|
86
|
87
|
panic(err)
|
|
87
|
88
|
}
|
|
88
|
89
|
|
|
89
|
90
|
return s
|
|
90
|
91
|
}
|
|
|
92
|
+
|
|
|
93
|
+func ParseSecret(secret string) (Secret, error) {
|
|
|
94
|
+ s := Secret{}
|
|
|
95
|
+
|
|
|
96
|
+ return s, s.UnmarshalText([]byte(secret))
|
|
|
97
|
+}
|