4

I have a regex to validate if the string is a base64 string. I also check if the mimimum legnth of the base 64 string is 4 but what is the maximum length of characters a base64 string can contain??

I created a regex and added a mimimun atribute and added validation.

public class EndpointAddRequest : RequestBase
{
    [MinLength(4, ErrorMessage = "The number of characters is less than the minimum amount")]
    [RegularExpression(RegexConstants.Base64String, ErrorMessage = "Invalid AccessDevice Uid")]
    [Required(ErrorMessage = "AccessDevice uid is required")]
 }
preciousbetine
  • 2,959
  • 3
  • 13
  • 29

2 Answers2

2

As far as I know there is no maximum length.

You can view the specification here.

Community
  • 1
  • 1
Marie
  • 2,114
  • 1
  • 17
  • 31
2

There is no maximum. The minimum comes from the fact that Base64 encodes 3 input bytes into 4 on the output, plus padding if necesary. But there is no such limitation of how many characters can be encoded, as literally any byte stream (regardless or lenght) input is valid and will produce therefore an arbitrary long base64 output.

The practical limit is the amount of storage (either memory/disk/whatever) one has available, but the encoding itself doesn't place any restrictions.

Alejandro
  • 7,290
  • 4
  • 34
  • 59