Create JWS
Code Samples
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
)
type Data struct {
Ver string `json:"ver"`
Timestamp string `json:"timestamp"`
TxnId string `json:"txnid"`
ConsentHandle string `json:"ConsentHandle"`
}
func main() {
data := Data{
Ver: "2.0.0",
Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05.999Z"),
TxnId: uuid.New().String(),
ConsentHandle: "465d1f35-f716-484f-a38e-30797d97b525",
}
// Convert struct to JSON string
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error: ", err)
return
}
// Print the JSON data
fmt.Println("Payload: ")
fmt.Println(string(jsonData))
const privateKeyJson = `{
"p": "4xjtcjnYMkkX97rViwxE9ZfhegRLVCQ7begAE_tBZe6-imKCSdQlw5Hasv5XPjY_SvEwkF0PD5oK-VIPRm40qjFSvSKjpz4myJabfbMwZQYhvsxOcPRqVRolCDW_OIQ7lXVjDEvl_4GmwRyp_sWaAUKaVIoyWLas2QvXZ8q4Yz0",
"kty": "RSA",
"q": "rtTjLo-H9nuOWEbl403YTf34acVOAyhNXDMgkim2v0TpMDWOCgEJOFuGH2ILEVRY1Rrcep7ups4wiqDXP7aN8icA96okOhSUlMFPvropk9uabPdPJvM9jh6VE2SP1VEAMf4BeDwq_dE_FyKWxGLZrRbN4joJheY75YYYyWELMDU",
"d": "FR73dPuWZgcvu57y8fHCaKQ142SCxvLxvC9mAsN4ztxWxCd7jjvqzJN7QUweEeqLHeQeTLcI7YcGL6cpUMGjA07XNSFjCKZyHtkeyCDNMvL91GVGluYxmIF66qu2f7EXqSsVElwlAHYUIs29b_H95Qpa53PiSMl97jfzQsG7W9X24KJM_NshPEmKvou0BWNmSFxNs66C-pq5i6Up224OY5a9bGXrRw5Vkv6QR7SwPhj0EFNkonMt1TK9vU4P5KLQ-82sEOqkTKtKYC86egj7gZKguqzhUkfw_kfghu-dGvnYeQ6h_7HQUvVsYRQJOutFrEuNvGay44SgDf1X0nZRcQ",
"e": "AQAB",
"use": "sig",
"kid": "57ea893e-7e1c-4383-b013-f6c96e3bf776",
"qi": "e3D3bnlVAPdqvg85eDh4Tp9VwM2VQjHSN_BLDl_8tm8Xbe68VzEpqBm_AEfA7OwlFuFWOGsQwCXO89kYJ6eF61jE_72zXRdrzseUM80c_nKIyx-ZlbijpTRp9VUnClMg4JcyZT4eWKkNurMFsf9vTWI4yayZqkJHc9J39B2MqLk",
"dp": "sVO3pWfxn2Jf6rrjIA5WkockM0jDIVKhbTKpTPD2ogeIlTpSjh-v1URAx1--8_8b8QLgbvmbVw1r4D7pWo-XZAXbm4A-1cq8MgkYJVEUSu48z2VtItpXZVhi7kQ5qoHmnM_qpaFmWNr-QPCG9T-K_8zoAXdf1NHfqiOK8arjy-k",
"alg": "RS256",
"dq": "TS_EFRxdRJ8MG0FB4XbO1rAX6mqnpJE4hko0SRONkShVY2lhil0muvD9aAGbLU114q_3Q1PDvXUuzSVxorlwNcaukKlXUSUg7APktyntjU_Y_9633VRqisJJwsQVFHKsiWXBNFosCN9G4Wnt8kpKHDcDMqu45JrfOthXPSFRFTk",
"n": "mxfLkK5DVngBOlVDdeFu_OQp3dIcfvHvoB1vU0DXTsTfZqpQa5ry9pI5N5lo5XxB_AUNw2bDPGCZBF6u6NKHsy50DXDfyh4VFz2SoxUQJELphfRwrHeugGsHuF3iyHxaXERyFjxjmzy9c3KKPszo_yzjVvZTfPesdyXRjZTXg-bqNIZbD8SrNDF8U0Nvh9kLlp0cfopbxuO4azts0rs3Z9WJZ-KnccFnEcgPvPkvLicsnlIyl3qUbZWFuoDKFgmItvGiOtwNBFtcwTRegrvbER-9bXByDB67KDryzzvdsLfRvs31snvdXSjF4BfXAXTuccQPLprKoHO0HkTq62b7oQ"
}`
privateKey, err := jwk.ParseKey([]byte(privateKeyJson))
if err != nil {
fmt.Printf("failed parse key: %sn", err)
return
}
headers := jws.NewHeaders()
headers.Set("b64", false)
headers.Set("crit", []string{"b64"})
serialized, err := jws.Sign(nil,
jws.WithKey(jwa.RS256,
privateKey,
jws.WithProtectedHeaders(headers),
),
jws.WithDetachedPayload([]byte(jsonData)),
)
if err != nil {
fmt.Printf("failed to sign payload: %sn", err)
return
}
fmt.Println("Detached JWS Signature:")
fmt.Println(string(serialized))
}
Java
public String signJws(String payload) throws JoseException {
payload = signature Creation using the payload;
Key = Public and Private Keypair Set
JsonWebKeySet rsaJsonWebKeySet = new JsonWebKeySet(key);
// Create a new JsonWebSignature object for the signing
JsonWebSignature signerJws = new JsonWebSignature();
// The content is the payload of the JWS
signerJws.setPayload(payload);
// Set the signature algorithm on the JWS
signerJws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
RsaJsonWebKey jwk = (RsaJsonWebKey)rsaJsonWebKeySet.getJsonWebKeys().get(0);
// The private key is used to sign
signerJws.setKey(jwk.getPrivateKey());
// Set the Key ID (kid) header because it's just the polite thing to do.
signerJws.setKeyIdHeaderValue(jwk.getKeyId());
// Set the "b64" header to false, which indicates that the payload is not
// encoded
// when calculating the signature (per RFC 7797)
signerJws
.getHeaders()
.setObjectHeaderValue(HeaderParameterNames.BASE64URL_ENCODE_PAYLOAD, false);
// Produce the compact serialization with an empty/detached payload,
// which is the encoded header + ".." + the encoded signature
return signerJws.getDetachedContentCompactSerialization();
}
Python
from joserfc.rfc7797 import serialize_compact
from joserfc.jwk import RSAKey
import json
# Private Key
private_key = RSAKey.import_key({
"p": "4xjtcjnYMkkX97rViwxE9ZfhegRLVCQ7begAE_tBZe6-imKCSdQlw5Hasv5XPjY_SvEwkF0PD5oK-VIPRm40qjFSvSKjpz4myJabfbMwZQYhvsxOcPRqVRolCDW_OIQ7lXVjDEvl_4GmwRyp_sWaAUKaVIoyWLas2QvXZ8q4Yz0",
"kty": "RSA",
"q": "rtTjLo-H9nuOWEbl403YTf34acVOAyhNXDMgkim2v0TpMDWOCgEJOFuGH2ILEVRY1Rrcep7ups4wiqDXP7aN8icA96okOhSUlMFPvropk9uabPdPJvM9jh6VE2SP1VEAMf4BeDwq_dE_FyKWxGLZrRbN4joJheY75YYYyWELMDU",
"d": "FR73dPuWZgcvu57y8fHCaKQ142SCxvLxvC9mAsN4ztxWxCd7jjvqzJN7QUweEeqLHeQeTLcI7YcGL6cpUMGjA07XNSFjCKZyHtkeyCDNMvL91GVGluYxmIF66qu2f7EXqSsVElwlAHYUIs29b_H95Qpa53PiSMl97jfzQsG7W9X24KJM_NshPEmKvou0BWNmSFxNs66C-pq5i6Up224OY5a9bGXrRw5Vkv6QR7SwPhj0EFNkonMt1TK9vU4P5KLQ-82sEOqkTKtKYC86egj7gZKguqzhUkfw_kfghu-dGvnYeQ6h_7HQUvVsYRQJOutFrEuNvGay44SgDf1X0nZRcQ",
"e": "AQAB",
"use": "sig",
"kid": "57ea893e-7e1c-4383-b013-f6c96e3bf776",
"qi": "e3D3bnlVAPdqvg85eDh4Tp9VwM2VQjHSN_BLDl_8tm8Xbe68VzEpqBm_AEfA7OwlFuFWOGsQwCXO89kYJ6eF61jE_72zXRdrzseUM80c_nKIyx-ZlbijpTRp9VUnClMg4JcyZT4eWKkNurMFsf9vTWI4yayZqkJHc9J39B2MqLk",
"dp": "sVO3pWfxn2Jf6rrjIA5WkockM0jDIVKhbTKpTPD2ogeIlTpSjh-v1URAx1--8_8b8QLgbvmbVw1r4D7pWo-XZAXbm4A-1cq8MgkYJVEUSu48z2VtItpXZVhi7kQ5qoHmnM_qpaFmWNr-QPCG9T-K_8zoAXdf1NHfqiOK8arjy-k",
"alg": "RS256",
"dq": "TS_EFRxdRJ8MG0FB4XbO1rAX6mqnpJE4hko0SRONkShVY2lhil0muvD9aAGbLU114q_3Q1PDvXUuzSVxorlwNcaukKlXUSUg7APktyntjU_Y_9633VRqisJJwsQVFHKsiWXBNFosCN9G4Wnt8kpKHDcDMqu45JrfOthXPSFRFTk",
"n": "mxfLkK5DVngBOlVDdeFu_OQp3dIcfvHvoB1vU0DXTsTfZqpQa5ry9pI5N5lo5XxB_AUNw2bDPGCZBF6u6NKHsy50DXDfyh4VFz2SoxUQJELphfRwrHeugGsHuF3iyHxaXERyFjxjmzy9c3KKPszo_yzjVvZTfPesdyXRjZTXg-bqNIZbD8SrNDF8U0Nvh9kLlp0cfopbxuO4azts0rs3Z9WJZ-KnccFnEcgPvPkvLicsnlIyl3qUbZWFuoDKFgmItvGiOtwNBFtcwTRegrvbER-9bXByDB67KDryzzvdsLfRvs31snvdXSjF4BfXAXTuccQPLprKoHO0HkTq62b7oQ"
})
# Headers
protected = {"alg": private_key.alg, "kid": private_key.kid, "b64": False, "crit": ["b64"]}
# Request Payload
payload = json.dumps({
"ver":"1.1.2",
"timestamp":"2024-04-25T12:51:22.638Z",
"txnid":"f5e123a9-202c-4c7e-8bfa-9db9eea89ff2"
}, separators=(',', ':'))
print("Payload: " + payload)
# Detached JWS Creation
value = serialize_compact(protected, payload, private_key)
print("Detached JWS: " + value)
Last updated