mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-23 07:25:11 +00:00
Add basic rar5 crypt header
This commit is contained in:
50
src/SharpCompress/Common/Rar/Headers/CryptHeader.cs
Normal file
50
src/SharpCompress/Common/Rar/Headers/CryptHeader.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user