Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final crypt = CkCrypt2();
crypt.encodingMode = 'hex';
crypt.hashAlgorithm = 'sha-1';
final hashStr = crypt.hashFileENC('qa_data/hamlet.xml');
print('hash to sign: $hashStr');
final dsa = CkDsa();
final pemPrivateKey = dsa.loadText('qa_data/dsa/dsaPrivKey2.pem');
try {
dsa.fromPem(pemPrivateKey);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Load the hash to be signed into the DSA object:
try {
dsa.setEncodedHash('hex', hashStr);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Sign the hash.
try {
dsa.signHash();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Get the ASN.1 signature.
var asnSig = dsa.getEncodedSignature('base64');
print('Signature: $asnSig');
// Examine the details of the ASN.1 signature.
// We want to get the r,s values as hex strings..
final asn = CkAsn();
try {
asn.loadEncoded(asnSig, 'base64');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Get the ASN.1 as XML.
final xml = CkXml();
xml.loadXml(asn.asnToXml());
print('Signature as XML: ');
print(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
final r = '2C187F3AB6E47A66497B86CE97BB39E2133810F5';
final 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
// --------------------------------------------------------------------
final dsa2 = CkDsa();
// Load the DSA public key to be used for verification:
final pemPublicKey = dsa2.loadText('qa_data/dsa/dsaPubKey2.pem');
try {
dsa2.fromPublicPem(pemPublicKey);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Load the hash to be verified.
try {
dsa2.setEncodedHash('hex', hashStr);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Load the ASN.1 signature:
try {
dsa2.setEncodedSignature('base64', asnSig);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Verify:
try {
dsa2.verify();
print('DSA Signature Verified!');
} on ChilkatException catch (e) {
print(e.lastErrorText);
}
}