React Native
React Native
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 React Native Downloads
import { JsonObject, Jws, PrivateKey } from '@chilkat/react-native'
function chilkatExample() {
const jws = new Jws();
// Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
const header = new JsonObject();
header.updateString('alg', 'RS256');
try {
jws.setProtectedHeader(0, header);
} catch {
console.log(jws.lastErrorText);
return;
}
const bIncludeBom = false;
try {
jws.setPayload('This is the content to sign.', 'utf-8', bIncludeBom);
} 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 private key and set it for signature 0.
const privKey = new PrivateKey();
try {
privKey.loadPemFile('qa_data/signer_privkey.pem');
} catch {
console.log(privKey.lastErrorText);
return;
}
try {
jws.setPrivateKey(0, privKey);
} catch {
console.log(jws.lastErrorText);
return;
}
let jwsCompact: string;
try {
jwsCompact = jws.createJws();
} catch {
console.log(jws.lastErrorText);
return;
}
console.log(jwsCompact);
}