Search code examples
javascriptperlrar

Determine if rar file is password protected


I'd like to be able to determine if the beginning portion (ideally first MB or so) of a file is of a password protected rar file. I don't just need to know if it's a rar file, I need to ensure that the file is password protected.

Is this possible? I know that the rar format is a proprietary format, but is this possible?

Edit: I'd like to do this by examining the file's content, with either javascript or perl. It should not have access to the rar library.

Edit2: With at least some consistency so far, I have been able to determine that the 10th byte appears to be always set to zero if no encryption is enabled. I haven't done enough testing yet to confirm this works reliably yet, but nonetheless, that is the result I am seeing.


Solution

  • My experiments gave me the following subroutine:

    sub is_rarfile_protected {
       my ($rar_filename) = @_;
       open my $rar_fh, '<', $rar_filename or die $!, "\n";
       sysread $rar_fh, my $mark, 25;
       return ord (substr $mark, -1) & 0b100; 
    }
    

    ... which works for me so far.

    Sadly, I don't have Rar installed, so I cannot check whether it'll work on all the password-encrypted rar-files (including multi-volumes, etc.) or not.