go-http-peering/crypto/rsa.go

36 lines
793 B
Go
Raw Normal View History

2019-02-22 17:35:49 +01:00
package crypto
import (
"crypto/rand"
"crypto/rsa"
"time"
peering "forge.cadoles.com/Cadoles/go-http-peering"
2019-02-22 17:35:49 +01:00
jwt "github.com/dgrijalva/jwt-go"
2022-09-12 17:46:59 +02:00
"github.com/pkg/errors"
2019-02-22 17:35:49 +01:00
)
func CreateRSAKey(bits int) (*rsa.PrivateKey, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
2022-09-12 17:46:59 +02:00
return nil, errors.WithStack(err)
2019-02-22 17:35:49 +01:00
}
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 {
2022-09-12 17:46:59 +02:00
return "", errors.WithStack(err)
2019-02-22 17:35:49 +01:00
}
return tokenStr, nil
}