Skip to content

Commit

Permalink
Adding function to generate final signature value
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-ionut-balan committed Apr 28, 2023
1 parent 32e18e5 commit d9df615
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Sign(payload string, privateKey *rsa.PrivateKey) ([]byte, error) {

signedPayload, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashedPayload)
if err != nil {
logging.Error("Cannot sign message. Signign with private key failed!", err)
logging.Error("Cannot sign message. Signign with private key failed!", nil)
return nil, err
}

Expand Down
18 changes: 17 additions & 1 deletion jws/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/alexandru-ionut-balan/ing-jws/util"
)

func generateHeader(jwsHeader *JwsHeader) (string, error) {
func generateEncodedHeader(jwsHeader *JwsHeader) (string, error) {
rawHeaderBytes, err := json.Marshal(jwsHeader)
if err != nil {
logging.Error("Cannot marshal JWS Header into JSON.", nil)
Expand All @@ -34,3 +34,19 @@ func generateSignatureValue(encodedJwsHeader string, httpHeaders []util.HttpHead

return crypto.Base64(signedInput), nil
}

func GenerateSignature(jwsHeader *JwsHeader, httpHeaders []util.HttpHeader, privateKey *rsa.PrivateKey) (string, error) {
encodedHeader, err := generateEncodedHeader(jwsHeader)
if err != nil {
logging.Error("Cannot create signature!", nil)
return "", err
}

signatureValue, err := generateSignatureValue(encodedHeader, httpHeaders, privateKey)
if err != nil {
logging.Error("Cannot create signature!", nil)
return "", err
}

return encodedHeader + ".." + signatureValue, nil
}

0 comments on commit d9df615

Please sign in to comment.