Sample code for 30+ languages & platforms
PureBasic

Create JWS using the Flattened JSON Serialization Format

See more JSON Web Signatures (JWS) Examples

Creates a JSON Web Signature (JWS) containing 1 signature and output using the flattened JSON serialization format.

Note: This example requires Chilkat v9.5.0.66 or greater.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJws.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    ; Note: This example requires Chilkat v9.5.0.66 or greater.

    ; The JWS to be created will contain one signature using an ECDSA key.

    sbEccJwk.i = CkStringBuilder::ckCreate()
    If sbEccJwk.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbEccJwk,"{" + Chr(34) + "kty" + Chr(34) + ":" + Chr(34) + "EC" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "crv" + Chr(34) + ":" + Chr(34) + "P-256" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "x" + Chr(34) + ":" + Chr(34) + "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "y" + Chr(34) + ":" + Chr(34) + "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "d" + Chr(34) + ":" + Chr(34) + "jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI" + Chr(34))
    CkStringBuilder::ckAppend(sbEccJwk,"}")

    eccKey.i = CkPrivateKey::ckCreate()
    If eccKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Note: This example loads the ECDSA key from JWK format.  Any format can be loaded
    ; into the private key object. (See the online reference documentation..)
    success = CkPrivateKey::ckLoadJwk(eccKey,CkStringBuilder::ckGetAsString(sbEccJwk))
    If success <> 1
        Debug CkPrivateKey::ckLastErrorText(eccKey)
        CkStringBuilder::ckDispose(sbEccJwk)
        CkPrivateKey::ckDispose(eccKey)
        ProcedureReturn
    EndIf

    ; ---------------------------------------------------
    ; Prepare both a protected and unprotected header.

    jwsProtHdr.i = CkJsonObject::ckCreate()
    If jwsProtHdr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendString(jwsProtHdr,"alg","ES256")

    jwsUnprotHdr.i = CkJsonObject::ckCreate()
    If jwsUnprotHdr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendString(jwsUnprotHdr,"kid","myEcKey")

    ; ---------------------------------------------------
    jws.i = CkJws::ckCreate()
    If jws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJws::ckSetProtectedHeader(jws,0,jwsProtHdr)
    CkJws::ckSetUnprotectedHeader(jws,0,jwsUnprotHdr)

    CkJws::ckSetPrivateKey(jws,0,eccKey)

    bIncludeBom.i = 0
    payloadStr.s = "In our village, folks say God crumbles up the old moon into stars."
    CkJws::ckSetPayload(jws,payloadStr,"utf-8",bIncludeBom)

    ; ---------------------------------------------------
    ; Create the JWS.
    ; Indicate we prefer the non-compact, flattened serialization.
    CkJws::setCkPreferCompact(jws, 0)
    CkJws::setCkPreferFlattened(jws, 1)

    jwsStr.s = CkJws::ckCreateJws(jws)
    If CkJws::ckLastMethodSuccess(jws) <> 1
        Debug CkJws::ckLastErrorText(jws)
        CkStringBuilder::ckDispose(sbEccJwk)
        CkPrivateKey::ckDispose(eccKey)
        CkJsonObject::ckDispose(jwsProtHdr)
        CkJsonObject::ckDispose(jwsUnprotHdr)
        CkJws::ckDispose(jws)
        ProcedureReturn
    EndIf

    ; The jwsStr is contains the flattened JSON in the smallest possible size, which is a single line.
    ; To get in human-readable format, load into a Chilkat JSON object and emit..
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,jwsStr)
    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    ; Sample output:

    ; { 
    ;   "payload": "SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu",
    ;   "protected": "eyJhbGciOiJFUzI1NiJ9",
    ;   "header": { 
    ;     "kid": "myEcKey"
    ;   },
    ;   "signature": "b7V2UpDPytr-kMnM_YjiQ3E0J2ucOI9LYA7mt57vccrK1rb84j9areqgQcJwOA00aWGoz4hf6sMTBfobdcJEGg"
    ; }


    CkStringBuilder::ckDispose(sbEccJwk)
    CkPrivateKey::ckDispose(eccKey)
    CkJsonObject::ckDispose(jwsProtHdr)
    CkJsonObject::ckDispose(jwsUnprotHdr)
    CkJws::ckDispose(jws)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure