Skip to content

Commit

Permalink
Issue #10 - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-kovalev committed Jun 16, 2022
1 parent 9d49915 commit 2ccf44d
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions rsa2jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"path/filepath"
"strings"

jwt "github.com/golang-jwt/jwt/v4"
jwk "github.com/lestrrat-go/jwx/v2/jwk"
)

Expand Down Expand Up @@ -58,39 +57,51 @@ func main() {
jwkPubSet := map[string][]jwkPubKey{"keys": {}}
fmt.Printf("%43s\t%s\n", "Kid", "Filename")
for _, f := range filePaths {
privKey, err := parseRSAPrivateKeyFromPEM(f)
jwkSet, err := jwk.ReadFile(f, jwk.WithPEM(true))
if err != nil {
log.Fatal(err)
}
pubKey := privKey.Public()

privJwk, err := jwk.FromRaw(privKey)
if err != nil {
log.Fatal(err)
for i := 0; i < jwkSet.Len(); i++ {
var rawKey interface{}
jwkKey, _ := jwkSet.Key(i)
err = jwkKey.Raw(&rawKey)
if err != nil {
log.Fatal(err)
}
privKey, ok := rawKey.(*rsa.PrivateKey)
if !ok {
log.Fatal("provided key is not private one")
}
pubKey := privKey.Public()

privJwk, err := jwk.FromRaw(privKey)
if err != nil {
log.Fatal(err)
}
// generates Kid using Key.Thumbprint method with crypto.SHA256
jwk.AssignKeyID(privJwk) //nolint:errcheck

jwkPub := jwkPubKey{
Kty: jwkKtyRsa,
Alg: jwkAlgRs256,
Use: jwkUseSig,
Kid: privJwk.KeyID(),
N: safeEncode(pubKey.(*rsa.PublicKey).N.Bytes()),
E: safeEncode(big.NewInt(int64(pubKey.(*rsa.PublicKey).E)).Bytes()),
}
jwkPriv := jwkPrivAndPubKeyPair{
jwkPubKey: jwkPub,
P: safeEncode(privKey.Primes[0].Bytes()),
Q: safeEncode(privKey.Primes[1].Bytes()),
D: safeEncode(privKey.D.Bytes()),
Qi: safeEncode(privKey.Precomputed.Qinv.Bytes()),
Dp: safeEncode(privKey.Precomputed.Dp.Bytes()),
Dq: safeEncode(privKey.Precomputed.Dq.Bytes()),
}
jwkPrivSet["keys"] = append(jwkPrivSet["keys"], jwkPriv)
jwkPubSet["keys"] = append(jwkPubSet["keys"], jwkPub)
fmt.Printf("%s\t%s\n", jwkPub.Kid, f)
}
// generates Kid using Key.Thumbprint method with crypto.SHA256
jwk.AssignKeyID(privJwk) //nolint:errcheck

jwkPub := jwkPubKey{
Kty: jwkKtyRsa,
Alg: jwkAlgRs256,
Use: jwkUseSig,
Kid: privJwk.KeyID(),
N: safeEncode(pubKey.(*rsa.PublicKey).N.Bytes()),
E: safeEncode(big.NewInt(int64(pubKey.(*rsa.PublicKey).E)).Bytes()),
}
jwkPriv := jwkPrivAndPubKeyPair{
jwkPubKey: jwkPub,
P: safeEncode(privKey.Primes[0].Bytes()),
Q: safeEncode(privKey.Primes[1].Bytes()),
D: safeEncode(privKey.D.Bytes()),
Qi: safeEncode(privKey.Precomputed.Qinv.Bytes()),
Dp: safeEncode(privKey.Precomputed.Dp.Bytes()),
Dq: safeEncode(privKey.Precomputed.Dq.Bytes()),
}
jwkPrivSet["keys"] = append(jwkPrivSet["keys"], jwkPriv)
jwkPubSet["keys"] = append(jwkPubSet["keys"], jwkPub)
fmt.Printf("%s\t%s\n", jwkPub.Kid, f)
}

if err := marshalAndSave(jwkPrivSet, filepath.Join(dir, jsonJwkPrivFilename)); err != nil {
Expand All @@ -109,16 +120,6 @@ func marshalAndSave(v interface{}, path string) error {
return ioutil.WriteFile(path, bytes, 0644)
}

// parsing a PEM encoded PKCS1 or PKCS8 private key
func parseRSAPrivateKeyFromPEM(path string) (*rsa.PrivateKey, error) {
key, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

return jwt.ParseRSAPrivateKeyFromPEM(key)
}

func lookupPemFiles(dirName string) ([]string, error) {
fileInfos, err := ioutil.ReadDir(dirName)
if err != nil {
Expand Down

0 comments on commit 2ccf44d

Please sign in to comment.