{"library":"jwcrypto","code":"from jwcrypto import jwk, jws\nfrom jwcrypto.common import json_encode\n\n# 1. Generate a symmetric key\nkey = jwk.JWK.generate(kty='oct', size=256)\n\n# 2. Define the payload and JWS headers\npayload = \"My Integrity protected message\"\njwstoken = jws.JWS(payload.encode('utf-8'))\n\n# 3. Add signature to the token\njwstoken.add_signature(\n    key,\n    None,\n    json_encode({\"alg\": \"HS256\"}),\n    json_encode({\"kid\": key.thumbprint()})\n)\n\n# 4. Serialize the JWS token\nsigned_token = jwstoken.serialize()\nprint(f\"Signed JWS: {signed_token}\")\n\n# 5. Verify the JWS token\nverifier_token = jws.JWS()\nverifier_token.deserialize(signed_token)\nverifier_token.verify(key)\n\n# 6. Access the verified payload\nverified_payload = verifier_token.payload.decode('utf-8')\nprint(f\"Verified Payload: {verified_payload}\")","lang":"python","description":"This quickstart demonstrates how to generate a symmetric key, sign a payload using JWS, and then verify the signed token. It utilizes `jwk` for key management and `jws` for signature operations, along with `jwcrypto.common` for JSON encoding.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}