base64.ino 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <gBase64.h>
  2. #include <ESP.h>
  3. /*
  4. Base64 Encode/Decode example
  5. Encodes the text "Hello world" to "SGVsbG8gd29ybGQA" and decodes "Zm9vYmFy" to "foobar"
  6. Created 29 April 2015
  7. by Nathan Friedly - http://nfriedly.com/
  8. This example code is in the public domain.
  9. */
  10. void setup()
  11. {
  12. // start serial port at 9600 bps:
  13. Serial.begin(9600);
  14. while (!Serial) {
  15. ; // wait for serial port to connect. Needed for Leonardo only
  16. }
  17. Serial.println("Base64 example");
  18. // encoding
  19. char input[] = "Hello world";
  20. int inputLen = sizeof(input);
  21. int encodedLen = base64_enc_len(inputLen);
  22. char encoded[encodedLen];
  23. Serial.print(input); Serial.print(" = ");
  24. // note input is consumed in this step: it will be empty afterwards
  25. base64_encode(encoded, input, inputLen);
  26. Serial.println(encoded);
  27. // decoding
  28. char input2[] = "Zm9vYmFy";
  29. int input2Len = sizeof(input2);
  30. int decodedLen = base64_dec_len(input2, input2Len);
  31. char decoded[decodedLen];
  32. base64_decode(decoded, input2, input2Len);
  33. Serial.print(input2); Serial.print(" = "); Serial.println(decoded);
  34. }
  35. void loop()
  36. {
  37. }