Add basic rar5 crypt header

This commit is contained in:
Adam Hathcock
2018-04-28 18:20:40 +01:00
parent 9d63dcb8d6
commit 15534f466a
4 changed files with 65 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
using SharpCompress.IO;
namespace SharpCompress.Common.Rar.Headers
{
internal class CryptHeader : RarHeader
{
private const int CRYPT_VERSION = 0; // Supported encryption version.
private const int SIZE_SALT50 = 16;
private const int SIZE_PSWCHECK = 8;
private const int SIZE_PSWCHECK_CSUM = 4;
private bool UsePswCheck;
private uint Lg2Count; // Log2 of PBKDF2 repetition count.
private byte[] Salt;
private byte[] PswCheck;
public CryptHeader(RarHeader header, RarCrcBinaryReader reader)
: base(header, reader, HeaderType.Crypt)
{
}
protected override void ReadFinish(MarkingBinaryReader reader)
{
var cryptVersion = reader.ReadRarVIntUInt32();
if (cryptVersion > CRYPT_VERSION)
{
//error?
return;
}
UsePswCheck = HasHeaderFlag(EncryptionFlagsV5.CHFL_CRYPT_PSWCHECK);
Lg2Count = reader.ReadRarVIntByte(1);
if (Lg2Count > 0)
{
//error?
return;
}
Salt = reader.ReadBytes(SIZE_SALT50);
if (UsePswCheck)
{
PswCheck = reader.ReadBytes(SIZE_PSWCHECK);
var csum = reader.ReadBytes(SIZE_PSWCHECK_CSUM);
}
}
}
}

View File

@@ -12,7 +12,8 @@ namespace SharpCompress.Common.Rar.Headers
Protect,
Sign,
NewSub,
EndArchive
EndArchive,
Crypt
}
internal static class HeaderCodeV
@@ -40,6 +41,12 @@ namespace SharpCompress.Common.Rar.Headers
public const ushort HasData = 0x8000;
}
internal static class EncryptionFlagsV5
{
// RAR 5.0 archive encryption header specific flags.
public const ushort CHFL_CRYPT_PSWCHECK = 0x0001; // Password check data is present.
}
internal static class HeaderFlagsV5
{
public const ushort HasExtra = 0x0001;

View File

@@ -158,6 +158,12 @@ namespace SharpCompress.Common.Rar.Headers
{
return new EndArchiveHeader(header, reader);
}
case HeaderCodeV.Rar5EncryptionHeader:
{
var ch = new CryptHeader(header, reader);
IsEncrypted = true;
return ch;
}
default:
{
throw new InvalidFormatException("Unknown Rar Header: " + header.HeaderCode);

View File

@@ -51,7 +51,7 @@ namespace SharpCompress.Test.Rar
{
foreach (var header in rarHeaderFactory.ReadHeaders(stream))
{
if (header.HeaderType == HeaderType.Archive)
if (header.HeaderType == HeaderType.Archive || header.HeaderType == HeaderType.Crypt)
{
Assert.Equal(isEncrypted, rarHeaderFactory.IsEncrypted);
break;