React Native Requires Chilkat v11.0.0+
React Native
Generate RSA Key and Sign a String
See more RSA Examples
Demonstrates how to generate a new RSA public/private key pair and use it to generate a signature for a string. The (binary) digital signature is returned as a hexidecimalized string.Chilkat React Native Downloads
import { PrivateKey, PublicKey, Rsa } from '@chilkat/react-native'
function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rsa = new Rsa();
// Generate a 2048-bit RSA key.
const privKey = new PrivateKey();
rsa.genKey(2048, privKey);
rsa.usePrivateKey(privKey);
// Return the signature in hex.
rsa.encodingMode = 'hex';
const strData = 'This is the string to be signed.';
// Sign the SHA256 hash of the string.
const hexSig = rsa.signStringENC(strData, 'sha256');
console.log(hexSig);
// Now verify the signature:
const pubKey = new PublicKey();
privKey.toPublicKey(pubKey);
rsa.usePublicKey(pubKey);
try {
rsa.verifyStringENC(strData, 'sha256', hexSig);
console.log('Signature verified!');
} catch {
console.log(rsa.lastErrorText);
}
// Try it with an invalid signature:
try {
rsa.verifyStringENC(strData, 'sha256', 'not a valid sig');
console.log('Signature verified!');
} catch {
console.log('Signature validation failed!');
}
// Try it with invalid data:
try {
rsa.verifyStringENC('Not the original data', 'sha256', hexSig);
console.log('Signature verified!');
} catch {
console.log('Signature validation failed!');
}
}