ArchiveCryptHeader.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using SharpCompress.IO;
  2. namespace SharpCompress.Common.Rar.Headers
  3. {
  4. internal class ArchiveCryptHeader : RarHeader
  5. {
  6. private const int CRYPT_VERSION = 0; // Supported encryption version.
  7. private const int SIZE_SALT50 = 16;
  8. private const int SIZE_SALT30 = 8;
  9. private const int SIZE_INITV = 16;
  10. private const int SIZE_PSWCHECK = 8;
  11. private const int SIZE_PSWCHECK_CSUM = 4;
  12. private const int CRYPT5_KDF_LG2_COUNT = 15; // LOG2 of PDKDF2 iteration count.
  13. private const int CRYPT5_KDF_LG2_COUNT_MAX = 24; // LOG2 of maximum accepted iteration count.
  14. private bool _usePswCheck;
  15. private uint _lg2Count; // Log2 of PBKDF2 repetition count.
  16. private byte[] _salt;
  17. private byte[] _pswCheck;
  18. private byte[] _pswCheckCsm;
  19. public ArchiveCryptHeader(RarHeader header, RarCrcBinaryReader reader)
  20. : base(header, reader, HeaderType.Crypt)
  21. {
  22. }
  23. protected override void ReadFinish(MarkingBinaryReader reader)
  24. {
  25. var cryptVersion = reader.ReadRarVIntUInt32();
  26. if (cryptVersion > CRYPT_VERSION)
  27. {
  28. //error?
  29. return;
  30. }
  31. var encryptionFlags = reader.ReadRarVIntUInt32();
  32. _usePswCheck = FlagUtility.HasFlag(encryptionFlags, EncryptionFlagsV5.CHFL_CRYPT_PSWCHECK);
  33. _lg2Count = reader.ReadRarVIntByte(1);
  34. //UsePswCheck = HasHeaderFlag(EncryptionFlagsV5.CHFL_CRYPT_PSWCHECK);
  35. if (_lg2Count > CRYPT5_KDF_LG2_COUNT_MAX)
  36. {
  37. //error?
  38. return;
  39. }
  40. _salt = reader.ReadBytes(SIZE_SALT50);
  41. if (_usePswCheck)
  42. {
  43. _pswCheck = reader.ReadBytes(SIZE_PSWCHECK);
  44. _pswCheckCsm = reader.ReadBytes(SIZE_PSWCHECK_CSUM);
  45. }
  46. }
  47. }
  48. }