11

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....

Community
  • 1
  • 1
Abdul Baig
  • 3,683
  • 3
  • 21
  • 48
  • 3
    possible duplicate of [How to check whether the string is base64 encoded or not](http://stackoverflow.com/questions/8571501/how-to-check-whether-the-string-is-base64-encoded-or-not) – Guillaume Munsch May 27 '15 at 08:22
  • please read the question again i have updated. i have already seen that post – Abdul Baig May 27 '15 at 08:32
  • 1
    "but this would be very heavy if the file is big." Unfortunately, there is no other way to know. Is this sentence valid English skagpoople? You don't know until you find something in it that tells you it isn't. This is not the same "encoding" that `String#encoding` reports - that is character encoding, not file format that you are interested in. – Amadan May 27 '15 at 08:38

1 Answers1

11

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.

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
Mirek Rusin
  • 18,820
  • 3
  • 43
  • 36