base64_url decode long string got run-time error #33

Closed
opened 2017-08-10 20:06:44 -05:00 by Mark-Joy · 4 comments
Mark-Joy commented 2017-08-10 20:06:44 -05:00 (Migrated from github.com)

Hello,
First of all, thank you for your lib!

I have run-time error when trying to decode the below:

std::string mydecode = base64_url::decode<std::string>("EtwFwjS7cNIZjXcufbR6r6mg8s1_e-jfObtENsgjQamDuuJZlT7lpReGgtl1bac77Lk0IDPNyFkhR-Rdv9kDBYcERUWMt_WBrqErnuciol09BReP7UCwfgwFftv3FMjRE29WpXInQ4vfFtbyd6YLN5AKAVZ-r5EcBYVdnjqli4zDR8TCKOPo1jhV5g3w43PPVSsc3KJS_C-i7SlJoGAjfWD97mL6kTYnoivbh_f61Vch9jS_zxslmqD_If0ZM7VtPxFE0oIPlbAka-OchPjrdEwF1lQqsmB1mE8zRF3yvPrz8tDjic3EQEjr-i6_F4NHJNg6tUq466DozbWct2ZrQdh70I764VrcktS1M93Rm5KjzEMUv7MmFjb03sWw7j2iVuLWgir4BqxGdS8E6yo8Vw12xk0pjOAaeuE_yzRRh_k_yAH2BkPFc7cibOYWcfOBL8-fd3PAbmSTm1BRSmZA3Epo9W93zVB6yazqk8YdvRmAde6WgPP8z2jckrLwpK88TySatMllkuxvpto0ZvdSDXNw2SfDuFrmxWPTsk0BHpS5jxjhV4R_FPyzTUAGijS_FJsfwXn-G1ii8AsDvyVuq3Om0cQmJ-mNC8pqTXvTvso556hIm1jHb-mIfhiwZk4gJONTqqQ_NPTA1vl6Omzznbl_BKSLlc4OcdIGqEdRll4NNjoxdVqWlNaS4O9L0C2UfEqK7NWAPymiLBCFYxSWsbul9seiZvS_UhV3I7lDDwy-40mQV9UN0wXV0cRZVpr_YrRvE9-SWtpyb3ZOi5y52d6oP6MeMe-kRvQRF8OkXsw-oXhkpOpMzcKi2xq9Q4k6Vuvme5s1PHCN8gAC4fJ4jm2H_XsJA6qFyDfXAdvcu6Z_GINje-sPRFc7Gpl88tbmPOAT-k0a-xeGMSd8h5xYIXcnrGjqXHFRxl_qPA");

Could you help me out?

Hello, First of all, thank you for your lib! I have run-time error when trying to decode the below: `std::string mydecode = base64_url::decode<std::string>("EtwFwjS7cNIZjXcufbR6r6mg8s1_e-jfObtENsgjQamDuuJZlT7lpReGgtl1bac77Lk0IDPNyFkhR-Rdv9kDBYcERUWMt_WBrqErnuciol09BReP7UCwfgwFftv3FMjRE29WpXInQ4vfFtbyd6YLN5AKAVZ-r5EcBYVdnjqli4zDR8TCKOPo1jhV5g3w43PPVSsc3KJS_C-i7SlJoGAjfWD97mL6kTYnoivbh_f61Vch9jS_zxslmqD_If0ZM7VtPxFE0oIPlbAka-OchPjrdEwF1lQqsmB1mE8zRF3yvPrz8tDjic3EQEjr-i6_F4NHJNg6tUq466DozbWct2ZrQdh70I764VrcktS1M93Rm5KjzEMUv7MmFjb03sWw7j2iVuLWgir4BqxGdS8E6yo8Vw12xk0pjOAaeuE_yzRRh_k_yAH2BkPFc7cibOYWcfOBL8-fd3PAbmSTm1BRSmZA3Epo9W93zVB6yazqk8YdvRmAde6WgPP8z2jckrLwpK88TySatMllkuxvpto0ZvdSDXNw2SfDuFrmxWPTsk0BHpS5jxjhV4R_FPyzTUAGijS_FJsfwXn-G1ii8AsDvyVuq3Om0cQmJ-mNC8pqTXvTvso556hIm1jHb-mIfhiwZk4gJONTqqQ_NPTA1vl6Omzznbl_BKSLlc4OcdIGqEdRll4NNjoxdVqWlNaS4O9L0C2UfEqK7NWAPymiLBCFYxSWsbul9seiZvS_UhV3I7lDDwy-40mQV9UN0wXV0cRZVpr_YrRvE9-SWtpyb3ZOi5y52d6oP6MeMe-kRvQRF8OkXsw-oXhkpOpMzcKi2xq9Q4k6Vuvme5s1PHCN8gAC4fJ4jm2H_XsJA6qFyDfXAdvcu6Z_GINje-sPRFc7Gpl88tbmPOAT-k0a-xeGMSd8h5xYIXcnrGjqXHFRxl_qPA");` Could you help me out?
efidler commented 2017-08-10 20:18:32 -05:00 (Migrated from github.com)

