mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 09:14:01 +03:00
Use array of fixed length for secret
This commit is contained in:
+13
-6
@@ -10,13 +10,15 @@ import (
|
|||||||
|
|
||||||
const SecretKeyLength = 16
|
const SecretKeyLength = 16
|
||||||
|
|
||||||
|
var secretEmptyKey [SecretKeyLength]byte
|
||||||
|
|
||||||
type Secret struct {
|
type Secret struct {
|
||||||
Key []byte
|
Key [SecretKeyLength]byte
|
||||||
Host string
|
Host string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Secret) MarshalText() ([]byte, error) {
|
func (s Secret) MarshalText() ([]byte, error) {
|
||||||
if s.Key == nil {
|
if s.Key == secretEmptyKey {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ func (s *Secret) UnmarshalText(data []byte) error {
|
|||||||
return fmt.Errorf("secret has incorrect length %d", len(text))
|
return fmt.Errorf("secret has incorrect length %d", len(text))
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Key = decoded[:SecretKeyLength]
|
copy(s.Key[:], decoded[:SecretKeyLength])
|
||||||
s.Host = string(decoded[SecretKeyLength:])
|
s.Host = string(decoded[SecretKeyLength:])
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -70,7 +72,7 @@ func (s Secret) Hex() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Secret) makeBytes() []byte {
|
func (s *Secret) makeBytes() []byte {
|
||||||
data := append([]byte{238}, s.Key...) // hex 'ee' = 238
|
data := append([]byte{238}, s.Key[:]...) // hex 'ee' = 238
|
||||||
data = append(data, s.Host...)
|
data = append(data, s.Host...)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
@@ -78,13 +80,18 @@ func (s *Secret) makeBytes() []byte {
|
|||||||
|
|
||||||
func GenerateSecret(hostname string) Secret {
|
func GenerateSecret(hostname string) Secret {
|
||||||
s := Secret{
|
s := Secret{
|
||||||
Key: make([]byte, SecretKeyLength),
|
|
||||||
Host: hostname,
|
Host: hostname,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := rand.Read(s.Key); err != nil {
|
if _, err := rand.Read(s.Key[:]); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseSecret(secret string) (Secret, error) {
|
||||||
|
s := Secret{}
|
||||||
|
|
||||||
|
return s, s.UnmarshalText([]byte(secret))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user