Sample code for 30+ languages & platforms
PureBasic

Load JWK into PublicKey Object

See more JSON Web Signatures (JWS) Examples

Demonstrates how to load a Java Web Key (JWK) into a Chilkat public key object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPublicKey.pb"

Procedure ChilkatExample()

    success.i = 0

    ; Imagine we have this JWK:

    ; {
    ; "kty":"RSA",
    ; "kid":"bilbo.baggins@hobbiton.example",
    ; "use":"sig",
    ; "n":"26381066103266289976070039679240841999340706859156341823971393693544846472963839705790075117105560154606562987481050889935247188300178764426769543782451174831475924241862817098997865881169498995546309260357488285676768114714548754913374731868063116787903918383610614912211576698081222844057810710088656983077876282582286966085572172471849443305250452393766960762340311496052418952315081937000716222751229644449475751932222180738210988190709824209639002026650048655399108647971010890335494074025258268035713167380057822555485524738518699437692145859633592396626258745090144803752145540471688039673495588314804430463791",
    ; "e":"65537"
    ; }
    ; 

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

    success = CkStringBuilder::ckLoadFile(sbJwk,"qa_data/jwk/simple_jwk_rsa.json","utf-8")
    If success <> 1
        Debug "Failed to load simple JWK file."
        CkStringBuilder::ckDispose(sbJwk)
        ProcedureReturn
    EndIf

    ; Loading it into a Chilkat public key object is easy:
    pubkey.i = CkPublicKey::ckCreate()
    If pubkey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPublicKey::ckLoadFromString(pubkey,CkStringBuilder::ckGetAsString(sbJwk))
    If success = 0
        Debug CkPublicKey::ckLastErrorText(pubkey)
        CkStringBuilder::ckDispose(sbJwk)
        CkPublicKey::ckDispose(pubkey)
        ProcedureReturn
    EndIf

    Debug "Simple JWK loaded into public key object."

    ; ----------------------------------------------------------------------------------------------------------
    ; Demonstrate how to load a JWK that is contained within JSON into a public key object.

    ; {
    ;   "alg":"RS256",
    ;   "jwk":  {
    ;           "kty":"RSA",
    ;           "kid":"bilbo.baggins@hobbiton.example",
    ;           "use":"sig",
    ;           "n":"26381066103266289976070039679240841999340706859156341823971393693544846472963839705790075117105560154606562987481050889935247188300178764426769543782451174831475924241862817098997865881169498995546309260357488285676768114714548754913374731868063116787903918383610614912211576698081222844057810710088656983077876282582286966085572172471849443305250452393766960762340311496052418952315081937000716222751229644449475751932222180738210988190709824209639002026650048655399108647971010890335494074025258268035713167380057822555485524738518699437692145859633592396626258745090144803752145540471688039673495588314804430463791",
    ;           "e":"65537"
    ;           }
    ; }

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

    success = CkStringBuilder::ckLoadFile(sbJwk2,"qa_data/jwk/contained_jwk_rsa.json","utf-8")
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sbJwk2)
        CkStringBuilder::ckDispose(sbJwk)
        CkPublicKey::ckDispose(pubkey)
        CkStringBuilder::ckDispose(sbJwk2)
        ProcedureReturn
    EndIf

    ; Get the "jwk" part of the JSON..
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,CkStringBuilder::ckGetAsString(sbJwk2))

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

    success = CkJsonObject::ckObjectOf2(json,"jwk",jsonJwk)
    If success = 0
        Debug CkJsonObject::ckLastErrorText(json)
        CkStringBuilder::ckDispose(sbJwk)
        CkPublicKey::ckDispose(pubkey)
        CkStringBuilder::ckDispose(sbJwk2)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonJwk)
        ProcedureReturn
    EndIf

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

    success = CkPublicKey::ckLoadFromString(pubkey2,CkJsonObject::ckEmit(jsonJwk))
    If success = 0
        Debug CkPublicKey::ckLastErrorText(pubkey2)
        CkStringBuilder::ckDispose(sbJwk)
        CkPublicKey::ckDispose(pubkey)
        CkStringBuilder::ckDispose(sbJwk2)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonJwk)
        CkPublicKey::ckDispose(pubkey2)
        ProcedureReturn
    EndIf

    Debug "Contained JWK loaded into public key object."


    CkStringBuilder::ckDispose(sbJwk)
    CkPublicKey::ckDispose(pubkey)
    CkStringBuilder::ckDispose(sbJwk2)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonJwk)
    CkPublicKey::ckDispose(pubkey2)


    ProcedureReturn
EndProcedure