Reusing CryptoPP::StringSource object and accumulating message to be hashed

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Reusing CryptoPP::StringSource object and accumulating message to be hashed

Nico
1) How can I re-use a StringSource object created for a pipeline?

2) Is it possible to hold back on the generation of the hash so I can add more information to be hashed? Or I should concatenate my input message before hand?

#include <iostream>

#include "cryptopp/keccak.h"
#include "cryptopp/hex.h"

int main(int argc, char* argv[]) {
    try {
        // Hash with sha3 and a non-standard digest
        CryptoPP::Keccak hash = CryptoPP::Keccak(16);
        std::string message = " A message ";

        std::cout<<"Input message: " << message << std::endl;

        std::string output;

        // Pipeline
        CryptoPP::StringSource ss1(message, true,
                new CryptoPP::HashFilter( hash ,
                new CryptoPP::HexEncoder(
                new CryptoPP::StringSink( output ))));
        std::cout<<"Output message: " << output << std::endl;

    }
    catch(const CryptoPP::Exception& ex) {
        std::cerr <<ex.what() <<std::endl;
    }
    return 0;
}


Can I create this ss1 object and reuse multiple times?

Thank you in advance!
Nico

--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to [hidden email].
More information about Crypto++ and this group is available at http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reusing CryptoPP::StringSource object and accumulating message to be hashed

Jeffrey Walton-3


On Friday, July 7, 2017 at 1:04:19 PM UTC-4, Nico wrote:
1) How can I re-use a StringSource object created for a pipeline?

Hmmm... I've never seen that use case. Given you cannot seek/rewind in a source, the answer is likely NO.
 
2) Is it possible to hold back on the generation of the hash so I can add more information to be hashed? Or I should concatenate my input message before hand?

YES. Instead of using a pipeline, Update() data directly into the hash. When you are done adding data, then call FInal().

 Looking at the wiki, Hash Function has the example you are looking for at https://www.cryptopp.com/wiki/Hash_Functions. You want to follow the SHA example.

Now, you can wrap the Constructor, Update and Final into a more Javaa-esque pattern using Init/Update/Final. For an example of that using block ciphers (not hashes), see https://www.cryptopp.com/wiki/Init-Update-Final.

Can I create this ss1 object and reuse multiple times?

NO.

Jeff

--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to [hidden email].
More information about Crypto++ and this group is available at http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reusing CryptoPP::StringSource object and accumulating message to be hashed

Nico
Thank you, Jeff, for the message.

Update and Final were exactly what I was looking for and I am already incorporating on my project.

Your mention to "seek/rewind in a source" made me understand why you cannot do it, hashing is a one way road after all. Thanks!

Best,
Nico

On Saturday, July 15, 2017 at 10:30:36 AM UTC-5, Jeffrey Walton wrote:


On Friday, July 7, 2017 at 1:04:19 PM UTC-4, Nico wrote:
1) How can I re-use a StringSource object created for a pipeline?

Hmmm... I've never seen that use case. Given you cannot seek/rewind in a source, the answer is likely NO.
 
2) Is it possible to hold back on the generation of the hash so I can add more information to be hashed? Or I should concatenate my input message before hand?

YES. Instead of using a pipeline, Update() data directly into the hash. When you are done adding data, then call FInal().

 Looking at the wiki, Hash Function has the example you are looking for at <a href="https://www.cryptopp.com/wiki/Hash_Functions" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.cryptopp.com%2Fwiki%2FHash_Functions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGpvQtxVus_PI6sPAOYHTXqqFGLSA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.cryptopp.com%2Fwiki%2FHash_Functions\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGpvQtxVus_PI6sPAOYHTXqqFGLSA&#39;;return true;">https://www.cryptopp.com/wiki/Hash_Functions. You want to follow the SHA example.

Now, you can wrap the Constructor, Update and Final into a more Javaa-esque pattern using Init/Update/Final. For an example of that using block ciphers (not hashes), see <a href="https://www.cryptopp.com/wiki/Init-Update-Final" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.cryptopp.com%2Fwiki%2FInit-Update-Final\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEQa6SI_YktZqEQoligVOv473wWaw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.cryptopp.com%2Fwiki%2FInit-Update-Final\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEQa6SI_YktZqEQoligVOv473wWaw&#39;;return true;">https://www.cryptopp.com/wiki/Init-Update-Final.

Can I create this ss1 object and reuse multiple times?

NO.

Jeff

--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to [hidden email].
More information about Crypto++ and this group is available at http://www.cryptopp.com.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...