问题
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?
回答1:
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]=$
回答2:
I would take the following steps:
- Run the existing RegEx to decide if it's valid base64 or not
- If true, decode from base64 and see if there are 256 bits, or 32 bytes.
- I believe this is a 256 bit number:
ampqampqampqampqampqampqampqampqampqampqamo=
All 256 bit numbers will have the same length when encoded into base64. This means they will all have 43 characters, followed by one = for a total of 44 characters. You should be able to use this as a shortcut to determine of the base64 encoded string represents 256 bits.
- I believe this is a 256 bit number:
来源:https://stackoverflow.com/questions/21151500/validate-base64-encoded-256-bit-numbers-for-digital-signature-keys