wireguard: implement MarshalJSON for PrivateKey and PublicKey
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519"
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
@@ -50,6 +51,25 @@ func (pub PublicKey) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalJSON encodes the key for JSON, omiting empty.
|
||||||
|
func (key PrivateKey) MarshalJSON() ([]byte, error) {
|
||||||
|
return encodeKeyJSON(key.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON encodes the key for JSON, omiting empty.
|
||||||
|
func (pub PublicKey) MarshalJSON() ([]byte, error) {
|
||||||
|
return encodeKeyJSON(pub.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeKeyJSON(s string) ([]byte, error) {
|
||||||
|
var out []byte
|
||||||
|
if s != "" {
|
||||||
|
out = []byte(fmt.Sprintf("%q", s))
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsZero tells if the key hasn't been set
|
// IsZero tells if the key hasn't been set
|
||||||
func (key PrivateKey) IsZero() bool {
|
func (key PrivateKey) IsZero() bool {
|
||||||
var zero PrivateKey
|
var zero PrivateKey
|
||||||
|
|||||||
Reference in New Issue
Block a user