Android™
Android™
Validate a JWS with a Public Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded JWS.
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. The public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkJws jws = new CkJws();
// Load the JWS compact serialization.
String jwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
success = jws.LoadJws(jwsCompact);
if (success == false) {
Log.i(TAG, jws.lastErrorText());
return;
}
// 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 public key and set it for validating signature 0.
CkPublicKey pubKey = new CkPublicKey();
success = pubKey.LoadFromFile("qa_data/signer_pubkey.pem");
if (success == false) {
Log.i(TAG, pubKey.lastErrorText());
return;
}
success = jws.SetPublicKey(0,pubKey);
if (success == false) {
Log.i(TAG, jws.lastErrorText());
return;
}
// Validate the signature. Validate returns 1 when valid, 0 when not, or a negative value on error.
int v = jws.Validate(0);
if (v < 0) {
Log.i(TAG, jws.lastErrorText());
return;
}
if (v != 1) {
Log.i(TAG, "The signature is not valid.");
return;
}
Log.i(TAG, "The signature is valid.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}