any-sync/util/keys/decode.go
2023-01-05 15:34:09 +03:00

31 lines
620 B
Go

package keys
import (
"encoding/base64"
)
func EncodeKeyToString[T 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 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)
}