any-sync/util/keys/decode.go
2023-03-24 11:45:39 +01:00

32 lines
679 B
Go

package keys
import (
"encoding/base64"
"github.com/anytypeio/any-sync/util/crypto"
)
func EncodeKeyToString[T crypto.Key](key T) (str string, err error) {
raw, err := key.Raw()
if err != nil {
return
}
str = EncodeBytesToString(raw)
return
}
func EncodeBytesToString(bytes []byte) string {
return base64.StdEncoding.EncodeToString(bytes)
}
func DecodeKeyFromString[T crypto.Key](str string, construct func([]byte) (T, error), def T) (T, error) {
dec, err := DecodeBytesFromString(str)
if err != nil {
return def, err
}
return construct(dec)
}
func DecodeBytesFromString(str string) (bytes []byte, err error) {
return base64.StdEncoding.DecodeString(str)
}