Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if string is base64

i may recieve these two strings:

    base = Base64.encode64(File.open("/home/usr/Desktop/test", "rb").read)
    => "YQo=\n"

    string = File.open("/home/usr/Desktop/test", "rb").read
    => "a\n"

what i have tried so far is to check string with regular expression i-e. /([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==$)/ but this would be very heavy if the file is big.

I also have tried base.encoding.name and string.encoding.name but both returns the same.

I have also seen this post and got regular expression solution but any other solution ?

Any idea ? I just want to get is the string is actually text or base64 encoded text....

like image 408
Abdul Baig Avatar asked May 27 '15 08:05

Abdul Baig


People also ask

How do I check if a string is Base64 encoded?

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.

How do you check if a string is Base64 or not online?

How to check whether a string is Base64 encoded or not? Online Base64 decoder can tell you if your string is Base64 encoded by validating it. If Base64 validation returns an error for your string, it is not Base64 encoded or encoding is not done properly.

What is == in Base64?

The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.

How do you check whether a string is Base64 encoded or not in Java?

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.


1 Answers

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.

like image 97
Mirek Rusin Avatar answered Sep 17 '22 16:09

Mirek Rusin