From 8fec42d2431ebad66176afd96904205173631948 Mon Sep 17 00:00:00 2001 From: Deterous <138427222+Deterous@users.noreply.github.com> Date: Sun, 29 Oct 2023 13:03:56 +1300 Subject: [PATCH] Cleanup --- LibIRD/IRD.cs | 69 ++++++++++++++++++++++++++++++------------------- LibIRD/ReIRD.cs | 18 +++++-------- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/LibIRD/IRD.cs b/LibIRD/IRD.cs index 54fbb8f..96064bb 100644 --- a/LibIRD/IRD.cs +++ b/LibIRD/IRD.cs @@ -15,23 +15,35 @@ namespace LibIRD #region Constants /// - /// "3IRD" + /// IRD file signature /// - /// - public static readonly byte[] Magic = new byte[] { 0x33, 0x49, 0x52, 0x44 }; + /// "3IRD" + private static readonly byte[] Magic = new byte[] { 0x33, 0x49, 0x52, 0x44 }; /// /// MD5 hash of null /// - public static readonly byte[] NullMD5 = new byte[] {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}; + private static readonly byte[] NullMD5 = new byte[] {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}; /// - /// AES encryption/decryption keys and initial values + /// AES CBC Encryption Key for Data 1 (Disc Key) /// - public static readonly byte[] D1AesKey = { 0x38, 0x0B, 0xCF, 0x0B, 0x53, 0x45, 0x5B, 0x3C, 0x78, 0x17, 0xAB, 0x4F, 0xA3, 0xBA, 0x90, 0xED }; - public static readonly byte[] D1AesIV = { 0x69, 0x47, 0x47, 0x72, 0xAF, 0x6F, 0xDA, 0xB3, 0x42, 0x74, 0x3A, 0xEF, 0xAA, 0x18, 0x62, 0x87 }; - public static readonly byte[] D2AesKey = { 0x7C, 0xDD, 0x0E, 0x02, 0x07, 0x6E, 0xFE, 0x45, 0x99, 0xB1, 0xB8, 0x2C, 0x35, 0x99, 0x19, 0xB3 }; - public static readonly byte[] D2AesIV = { 0x22, 0x26, 0x92, 0x8D, 0x44, 0x03, 0x2F, 0x43, 0x6A, 0xFD, 0x26, 0x7E, 0x74, 0x8B, 0x23, 0x93 }; + private static readonly byte[] D1AesKey = { 0x38, 0x0B, 0xCF, 0x0B, 0x53, 0x45, 0x5B, 0x3C, 0x78, 0x17, 0xAB, 0x4F, 0xA3, 0xBA, 0x90, 0xED }; + + /// + /// AES CBC Initial Value for Data 1 (Disc Key) + /// + private static readonly byte[] D1AesIV = { 0x69, 0x47, 0x47, 0x72, 0xAF, 0x6F, 0xDA, 0xB3, 0x42, 0x74, 0x3A, 0xEF, 0xAA, 0x18, 0x62, 0x87 }; + + /// + /// AES CBC Encryption Key for Data 2 (Disc ID) + /// + private static readonly byte[] D2AesKey = { 0x7C, 0xDD, 0x0E, 0x02, 0x07, 0x6E, 0xFE, 0x45, 0x99, 0xB1, 0xB8, 0x2C, 0x35, 0x99, 0x19, 0xB3 }; + + /// + /// AES CBC Initial Value for Data 2 (Disc ID) + /// + private static readonly byte[] D2AesIV = { 0x22, 0x26, 0x92, 0x8D, 0x44, 0x03, 0x2F, 0x43, 0x6A, 0xFD, 0x26, 0x7E, 0x74, 0x8B, 0x23, 0x93 }; #endregion @@ -50,7 +62,7 @@ namespace LibIRD _version = value; } } - public byte _version = 9; // Default to latest IRD version = 9 + private byte _version = 9; // Default to latest IRD version = 9 /// /// Unique Identifier @@ -83,7 +95,7 @@ namespace LibIRD /// /// Uncompressed PIC data /// - /// 115 bytes, before D1/D2 keys on version 9 + /// 115 bytes public byte[] PIC { get; set; } #endregion @@ -183,7 +195,7 @@ namespace LibIRD /// Byte array containing AES encrypted Disc Key /// /// - protected void GenerateD1(byte[] key) + private protected void GenerateD1(byte[] key) { // Validate key if (key == null || key.Length <= 0) @@ -224,7 +236,7 @@ namespace LibIRD /// Byte array containing AES decrypted Disc ID /// /// - protected void GenerateD2(byte[] id) + private protected void GenerateD2(byte[] id) { // Validate id if (id == null || id.Length <= 0) @@ -276,7 +288,7 @@ namespace LibIRD /// /// Constructor that reads an existing IRD file and stores its fields in the IRD object /// - /// Full path to IRD file to read data from + /// Path to IRD file to read data from public IRD(string irdPath) { Read(irdPath); @@ -285,13 +297,13 @@ namespace LibIRD /// /// Public Constructor must be called with required fields /// - /// Full path to the disc drive / mounted ISO + /// Path to the ISO /// Disc Key, byte array of length 16 /// Disc ID, byte array of length 16 - /// Disc PIC, bye array of length 115 + /// Disc PIC, byte array of length 115 public IRD(string discPath, byte[] discKey, byte[] discID, byte[] discPIC) { - // Store IRD fields that cannot be determined from the disc drive or mounted ISO + // Store IRD fields that cannot be determined from the ISO GenerateD1(discKey); GenerateD2(discID); PIC = discPIC; @@ -307,13 +319,13 @@ namespace LibIRD /// /// Generate IRD fields from a disc drive or mounted ISO /// - /// Full path to the disc drive / mounted ISO + /// Path to the ISO /// - public void Generate(string discPath) + private protected void Generate(string isoPath) { - // TODO: Check that discPath is a valid disc drive/ISO - if (discPath == null) - throw new ArgumentNullException(nameof(discPath)); + // Validate ISO path + if (isoPath == null || isoPath.Length <= 0) + throw new ArgumentNullException(nameof(isoPath)); // TODO: Code the difficult part (remove these initial values) TitleID = "ABCD12345"; @@ -341,7 +353,7 @@ namespace LibIRD /// /// Read IRD data from file, store fields into class /// - /// Full path to IRD file to read data from + /// Path to IRD file to read data from /// public void Read(string irdPath) { @@ -375,12 +387,15 @@ namespace LibIRD /// /// Write IRD data to file /// - /// Full path to IRD file to be written to - public void Write(string filePath) + /// Path to IRD file to be written to + public void Write(string irdPath) { - // TODO: Check filePath is a valid path for a new IRD file + // Validate irdPath + if (irdPath == null || irdPath.Length <= 0) + throw new ArgumentNullException(nameof(irdPath)); + // Create the IRD file stream - using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) + using (FileStream fs = new FileStream(irdPath, FileMode.Create, FileAccess.Write)) { // Create a GZipped IRD file stream using (GZipStream outStream = new GZipStream(fs, CompressionLevel.Optimal)) diff --git a/LibIRD/ReIRD.cs b/LibIRD/ReIRD.cs index 334f834..1c890ac 100644 --- a/LibIRD/ReIRD.cs +++ b/LibIRD/ReIRD.cs @@ -163,9 +163,7 @@ namespace LibIRD /// /// Generates the UID field by computing the CRC32 hash of the ISO /// - /// Full path to the ISO - /// - /// + /// Path to the ISO private void GenerateUID(string isoPath) { // Compute CRC32 hash @@ -178,12 +176,6 @@ namespace LibIRD Array.Reverse(crc32); } - // Validate CRC32 - if (crc32 == null) - throw new ArgumentNullException(nameof(crc32)); - if (crc32.Length != 4) - throw new ArgumentException("CRC32 hash must be an byte array of length 4", nameof(crc32)); - // Redump ISO CRC32 hash is used as the Unique ID in the reproducible IRD UID = BitConverter.ToUInt32(crc32, 0); } @@ -195,7 +187,7 @@ namespace LibIRD /// /// Constructor with required redump-style IRD fields /// - /// Full path to the ISO + /// Path to the ISO /// Disc Key, redump-style (AES encrypted Data 1) /// /// @@ -232,12 +224,14 @@ namespace LibIRD /// /// Constructor with additional region to generate a specific Disc ID /// - /// Full path to the ISO + /// Path to the ISO /// Disc Key, redump-style (AES encrypted Data 1) /// Disc Region + /// + /// public ReIRD(string isoPath, byte[] key, Region region) { - // Check path exists + // Validate ISO path if (isoPath == null || isoPath.Length <= 0) throw new ArgumentNullException(nameof(isoPath));