Check If String Is Base64

How to check whether a string is Base64 encoded or not

You can use the following regular expression to check if a string constitutes a valid base64 encoding:

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

In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /]. If the rest length is less than 4, the string is padded with '=' characters.

^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 groups.

([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$ means the string ends in one of three forms: [A-Za-z0-9+/]{4}, [A-Za-z0-9+/]{3}= or [A-Za-z0-9+/]{2}==.

Determine if string is in base64 using JavaScript

If "valid" means "only has base64 chars in it" then check against /[A-Za-z0-9+/=]/.

If "valid" means a "legal" base64-encoded string then you should check for the = at the end.

If "valid" means it's something reasonable after decoding then it requires domain knowledge.

How to check whether a string is base64 encoded or not?

If you receive the exact value by <img src="..." /> attribute then it should have Data URL format

The simple regexp could determine whether the URL is Data or regular. In java it can look like

    private static final Pattern DATA_URL_PATTERN = Pattern.compile("^data:image/(.+?);base64,\\s*", Pattern.CASE_INSENSITIVE);

static void handleImgSrc(String path) {
if (path.startsWith("data:")) {
final Matcher m = DATA_URL_PATTERN.matcher(path);
if (m.find()) {
String imageType = m.group(1);
String base64 = path.substring(m.end());
// decodeImage(imageType, base64);
} else {
// some logging
}
} else {
// downloadImage(path);
}
}

check if string is base64

You can use something like this, not very performant but you are guaranteed not to get false positives:

require 'base64'

def base64?(value)
value.is_a?(String) && Base64.strict_encode64(Base64.decode64(value)) == value
end

The use of strict_encode64 versus encode64 prevents Ruby from inadvertently inserting newlines if you have a long string. See this post for details.

How to check if a string is plaintext or base64 format in Node.js

Encoding is byte level.
If you're dealing in strings then all you can do is to guess or keep meta data information with your string to identify

But you can check these libraries out:

  1. https://www.npmjs.com/package/detect-encoding
  2. https://github.com/mooz/node-icu-charset-detector

How to check for a valid Base64 encoded string

Update: For newer versions of C#, there's a much better alternative, please refer to the answer by Tomas here: https://stackoverflow.com/a/54143400/125981.


It's pretty easy to recognize a Base64 string, as it will only be composed of characters 'A'..'Z', 'a'..'z', '0'..'9', '+', '/' and it is often padded at the end with up to three '=', to make the length a multiple of 4. But instead of comparing these, you'd be better off ignoring the exception, if it occurs.

Check if a string is encoded in base64 using Python

This isn't possible. The best you could do would be to verify that a string might be valid Base 64, although many strings consisting of only ASCII text can be decoded as if they were Base 64.



Related Topics



Leave a reply



Submit