Search code examples
regexstringbase64ed25519

Validate Base64 encoded 256 bit numbers for digital signature keys


I've seen many RegEx answers on how to check for Base64, but I can't find one specifically for representations of 256-bit numbers.

I'm brand new to Base64, byte conversions, and RegEx. This answer seems to be the best for checking Base64, but I can't tell from the details if it can be specifically applied to a representation of a 256-bit number.

^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$

I need to make sure of the validity of these strings because I'm using them as encodings of Ed25519 keys, and my en/decoder seems to accept non-ASCII characters.

I don't really understand if that can specifically be applied to a representation of a 256-bit number.

How can RegEx validate a Base64 encoded 256 bit number?


Solution

  • As portforwardpodcast pointed out, a 256 bit number will be decoded into 43 characters with one = at the end as filler.

    Only the first four bits are used of the number that the 43th character represents, so it can only be a character that represents a number where the two last bits are zero.

    You can make a simpler regular expression to validate this than any base64 string, as you know exactly how long it should be:

    ^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw048]=$