I don't think your string is padded correctly. Try

std::string mydecode = base64_url_unpadded::decode<std::string>("EtwFwjS7cNIZjXcufbR6r6mg8s1_e-jfObtENsgjQamDuuJZlT7lpReGgtl1bac77Lk0IDPNyFkhR-Rdv9kDBYcERUWMt_WBrqErnuciol09BReP7UCwfgwFftv3FMjRE29WpXInQ4vfFtbyd6YLN5AKAVZ-r5EcBYVdnjqli4zDR8TCKOPo1jhV5g3w43PPVSsc3KJS_C-i7SlJoGAjfWD97mL6kTYnoivbh_f61Vch9jS_zxslmqD_If0ZM7VtPxFE0oIPlbAka-OchPjrdEwF1lQqsmB1mE8zRF3yvPrz8tDjic3EQEjr-i6_F4NHJNg6tUq466DozbWct2ZrQdh70I764VrcktS1M93Rm5KjzEMUv7MmFjb03sWw7j2iVuLWgir4BqxGdS8E6yo8Vw12xk0pjOAaeuE_yzRRh_k_yAH2BkPFc7cibOYWcfOBL8-fd3PAbmSTm1BRSmZA3Epo9W93zVB6yazqk8YdvRmAde6WgPP8z2jckrLwpK88TySatMllkuxvpto0ZvdSDXNw2SfDuFrmxWPTsk0BHpS5jxjhV4R_FPyzTUAGijS_FJsfwXn-G1ii8AsDvyVuq3Om0cQmJ-mNC8pqTXvTvso556hIm1jHb-mIfhiwZk4gJONTqqQ_NPTA1vl6Omzznbl_BKSLlc4OcdIGqEdRll4NNjoxdVqWlNaS4O9L0C2UfEqK7NWAPymiLBCFYxSWsbul9seiZvS_UhV3I7lDDwy-40mQV9UN0wXV0cRZVpr_YrRvE9-SWtpyb3ZOi5y52d6oP6MeMe-kRvQRF8OkXsw-oXhkpOpMzcKi2xq9Q4k6Vuvme5s1PHCN8gAC4fJ4jm2H_XsJA6qFyDfXAdvcu6Z_GINje-sPRFc7Gpl88tbmPOAT-k0a-xeGMSd8h5xYIXcnrGjqXHFRxl_qPA");

or

std::string mydecode = base64_url::decode<std::string>("EtwFwjS7cNIZjXcufbR6r6mg8s1_e-jfObtENsgjQamDuuJZlT7lpReGgtl1bac77Lk0IDPNyFkhR-Rdv9kDBYcERUWMt_WBrqErnuciol09BReP7UCwfgwFftv3FMjRE29WpXInQ4vfFtbyd6YLN5AKAVZ-r5EcBYVdnjqli4zDR8TCKOPo1jhV5g3w43PPVSsc3KJS_C-i7SlJoGAjfWD97mL6kTYnoivbh_f61Vch9jS_zxslmqD_If0ZM7VtPxFE0oIPlbAka-OchPjrdEwF1lQqsmB1mE8zRF3yvPrz8tDjic3EQEjr-i6_F4NHJNg6tUq466DozbWct2ZrQdh70I764VrcktS1M93Rm5KjzEMUv7MmFjb03sWw7j2iVuLWgir4BqxGdS8E6yo8Vw12xk0pjOAaeuE_yzRRh_k_yAH2BkPFc7cibOYWcfOBL8-fd3PAbmSTm1BRSmZA3Epo9W93zVB6yazqk8YdvRmAde6WgPP8z2jckrLwpK88TySatMllkuxvpto0ZvdSDXNw2SfDuFrmxWPTsk0BHpS5jxjhV4R_FPyzTUAGijS_FJsfwXn-G1ii8AsDvyVuq3Om0cQmJ-mNC8pqTXvTvso556hIm1jHb-mIfhiwZk4gJONTqqQ_NPTA1vl6Omzznbl_BKSLlc4OcdIGqEdRll4NNjoxdVqWlNaS4O9L0C2UfEqK7NWAPymiLBCFYxSWsbul9seiZvS_UhV3I7lDDwy-40mQV9UN0wXV0cRZVpr_YrRvE9-SWtpyb3ZOi5y52d6oP6MeMe-kRvQRF8OkXsw-oXhkpOpMzcKi2xq9Q4k6Vuvme5s1PHCN8gAC4fJ4jm2H_XsJA6qFyDfXAdvcu6Z_GINje-sPRFc7Gpl88tbmPOAT-k0a-xeGMSd8h5xYIXcnrGjqXHFRxl_qPA==");

