#include <CkCrypt2>
#include <CkDsa.h>

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

    CkCrypt2 crypt;

    crypt.put_EncodingMode("hex");
    crypt.put_HashAlgorithm("sha-1");

    // Return the SHA-1 hash of a file.  The file may be any size.
    // The Chilkat Crypt component will stream the file when 
    // computing the hash, keeping the memory usage constant
    // and reasonable.
    // The 20-byte SHA-1 hash is returned as a hex-encoded string.
    const char *hashStr = crypt.hashFileENC("hamlet.xml");

    CkDsa dsa;

    // Load a DSA private key from a PEM file.  Chilkat DSA
    // provides the ability to load and save DSA public and private
    // keys from encrypted or non-encrypted PEM or DER.
    // The LoadText method is for convenience only.  You may
    // use any means to load the contents of a PEM file into
    // a string.
    const char *pemPrivateKey = 0;
    pemPrivateKey = dsa.loadText("dsa_priv.pem");
    bool success = dsa.FromPem(pemPrivateKey);
    if (success != true) {
        std::cout << dsa.lastErrorText() << "\r\n";
        return;
    }

    // You may optionally verify the key to ensure that it is a valid
    // DSA key.
    success = dsa.VerifyKey();
    if (success != true) {
        std::cout << dsa.lastErrorText() << "\r\n";
        return;
    }

    // Load the hash to be signed into the DSA object:
    success = dsa.SetEncodedHash("hex",hashStr);
    if (success != true) {
        std::cout << dsa.lastErrorText() << "\r\n";
        return;
    }

    // Now that the DSA object contains both the private key and hash,
    // it is ready to create the signature:
    success = dsa.SignHash();
    if (success != true) {
        std::cout << dsa.lastErrorText() << "\r\n";
        return;
    }

    // If SignHash is successful, the DSA object contains the
    // signature.  It may be accessed as a hex or base64 encoded
    // string.  (It is also possible to access directly in byte array form via
    // the "Signature" property.)
    const char *hexSig = dsa.getEncodedSignature("hex");
    std::cout << "Signature:" << "\r\n";
    std::cout << hexSig << "\r\n";

    // -----------------------------------------------------------
    // Step 2: Verify the DSA Signature
    // -----------------------------------------------------------

    CkDsa dsa2;

    // Load the DSA public key to be used for verification:
    const char *pemPublicKey = 0;
    pemPublicKey = dsa2.loadText("dsa_pub.pem");
    success = dsa2.FromPublicPem(pemPublicKey);
    if (success != true) {
        std::cout << dsa2.lastErrorText() << "\r\n";
        return;
    }

    // Load the hash to be verified against the signature.
    success = dsa2.SetEncodedHash("hex",hashStr);
    if (success != true) {
        std::cout << dsa2.lastErrorText() << "\r\n";
        return;
    }

    // Load the signature:
    success = dsa2.SetEncodedSignature("hex",hexSig);
    if (success != true) {
        std::cout << dsa2.lastErrorText() << "\r\n";
        return;
    }

    // Verify:
    success = dsa2.Verify();
    if (success != true) {
        std::cout << dsa2.lastErrorText() << "\r\n";
    }
    else {
        std::cout << "DSA Signature Verified!" << "\r\n";
    }
    }
 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}