go-http-peering/crypto/pem.go

26 lines
462 B
Go
Raw Normal View History

2019-02-03 20:56:58 +01:00
package crypto
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
jwt "github.com/dgrijalva/jwt-go"
)
func EncodePublicKeyToPEM(key interface{}) ([]byte, error) {
pub, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return nil, err
}
data := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pub,
})
return data, nil
}
func DecodePEMToPublicKey(pem []byte) (*rsa.PublicKey, error) {
return jwt.ParseRSAPublicKeyFromPEM(pem)
}