1

I have an interesting problem. I'm working on a Perl script that takes in a variety of strings, some base64 encoded, others not.

This data structure is dynamic, generated from a input from third party application. I don't have a ready way to know in advance which of these fields are base64 encoded and those which are not.

The various solutions I have looked at all involved regular expressions on the input data. Unfortunately these all fail if the data is of the same strictly alphanumeric structure.

How can I determine which strings are truly base64 encoded?

Tim Brigham
  • 502
  • 1
  • 5
  • 21
  • Previously answered [RegEx to parse or validate Base64 data](http://stackoverflow.com/questions/475074/regex-to-parse-or-validate-base64-data) question should help. – yko Nov 15 '11 at 18:18

1 Answers1

0

I ended up going with doing a decode on everything to a temporary variable and seeing of the output contains any high octet output.

    $value2=decode_base64( $hash{$key} );
    if( !( $value2 =~ m/[\x7F-\xFF]/ ) )
    {
        print "It appears that $key is base64 encoded.\n";
        $value=$value2;
    }
    else
    {
        print "It appears that $key is not base64 encoded.\n";
    }
Tim Brigham
  • 502
  • 1
  • 5
  • 21