versia-go/pkg/versia/crypto/spki_public_key.go

57 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-08-22 23:03:38 +02:00
package versiacrypto
2024-08-11 03:51:22 +02:00
import (
2024-08-20 22:43:26 +02:00
"crypto"
2024-08-11 03:51:22 +02:00
"crypto/x509"
"encoding/base64"
"encoding/json"
)
// SPKIPublicKey is a type that represents a [ed25519.PublicKey] in the SPKI
// format.
2024-08-20 22:43:26 +02:00
type SPKIPublicKey struct {
Key any
Algorithm string
}
2024-08-11 03:51:22 +02:00
2024-08-22 23:03:38 +02:00
func UnmarshalSPKIPubKey(algorithm string, raw []byte) (*SPKIPublicKey, error) {
2024-08-11 03:51:22 +02:00
rawStr := ""
if err := json.Unmarshal(raw, &rawStr); err != nil {
2024-08-20 22:43:26 +02:00
return nil, err
2024-08-11 03:51:22 +02:00
}
raw, err := base64.StdEncoding.DecodeString(rawStr)
if err != nil {
2024-08-20 22:43:26 +02:00
return nil, err
2024-08-11 03:51:22 +02:00
}
2024-08-20 22:43:26 +02:00
return NewSPKIPubKey(algorithm, raw)
}
// NewSPKIPubKey decodes the public key from a base64 encoded string and then unmarshals it from the SPKI form.
func NewSPKIPubKey(algorithm string, raw []byte) (*SPKIPublicKey, error) {
2024-08-11 03:51:22 +02:00
parsed, err := x509.ParsePKIXPublicKey(raw)
if err != nil {
2024-08-20 22:43:26 +02:00
return nil, err
2024-08-11 03:51:22 +02:00
}
2024-08-20 22:43:26 +02:00
return &SPKIPublicKey{
Key: parsed,
Algorithm: algorithm,
}, nil
2024-08-11 03:51:22 +02:00
}
// MarshalJSON marshals the SPKI-encoded public key to a base64 encoded string.
func (k SPKIPublicKey) MarshalJSON() ([]byte, error) {
2024-08-20 22:43:26 +02:00
raw, err := x509.MarshalPKIXPublicKey(k.Key)
2024-08-11 03:51:22 +02:00
if err != nil {
return nil, err
}
return json.Marshal(base64.StdEncoding.EncodeToString(raw))
}
2024-08-20 22:43:26 +02:00
func (k SPKIPublicKey) ToKey() crypto.PublicKey {
return k.Key
2024-08-11 03:51:22 +02:00
}