Sample code for 30+ languages & platforms
React Native

DSA R,S Signature Values

See more DSA Examples

Creates a DSA signature. Gets r,s values from the signature. Re-creates the DSA signature ASN.1 from the r,s values. Then verifies the signature using the re-created ASN.1 DSA signature.

Chilkat React Native Downloads

React Native
import { Asn, Crypt2, Dsa, Xml } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const crypt = new Crypt2();

  crypt.encodingMode = 'hex';
  crypt.hashAlgorithm = 'sha-1';

  const hashStr = await crypt.hashFileENCAsync('qa_data/hamlet.xml');
  console.log(`hash to sign: ${hashStr}`);

  const dsa = new Dsa();

  const pemPrivateKey = dsa.loadText('qa_data/dsa/dsaPrivKey2.pem');
  try {
    dsa.fromPem(pemPrivateKey);
  } catch {
    console.log(dsa.lastErrorText);
    return;
  }

  // Load the hash to be signed into the DSA object:
  try {
    dsa.setEncodedHash('hex', hashStr);
  } catch {
    console.log(dsa.lastErrorText);
    return;
  }

  // Sign the hash.
  try {
    dsa.signHash();
  } catch {
    console.log(dsa.lastErrorText);
    return;
  }

  // Get the ASN.1 signature.
  let asnSig = dsa.getEncodedSignature('base64');
  console.log(`Signature: ${asnSig}`);

  // Examine the details of the ASN.1 signature.
  // We want to get the r,s values as hex strings..
  const asn = new Asn();
  try {
    asn.loadEncoded(asnSig, 'base64');
  } catch {
    console.log(asn.lastErrorText);
    return;
  }

  // Get the ASN.1 as XML.
  const xml = new Xml();
  xml.loadXml(asn.asnToXml());
  console.log('Signature as XML: ');
  console.log(xml.getXml());

  // Sample XML shown here.
  // The r and s values are the two hex strings in the XML.

  // <?xml version="1.0" encoding="utf-8"?>
  // <sequence>
  //     <int>2C187F3AB6E47A66497B86CE97BB39E2133810F5</int>
  //     <int>588E53D3F7B69636B48FD7175E99A3961BD7D775</int>
  // </sequence>

  // Pretend we're starting with r,s
  const r = '2C187F3AB6E47A66497B86CE97BB39E2133810F5';
  const s = '588E53D3F7B69636B48FD7175E99A3961BD7D775';

  // Build the XML that will be converted to ASN.1
  xml.clear();
  xml.tag = 'sequence';
  xml.newChild2('int', r);
  xml.newChild2('int', s);

  // Convert the XML to ASN.1
  asn.loadAsnXml(xml.getXml());

  // Emit the signature as DER encoded ASN.1 (base64)
  asnSig = asn.getEncodedDer('base64');

  // --------------------------------------------------------------------
  // Verify the signature using the asnSig we built from the r,s values
  // --------------------------------------------------------------------

  const dsa2 = new Dsa();

  // Load the DSA public key to be used for verification:
  const pemPublicKey = dsa2.loadText('qa_data/dsa/dsaPubKey2.pem');
  try {
    dsa2.fromPublicPem(pemPublicKey);
  } catch {
    console.log(dsa2.lastErrorText);
    return;
  }

  // Load the hash to be verified.
  try {
    dsa2.setEncodedHash('hex', hashStr);
  } catch {
    console.log(dsa2.lastErrorText);
    return;
  }

  // Load the ASN.1 signature:
  try {
    dsa2.setEncodedSignature('base64', asnSig);
  } catch {
    console.log(dsa2.lastErrorText);
    return;
  }

  // Verify:
  try {
    dsa2.verify();
    console.log('DSA Signature Verified!');
  } catch {
    console.log(dsa2.lastErrorText);
  }
}