I don't think your string is padded correctly. Try `std::string mydecode = base64_url_unpadded::decode<std::string>("EtwFwjS7cNIZjXcufbR6r6mg8s1_e-jfObtENsgjQamDuuJZlT7lpReGgtl1bac77Lk0IDPNyFkhR-Rdv9kDBYcERUWMt_WBrqErnuciol09BReP7UCwfgwFftv3FMjRE29WpXInQ4vfFtbyd6YLN5AKAVZ-r5EcBYVdnjqli4zDR8TCKOPo1jhV5g3w43PP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r-i6_F4NHJNg6tUq466DozbWct2ZrQdh70I764VrcktS1M93Rm5KjzEMUv7MmFjb03sWw7j2iVuLWgir4BqxGdS8E6yo8Vw12xk0pjOAaeuE_yzRRh_k_yAH2BkPFc7cibOYWcfOBL8-fd3PAbmSTm1BRSmZA3Epo9W93zVB6yazqk8YdvRmAde6WgPP8z2jckrLwpK88TySatMllkuxvpto0ZvdSDXNw2SfDuFrmxWPTsk0BHpS5jxjhV4R_FPyzTUAGijS_FJsfwXn-G1ii8AsDvyVuq3Om0cQmJ-mNC8pqTXvTvso556hIm1jHb-mIfhiwZk4gJONTqqQ_NPTA1vl6Omzznbl_BKSLlc4OcdIGqEdRll4NNjoxdVqWlNaS4O9L0C2UfEqK7NWAPymiLBCFYxSWsbul9seiZvS_UhV3I7lDDwy-40mQV9UN0wXV0cRZVpr_YrRvE9-SWtpyb3ZOi5y52d6oP6MeMe-kRvQRF8OkXsw-oXhkpOpMzcKi2xq9Q4k6Vuvme5s1PHCN8gAC4fJ4jm2H_XsJA6qFyDfXAdvcu6Z_GINje-sPRFc7Gpl88tbmPOAT-k0a-xeGMSd8h5xYIXcnrGjqXHFRxl_qPA==");`
Mark-Joy commented 2017-08-10 20:38:35 -05:00 (Migrated from github.com)

ohh, I see..
Could you make it auto detecting whether it was padded or not before decoding?
because when one person mixed padded and no-padded string, to know exactly which one to use at run-time is troublesome.
Something like below:
function encode(string, bool want_to_pad = false); -->option to pad
function decode(string); --> auto detect padding and decode

ohh, I see.. Could you make it auto detecting whether it was padded or not before decoding? because when one person mixed padded and no-padded string, to know exactly which one to use at run-time is troublesome. Something like below: function encode(string, bool want_to_pad = false); -->option to pad function decode(string); --> auto detect padding and decode
jpetso commented 2017-08-11 09:53:24 -05:00 (Migrated from github.com)

base64_url_unpadded accepts padding, but doesn't require it (in your words, "auto detect padding"). I believe that's what you're asking for for decode().

Regarding encode(), adding a run-time option is made harder by the architectural choices for this project. On one hand, I use the want_to_pad option (in the code: generates_padding()) at compile time to decide whether padding should even be part of the code base. On the other hand, cppcodec provides the same API for a number of different codecs - some are stricter about padding and don't allow it at all, it would be wrong for those to include it in the API. Both of those would be significant changes and I'm not sure that it's a good trade-off as opposed to just using different codec names.

Please let me know how you would change the documentation so that you wouldn't run into the same issue if you did it again from scratch.

`base64_url_unpadded` accepts padding, but doesn't require it (in your words, "auto detect padding"). I believe that's what you're asking for for decode(). Regarding encode(), adding a run-time option is made harder by the architectural choices for this project. On one hand, I use the want_to_pad option (in the code: `generates_padding()`) at compile time to decide whether padding should even be part of the code base. On the other hand, cppcodec provides the same API for a number of different codecs - some are stricter about padding and don't allow it at all, it would be wrong for those to include it in the API. Both of those would be significant changes and I'm not sure that it's a good trade-off as opposed to just using different codec names. Please let me know how you would change the documentation so that you wouldn't run into the same issue if you did it again from scratch.
Mark-Joy commented 2017-08-14 05:21:12 -05:00 (Migrated from github.com)

Thank you for your answer. I think a simple recommendation in Readme page is enough.
Some thing like: If you don't know whether the encoded message was padded or not, then just use base64_url_unpadded::decode because it works on both situations.

Thank you for your answer. I think a simple recommendation in Readme page is enough. Some thing like: If you don't know whether the encoded message was padded or not, then just use base64_url_unpadded::decode because it works on both situations.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-tplgy/cppcodec#33
No description provided.