Skip to content

Commit

Permalink
Switch to System.IO.Hashing.Crc32
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Nov 16, 2023
1 parent 414e6e3 commit 51cf073
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO.Hashing;
using System.Net;
using SteamKit2.Internal;

Expand Down Expand Up @@ -166,7 +167,7 @@ void HandleEncryptRequest( IPacketMsg packetMsg )
}
}

var keyCrc = CryptoHelper.CRCHash( encryptedHandshakeBlob );
var keyCrc = Crc32.Hash( encryptedHandshakeBlob );

response.Write( encryptedHandshakeBlob );
response.Write( keyCrc );
Expand Down
1 change: 1 addition & 0 deletions SteamKit2/SteamKit2/SteamKit2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="protobuf-net" Version="3.2.26" />
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion SteamKit2/SteamKit2/Types/DepotManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using System.Text;
using System.Linq;
using System.IO.Hashing;

namespace SteamKit2
{
Expand Down Expand Up @@ -438,7 +439,7 @@ void ParseProtobufManifestMetadata(ContentManifestMetadata metadata)
byte[] data = new byte[ 4 + len ];
Buffer.BlockCopy( BitConverter.GetBytes( len ), 0, data, 0, 4 );
Buffer.BlockCopy( ms_payload.ToArray(), 0, data, 4, len );
uint crc32 = Crc32.Compute( data );
uint crc32 = Crc32.HashToUInt32( data );

if ( FilenamesEncrypted )
{
Expand Down
5 changes: 3 additions & 2 deletions SteamKit2/SteamKit2/Types/GameID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Diagnostics;
using System.IO.Hashing;

namespace SteamKit2
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public GameID( UInt32 nAppID, string modPath )
{
AppID = nAppID;
AppType = GameType.GameMod;
ModID = Crc32.Compute(System.Text.Encoding.UTF8.GetBytes(modPath));
ModID = Crc32.HashToUInt32(System.Text.Encoding.UTF8.GetBytes(modPath));
}
/// <summary>
/// Initializes a new instance of the <see cref="GameID"/> class.
Expand All @@ -92,7 +93,7 @@ public GameID( string exePath, string appName )

AppID = 0;
AppType = GameType.Shortcut;
ModID = Crc32.Compute(System.Text.Encoding.UTF8.GetBytes(combined));
ModID = Crc32.HashToUInt32(System.Text.Encoding.UTF8.GetBytes(combined));
}


Expand Down
114 changes: 0 additions & 114 deletions SteamKit2/SteamKit2/Util/Crc32.cs

This file was deleted.

17 changes: 1 addition & 16 deletions SteamKit2/SteamKit2/Util/CryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Hashing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -399,22 +400,6 @@ public static byte[] SymmetricDecryptECB( byte[] input, byte[] key )
}
}

/// <summary>
/// Performs CRC32 on an input byte array using the CrcStandard.Crc32Bit parameters
/// </summary>
public static byte[] CRCHash( byte[] input )
{
ArgumentNullException.ThrowIfNull( input );

using ( var crc = new Crc32() )
{
byte[] hash = crc.ComputeHash( input );
Array.Reverse( hash );

return hash;
}
}

/// <summary>
/// Performs an Adler32 on the given input
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion SteamKit2/SteamKit2/Util/VZipDeltaUtil.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.IO.Hashing;

namespace SteamKit2
{
Expand Down Expand Up @@ -53,7 +54,7 @@ public static byte[] Decompress( byte[] buffer, byte[] sourceChunkData )
decoder.Code( inputStream, outStream, deltaBuffer.Length, sizeDecompressed, null );

var outData = outStream.ToArray();
if ( Crc32.Compute( outData ) != outputCRC )
if ( Crc32.HashToUInt32( outData ) != outputCRC )
{
throw new InvalidDataException( "CRC does not match decompressed data. VZip data may be corrupted." );
}
Expand Down
5 changes: 3 additions & 2 deletions SteamKit2/SteamKit2/Util/VZipUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Hashing;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -55,7 +56,7 @@ public static byte[] Decompress(byte[] buffer)
decoder.Code(inputStream, outStream, compressedBuffer.Length, sizeDecompressed, null);

var outData = outStream.ToArray();
if (Crc32.Compute(outData) != outputCRC)
if (Crc32.HashToUInt32(outData) != outputCRC)
{
throw new InvalidDataException("CRC does not match decompressed data. VZip data may be corrupted.");
}
Expand All @@ -70,7 +71,7 @@ public static byte[] Compress(byte[] buffer)
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
byte[] crc = CryptoHelper.CRCHash(buffer);
byte[] crc = Crc32.Hash(buffer);

writer.Write(VZipHeader);
writer.Write((byte)Version);
Expand Down
5 changes: 3 additions & 2 deletions SteamKit2/SteamKit2/Util/ZipUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.IO;
using System.IO.Compression;
using System.IO.Hashing;
using System.Text;

namespace SteamKit2
Expand Down Expand Up @@ -61,7 +62,7 @@ public static byte[] Decompress( byte[] buffer )
else
decompressed = compressedBuffer;

uint checkSum = Crc32.Compute( decompressed );
uint checkSum = Crc32.HashToUInt32( decompressed );

if ( checkSum != crc )
{
Expand All @@ -77,7 +78,7 @@ public static byte[] Compress( byte[] buffer )
using ( MemoryStream ms = new MemoryStream() )
using ( BinaryWriter writer = new BinaryWriter( ms ) )
{
uint checkSum = Crc32.Compute( buffer );
uint checkSum = Crc32.HashToUInt32( buffer );

byte[] compressed = DeflateBuffer( buffer );

Expand Down

0 comments on commit 51cf073

Please sign in to comment.