React Native
React Native
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 React Native Downloads
import { Jws, PublicKey } from '@chilkat/react-native'
function chilkatExample() {
const jws = new Jws();
// Load the JWS compact serialization.
const jwsCompact = 'eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>';
try {
jws.loadJws(jwsCompact);
} catch {
console.log(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.
const pubKey = new PublicKey();
try {
pubKey.loadFromFile('qa_data/signer_pubkey.pem');
} catch {
console.log(pubKey.lastErrorText);
return;
}
try {
jws.setPublicKey(0, pubKey);
} catch {
console.log(jws.lastErrorText);
return;
}
// Validate the signature. Validate returns 1 when valid, 0 when not, or a negative value on error.
const v = jws.validate(0);
if (v < 0) {
console.log(jws.lastErrorText);
return;
}
if (v !== 1) {
console.log('The signature is not valid.');
return;
}
console.log('The signature is valid.');
}