go-http-peering/crypto/rsa.go

36 lines
793 B
Go

package crypto
import (
"crypto/rand"
"crypto/rsa"
"time"
peering "forge.cadoles.com/Cadoles/go-http-peering"
jwt "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
)
func CreateRSAKey(bits int) (*rsa.PrivateKey, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, errors.WithStack(err)
}
return key, nil
}
func CreateServerToken(privateKey *rsa.PrivateKey, issuer string, peerID peering.PeerID) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, peering.ServerTokenClaims{
StandardClaims: jwt.StandardClaims{
NotBefore: time.Now().Unix(),
Issuer: issuer,
},
PeerID: peerID,
})
tokenStr, err := token.SignedString(privateKey)
if err != nil {
return "", errors.WithStack(err)
}
return tokenStr, nil
}