diff --git a/cores/esp8266/base64.cpp b/cores/esp8266/base64.cpp
index a1e6261890..3ae51f5a0f 100644
--- a/cores/esp8266/base64.cpp
+++ b/cores/esp8266/base64.cpp
@@ -36,8 +36,9 @@ extern "C" {
  * @return String
  */
 String base64::encode(uint8_t * data, size_t length, bool doNewLines) {
-    // base64 needs more size then the source data
-    size_t size = ((length * 1.6f) + 1);
+    // base64 needs more size then the source data, use cencode.h macros
+    size_t size = ((doNewLines ? base64_encode_expected_len(length)
+                               : base64_encode_expected_len_nonewlines(length)) + 1);
     char * buffer = (char *) malloc(size);
     if(buffer) {
         base64_encodestate _state;