adamvr / arduino-base64

A base64 library for the arduino platform, written in C
MIT License
184 stars 102 forks source link

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

Open tbraly opened 8 years ago

tbraly commented 8 years ago

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 commented 8 years ago

(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 .

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

Cat30Mk3 commented 8 years ago

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

uzi18 commented 7 years ago

int inputLen = strlen(input);

palto42 commented 6 years ago

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 https://github.com/adamvr/arduino-base64/issues/12#issuecomment-386869533 and pull request #23