Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to reduce input length by 1 to be compatible with HTTP basic authentication #15

Open
tbraly opened this issue Apr 29, 2016 · 4 comments

Comments

@tbraly
Copy link

tbraly commented Apr 29, 2016

In the example code... using it to authenticate a RESTAPI, I ran into a problem, but fixed it by decreasing the input size by 1.

// encoding
char input[] = "username:password";
int inputLen = sizeof(input)-1; <== Need to do this (most likely the code is processing the \0)

int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];

// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, input, inputLen);

Serial.println(encoded);

will produce the following:
dXNlcm5hbWU6cGFzc3dvcmQ=

Here is what Apache will provide in the header for basic authentication... They match, but without decreasing the inputLen by 1, they will not.

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

@Cat30Mk3
Copy link

(1) I confirm that the above fix [ int inputLen = sizeof(input)-1 ] is needed and does work. I tried to use this library on ESP8266 smtp to smtp2go. Without the fix, the smtp2go server rejects the base64 encrypted userid and password, but with the fix the server accepts both and the email communication completes successfully.

(2) Note that there is a conflict with other base64.h files auto installed with esp8266 core library at least on the Arduino IDE 1.6.7 I am using. To use this library the base64.h and base64.cpp must be located in the same directory as the main.ino file and the include statement must be changed to #include "Base64.h" instead of #include <Base64.h>.

(3) And finally, the file base64.cpp must be modified as follows:
change #include <avr/pgmspace.h> to #include <pgmspace.h>

@Cat30Mk3
Copy link

(3) And finally, the file base64.cpp must be modified as follows:
in the #include statement change avr/pgmspace.h to pgmspace.h

@uzi18
Copy link

uzi18 commented Sep 8, 2017

int inputLen = strlen(input);

@palto42
Copy link

palto42 commented May 6, 2018

Another issue is that the length of the output char array must be increased by 1 because the library adds an extra \0 at the end (string termination).
See my #12 (comment) and pull request #23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants