PureBasic
PureBasic
Sign a JWS with a Private Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPrivateKey, which sets the private key used to create a signature. The example uses RS256 (RSASSA-PKCS1-v1_5 with SHA-256).
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. Public-key JWS signs with a private key so that anyone holding the corresponding public key can verify the signature.
Chilkat PureBasic Downloads
IncludeFile "CkJws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPrivateKey.pb"
Procedure ChilkatExample()
success.i = 0
jws.i = CkJws::ckCreate()
If jws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
header.i = CkJsonObject::ckCreate()
If header.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(header,"alg","RS256")
success = CkJws::ckSetProtectedHeader(jws,0,header)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
ProcedureReturn
EndIf
bIncludeBom.i = 0
success = CkJws::ckSetPayload(jws,"This is the content to sign.","utf-8",bIncludeBom)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
ProcedureReturn
EndIf
; The file path is relative to the application's current working directory. An absolute path
; may also be used. Supply the path appropriate to your own environment.
; Load the signer's private key and set it for signature 0.
privKey.i = CkPrivateKey::ckCreate()
If privKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadPemFile(privKey,"qa_data/signer_privkey.pem")
If success = 0
Debug CkPrivateKey::ckLastErrorText(privKey)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
success = CkJws::ckSetPrivateKey(jws,0,privKey)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
jwsCompact.s = CkJws::ckCreateJws(jws)
If CkJws::ckLastMethodSuccess(jws) = 0
Debug CkJws::ckLastErrorText(jws)
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
Debug jwsCompact
CkJws::ckDispose(jws)
CkJsonObject::ckDispose(header)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndProcedure