Is an empty string valid base64 encoded data of zero bytes length?

前端 未结 2 758
夕颜
夕颜 2021-02-18 16:37

One of my colleges was telling me that the empty string is not a valid base64 encoded data string. I don\'t think this is true (he is too lazy to parse it), but after googling a

相关标签:
2条回答
  • 2021-02-18 16:51

    According to RFC 4648 Section 10, Test Vectors,

    BASE64("") = ""
    

    I would assume the inverse must hold as well.

    0 讨论(0)
  • 2021-02-18 17:06

    My thought on this is that there are two possible base64 values that an empty string could produce; either an empty string, or a string that consists entirely of pad characters ('==='). Any other valid base64 string contains information. With the second case, we can apply the following rule from the RFC:

    If more than the allowed number of pad characters are found at the end of the string, e.g., a base 64 string terminated with "===", the excess pad characters could be ignored.

    As they can be ignored, they can be dropped from the resultant encoded string without consequence, once again leaving us with an empty string as the base64 representation of an empty string.

    0 讨论(0)
提交回复
热议问题