go-http-peering/crypto/rsa.go

35 lines
732 B
Go
Raw Permalink 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"
)
func CreateRSAKey(bits int) (*rsa.PrivateKey, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, 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 "", err
}
return tokenStr, nil
}