26 lines
462 B
Go
26 lines
462 B
Go
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)
|
|
}
|