Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
I have faced a problem in using HMAC of cryptopp library in decoding section.
I have used the flowing code for encoding the planText and it works okay
this code is described in this link [HMAC - Crypto++ Wiki[^]]
AutoSeededRandomPool prng;

    SecByteBlock key(16);
    prng.GenerateBlock(key, key.size());

    string plain = "HMAC Test";
    string mac, encoded;

    /*********************************\
    \*********************************/

    // Pretty print key
    encoded.clear();
    StringSource ss1(key, key.size(), true,
        new HexEncoder(
            new StringSink(encoded)
        ) // HexEncoder
    ); // StringSource

    cout << "key: " << encoded << endl;
    cout << "plain text: " << plain << endl;

    /*********************************\
    \*********************************/

    try
    {
        HMAC< SHA256 > hmac(key, key.size());

        StringSource ss2(plain, true,
            new HashFilter(hmac,
                new StringSink(mac)
            ) // HashFilter      
        ); // StringSource
    }
    catch (const CryptoPP::Exception& e)
    {
        cerr << e.what() << endl;
        exit(1);
    }

    /*********************************\
    \*********************************/

    // Pretty print
    encoded.clear();
    StringSource ss3(mac, true,
        new HexEncoder(
            new StringSink(encoded)
        ) // HexEncoder
    ); // StringSource
    cout << "hmac: " << encoded << endl;


What I have tried:

now for decoding that on the other side
I read this link but it didn't help me HexDecoder - Crypto++ Wiki[^]
and this section doesn't return the plaintext
string decoded;
StringSource ss(encoded, true,
    new HexDecoder(
        new StringSink(decoded)
    ) // HexDecoder
);

I Will be very grateful for any advice
Posted
Updated 30-Oct-19 8:44am
v2

HMAC is a one-way cryptographic hash function. You cannot retrieve the plaintext from the hash.

HMAC - Wikipedia[^]
 
Share this answer
 
v2
Comments
saide_a 2-Nov-19 9:14am    
many thanks, I understood , I have another question , if it is possible you help me.
which part of message should be use in making signature(I am going to use SHA_1).
and does length of that(part of message in signature) matter?
for example I have source and destination id , time and sequence number in my message.
HMAC is a cryptographic hash, not an encryption.

You cannot get the original content back from the hash value.
 
Share this answer
 
Comments
saide_a 2-Nov-19 9:14am    
many thanks, I understood , I have another question , if it is possible you help me.
which part of message should be use in making signature(I am going to use SHA_1).
and does length of that(part of message in signature) matter?
for example I have source and destination id , time and sequence number in my message.
Dave Kreskowiak 2-Nov-19 9:40am    
What are you talking about? If you're generating a signature, which is just a hash value, for the message to protect the message, you don't use a part of the message, you use the entire message.
saide_a 3-Nov-19 1:32am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900