Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:42:21+00:00 2026-06-15T08:42:21+00:00

So I have a little unusual situation, that my experience with templates and tutorials

  • 0

So I have a little unusual situation, that my experience with templates and tutorials doesn’t seem to cover.

I am writing an program that automatically signs messages for another program that is incapable but has a limited API. There is a configuration file that contains a string telling my program what hash to use to sign a message.
This string is stored in a signatureAlgorithm object which stores a digest, the size the digest should be, and offers a few options for the encoding of the digest.
I am using the Crypto++ library for the hashing functions.
Right now I have a large set of if/else if statements and three commands in each one depending on what the string says the algorithm should be, as shown below:

void signatureAlgorithm::createDigest(string input)
{
bool digestFail = false;

if (_algorithm == "md5")
{
    //byte digest[ CryptoPP::MD5::DIGESTSIZE ];
    _digestSize = (CryptoPP::MD5::DIGESTSIZE);
    _digest = new byte[ _digestSize ];
    CryptoPP::MD5().CalculateDigest( _digest, (byte*) input.c_str(), input.length() );
}

.....

else if (_algorithm == "sha256")
{
    _digestSize = (CryptoPP::SHA256::DIGESTSIZE);
    _digest = new byte[ _digestSize ];
    CryptoPP::SHA256().CalculateDigest( _digest, (byte*) input.c_str(), input.length() );
}
else if (_algorithm == "sha512")
{
    _digestSize = (CryptoPP::SHA512::DIGESTSIZE);
    _digest = new byte[ _digestSize ];
    CryptoPP::SHA512().CalculateDigest( _digest, (byte*) input.c_str(), input.length() );
}
else
    digestFail = true;

if (!digestFail)
    _ready = true;
else
    _ready = false;
}

_digestSize stores the digest size in the object, as _digest does for the bytes of the digest (bytes are typedef const unsigned char*), and _ready is used in other functions to determine of the value has been digested before giving it to a calling program, or instead returning an error to the program.

I eventually plan to switch to a map to make things more efficient and tidier, however before I do I have to find a way to make the setting of _digestSize and the calling of CreateDigest() more generic. I am trying to avoid modifying the library as I want to maintain the FIPS status of Crypto++ in compiled form in the final release, but for now I’m just testing using the source if that makes a difference.

I thought about templates, with the inner section of each if statement just setting the value, but I can’t find an example of the template being used in such a fashion. I find them for making classes where there are other functions, but in this case I just need to be able to make a generic class that simply points to another class if I am thinking right here.

pseudo-code for what I’m trying to do:

class tempClass {};

if (_algorithm == "md5")
{
  tempClass = CryptoPP::MD5
}
......
else
  digestFail = true;

_digestSize = (tempClass::DIGESTSIZE);
_digest = new byte[_digestSize];
tempClass().CalculateDigest( _digest, (byte*) input.c_str(), input.length() );

-Jimmy

EDIT: While in the comments I stated that inheritance wouldn’t work because of the forking in the class tree, it turns out that the documentation I’ve been using is for the most recent version. The version I’m using for testing and the FIPS version are older, from before functions like MD5 etc were split out into a separate “weak” structure.

EDIT 2: However, I’m still curious as to if there were a way to go about this even if the classes were forked and didn’t inherit CalculateDigest from the same place.

EDIT 3: John Bandela answered the question, but I just had to change what he gave me a little bit. Changes are in the comments for the answer. Thanks all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:42:22+00:00Added an answer on June 15, 2026 at 8:42 am

    Break out the code into a template function like this

    // Assuming _digest and _digest size are class members;

    template<class Digest>
    byte* doDigest(string input, int& digestSize){
            int digestSize = (Digest::DIGESTSIZE);
            byte* digest = new byte[ digestSize ];
            Digest().CalculateDigest( digest, (byte*) input.c_str(), input.length() );
            return digest;
     }
    

    Then in your createDigest function do this

    if (_algorithm == "md5")
    {
        _digest = doDigest<CryptoPP::MD5>(input,_digestSize);
    }
    
    else if (_algorithm == "sha256")
    {
        _digest = doDigest<CryptoPP::SHA256>(input,_digestSize);
    }
    ...
    

    Off topic, but you may want to consider using a vector instead of byte*

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It may seem a little unusual but it is quite likely that only one
I have little unusual problem to solve. I need some hint or links to
I have little JAVA coding experience (some classes at university) but none in the
I have little program creating a maze. It uses lots of collections (the default
I have used Jetty in the past but I have little experience with jWebSocket.
I have a little problem with Excel. I nead to make a function that
I have little experience in C#.NET, and below is what I got so far:
i have little experience with Liferay, builded a couple of sites with static content
I know it is a little unusual, however I have a book on Oracle
I have little experience with CSS so this might be a very simple problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.