Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oCrypt = ObjCreate("Chilkat.Crypt2")

$oCrypt.EncodingMode = "hex"
$oCrypt.HashAlgorithm = "sha-1"

Local $sHashStr = $oCrypt.HashFileENC("qa_data/hamlet.xml")
ConsoleWrite("hash to sign: " & $sHashStr & @CRLF)

$oDsa = ObjCreate("Chilkat.Dsa")

Local $sPemPrivateKey
$sPemPrivateKey = $oDsa.LoadText("qa_data/dsa/dsaPrivKey2.pem")
$bSuccess = $oDsa.FromPem($sPemPrivateKey)
If ($bSuccess = False) Then
    ConsoleWrite($oDsa.LastErrorText & @CRLF)
    Exit
EndIf

; Load the hash to be signed into the DSA object:
$bSuccess = $oDsa.SetEncodedHash("hex",$sHashStr)
If ($bSuccess = False) Then
    ConsoleWrite($oDsa.LastErrorText & @CRLF)
    Exit
EndIf

; Sign the hash.
$bSuccess = $oDsa.SignHash()
If ($bSuccess = False) Then
    ConsoleWrite($oDsa.LastErrorText & @CRLF)
    Exit
EndIf

; Get the ASN.1 signature.
Local $sAsnSig = $oDsa.GetEncodedSignature("base64")
ConsoleWrite("Signature: " & $sAsnSig & @CRLF)

; Examine the details of the ASN.1 signature.
; We want to get the r,s values as hex strings..
$oAsn = ObjCreate("Chilkat.Asn")
$bSuccess = $oAsn.LoadEncoded($sAsnSig,"base64")
If ($bSuccess = False) Then
    ConsoleWrite($oAsn.LastErrorText & @CRLF)
    Exit
EndIf

; Get the ASN.1 as XML.
$oXml = ObjCreate("Chilkat.Xml")
$bSuccess = $oXml.LoadXml($oAsn.AsnToXml())
ConsoleWrite("Signature as XML: " & @CRLF)
ConsoleWrite($oXml.GetXml() & @CRLF)

; 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
Local $sR = "2C187F3AB6E47A66497B86CE97BB39E2133810F5"
Local $s = "588E53D3F7B69636B48FD7175E99A3961BD7D775"

; Build the XML that will be converted to ASN.1
$oXml.Clear 
$oXml.Tag = "sequence"
$oXml.NewChild2 "int",$sR
$oXml.NewChild2 "int",$s

; Convert the XML to ASN.1
$bSuccess = $oAsn.LoadAsnXml($oXml.GetXml())

; Emit the signature as DER encoded ASN.1 (base64)
$sAsnSig = $oAsn.GetEncodedDer("base64")

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

$oDsa2 = ObjCreate("Chilkat.Dsa")

; Load the DSA public key to be used for verification:
Local $sPemPublicKey
$sPemPublicKey = $oDsa2.LoadText("qa_data/dsa/dsaPubKey2.pem")
$bSuccess = $oDsa2.FromPublicPem($sPemPublicKey)
If ($bSuccess = False) Then
    ConsoleWrite($oDsa2.LastErrorText & @CRLF)
    Exit
EndIf

; Load the hash to be verified.
$bSuccess = $oDsa2.SetEncodedHash("hex",$sHashStr)
If ($bSuccess = False) Then
    ConsoleWrite($oDsa2.LastErrorText & @CRLF)
    Exit
EndIf

; Load the ASN.1 signature:
$bSuccess = $oDsa2.SetEncodedSignature("base64",$sAsnSig)
If ($bSuccess = False) Then
    ConsoleWrite($oDsa2.LastErrorText & @CRLF)
    Exit
EndIf

; Verify:
$bSuccess = $oDsa2.Verify()
If ($bSuccess = False) Then
    ConsoleWrite($oDsa2.LastErrorText & @CRLF)
Else
    ConsoleWrite("DSA Signature Verified!" & @CRLF)
EndIf