adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.22k stars 476 forks source link

Rar5: IArchiveEntryExtensions WriteTo does not write to stream in 0.33 (RarArchive) #755

Closed olegfeferman closed 10 months ago

olegfeferman commented 10 months ago

Hello,

I am trying to write a piece of code that verifies if user entered a correct password in case archive was password protected like this:

        using (var archive = RarArchive.Open(archiveFile, new ReaderOptions() { Password = pass }))
        {
            var entry = archive.Entries.FirstOrDefault(e => !e.IsDirectory);

            using (var stream = new MemoryStream( (int) entry.Size))
            {                    
                entry.WriteTo(stream);                                        
                if (stream.Length == 0)
                {
                    ShowMessage("You have entered incorrect password.");
                }
            }
        }

Problem is stream is always zero. Either it is a bug or I am doing something wrong.

Erior commented 10 months ago

What does entry.Size say for the difference scenarios you test?

olegfeferman commented 10 months ago

It reports correct size in bytes of that entry

Erior commented 10 months ago

I don't think you actually know if the password is correct until you try to decompress, that would give you a size, however it would be the wrong size / CRC, comparing the result with entry.Size would make more sense. Btw, when I try this with encrypted header info, it throw an exception, you might need to catch that if you have such files.

I tested your code snippet as a test with the archive "Rar.encrypted_filesOnly.rar". At least with that encrypted test file I got stream.Length

olegfeferman commented 10 months ago

Thank you for the feedback. When I test with Rar.encrypted_filesAndHeader.rar or Rar.encrypted_filesOnly.rar it works OK (in first case I get exception in second stream.Length is greater than 0). What I did not realize before you sent your message is that I am only having problem with Rar5.encrypted_filesAndHeader.rar and Rar5.encrypted_filesOnly.rar. Actually before I was just testing with a test archive that I created with latest version of Winrar (6.23).

adamhathcock commented 10 months ago

RAR5 decryption is a known TODO: https://github.com/adamhathcock/sharpcompress#todos-always-lots

Dup of https://github.com/adamhathcock/sharpcompress/issues/372

olegfeferman commented 10 months ago

I didn't know. Thank you!