Minor cleanup

This commit is contained in:
Deterous
2023-11-01 20:46:01 +13:00
parent fd18b425d1
commit be713935b6
3 changed files with 129 additions and 130 deletions
+69 -69
View File
@@ -131,75 +131,7 @@ namespace LibIRD
#endregion #endregion
#region Generate Keys #region Helper Functions
/// <summary>
/// Generates Data 1, via AES-128 CBC decryption of a Disc Key
/// </summary>
/// <param name="key">Byte array containing AES encrypted Disc Key</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD1(byte[] key)
{
// Validate key
if (key == null)
throw new ArgumentNullException(nameof(key));
if (key.Length != 16)
throw new ArgumentException("Disc Key must be a byte array of length 16", nameof(key));
// Setup AES decryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D1AesKey;
aes.IV = D1AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES decryption
using MemoryStream stream = new();
using ICryptoTransform dec = aes.CreateDecryptor();
using CryptoStream cs = new(stream, dec, CryptoStreamMode.Write);
cs.Write(key, 0, 16);
cs.FlushFinalBlock();
// Save decrypted key to field
Data1Key = stream.ToArray();
}
/// <summary>
/// Generates Data 2, via AES-128 CBC encryption of a Disc ID
/// </summary>
/// <param name="id">Byte array containing AES decrypted Disc ID</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD2(byte[] id)
{
// Validate id
if (id == null)
throw new ArgumentNullException(nameof(id));
if (id.Length != 16)
throw new ArgumentException("Disc ID must be a byte array of length 16", nameof(id));
// Setup AES encryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D2AesKey;
aes.IV = D2AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES encryption
using MemoryStream stream = new();
using ICryptoTransform enc = aes.CreateEncryptor();
using CryptoStream cs = new(stream, enc, CryptoStreamMode.Write);
cs.Write(id, 0, 16);
cs.FlushFinalBlock();
// Save encrypted key to field
Data2Key = stream.ToArray();
}
#endregion #endregion
@@ -315,6 +247,74 @@ namespace LibIRD
#region Methods #region Methods
/// <summary>
/// Generates Data 1, via AES-128 CBC decryption of a Disc Key
/// </summary>
/// <param name="key">Byte array containing AES encrypted Disc Key</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD1(byte[] key)
{
// Validate key
if (key == null)
throw new ArgumentNullException(nameof(key));
if (key.Length != 16)
throw new ArgumentException("Disc Key must be a byte array of length 16", nameof(key));
// Setup AES decryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D1AesKey;
aes.IV = D1AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES decryption
using MemoryStream stream = new();
using ICryptoTransform dec = aes.CreateDecryptor();
using CryptoStream cs = new(stream, dec, CryptoStreamMode.Write);
cs.Write(key, 0, 16);
cs.FlushFinalBlock();
// Save decrypted key to field
Data1Key = stream.ToArray();
}
/// <summary>
/// Generates Data 2, via AES-128 CBC encryption of a Disc ID
/// </summary>
/// <param name="id">Byte array containing AES decrypted Disc ID</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private protected void GenerateD2(byte[] id)
{
// Validate id
if (id == null)
throw new ArgumentNullException(nameof(id));
if (id.Length != 16)
throw new ArgumentException("Disc ID must be a byte array of length 16", nameof(id));
// Setup AES encryption
using Aes aes = Aes.Create() ?? throw new InvalidOperationException("AES not available. Change your system settings");
// Set AES settings
aes.Key = D2AesKey;
aes.IV = D2AesIV;
aes.Padding = PaddingMode.None;
aes.Mode = CipherMode.CBC;
// Perform AES encryption
using MemoryStream stream = new();
using ICryptoTransform enc = aes.CreateEncryptor();
using CryptoStream cs = new(stream, enc, CryptoStreamMode.Write);
cs.Write(id, 0, 16);
cs.FlushFinalBlock();
// Save encrypted key to field
Data2Key = stream.ToArray();
}
/// <summary> /// <summary>
/// Write IRD data to file /// Write IRD data to file
/// </summary> /// </summary>
+4 -5
View File
@@ -126,7 +126,7 @@ namespace LibIRD
throw new ArgumentNullException(nameof(sfoPath)); throw new ArgumentNullException(nameof(sfoPath));
// Read file as a stream, and parse file // Read file as a stream, and parse file
using (FileStream fs = new FileStream(sfoPath, FileMode.Open, FileAccess.Read)) using FileStream fs = new(sfoPath, FileMode.Open, FileAccess.Read);
Parse(fs); Parse(fs);
} }
@@ -138,8 +138,8 @@ namespace LibIRD
private void Parse(Stream sfoStream) private void Parse(Stream sfoStream)
{ {
// Read binary stream // Read binary stream
using (BinaryReader br = new BinaryReader(sfoStream)) using BinaryReader br = new(sfoStream);
{
// Check file signature is correct // Check file signature is correct
string magic = Encoding.ASCII.GetString(br.ReadBytes(4)); string magic = Encoding.ASCII.GetString(br.ReadBytes(4));
if (magic != ParamSFO.Magic) if (magic != ParamSFO.Magic)
@@ -201,7 +201,6 @@ namespace LibIRD
} }
} }
} }
}
/// <summary> /// <summary>
/// Prints formatted parameters extracted from PARAM.SFO to console /// Prints formatted parameters extracted from PARAM.SFO to console
@@ -209,7 +208,7 @@ namespace LibIRD
public void Print() public void Print()
{ {
// Build string from parameters // Build string from parameters
StringBuilder print = new StringBuilder("PARAM.SFO Contents:\n====================\n"); StringBuilder print = new("PARAM.SFO Contents:\n====================\n");
// Loop through all parameters in PARAM.SFO // Loop through all parameters in PARAM.SFO
for (int i = 0; i < ParamCount; i++) for (int i = 0; i < ParamCount; i++)
+1 -1
View File
@@ -170,7 +170,7 @@ namespace LibIRD
byte[] crc32; byte[] crc32;
using (FileStream fs = File.OpenRead(isoPath)) using (FileStream fs = File.OpenRead(isoPath))
{ {
Crc32 hasher = new Crc32(); Crc32 hasher = new();
hasher.Append(fs); hasher.Append(fs);
crc32 = hasher.GetCurrentHash(); crc32 = hasher.GetCurrentHash();
Array.Reverse(crc32); Array.Reverse(crc32);