Skip to content

Commit

Permalink
Lint code (dotnet format)
Browse files Browse the repository at this point in the history
  • Loading branch information
savagesteel committed Mar 18, 2022
1 parent d792792 commit 4ffece0
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 247 deletions.
2 changes: 1 addition & 1 deletion source/MpqNameBreaker/GetAccelerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MpqNameBreaker
{
[Cmdlet(VerbsCommon.Get,"Accelerator")]
[Cmdlet(VerbsCommon.Get, "Accelerator")]
[OutputType(typeof(uint))]
public class GetAcceleratorCommand : PSCmdlet
{
Expand Down
10 changes: 5 additions & 5 deletions source/MpqNameBreaker/GetMpqStringHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace MpqNameBreaker
{
[Cmdlet(VerbsCommon.Get,"MpqStringHash")]
[Cmdlet(VerbsCommon.Get, "MpqStringHash")]
[OutputType(typeof(uint))]
public class GetMpqStringHashCommand : PSCmdlet
{
Expand All @@ -23,7 +23,7 @@ public class GetMpqStringHashCommand : PSCmdlet
Position = 1,
ValueFromPipelineByPropertyName = true)]
public HashType Type { get; set; }

// Fields
private HashCalculator _hashCalculator;

Expand All @@ -45,12 +45,12 @@ protected override void ProcessRecord()
// Convert string to uppercase
strUpper = String.ToUpper();
// Get ASCII chars
strBytes = Encoding.ASCII.GetBytes( strUpper );
strBytes = Encoding.ASCII.GetBytes(strUpper);
// Compute hash
hash = _hashCalculator.HashString( strBytes, Type );
hash = _hashCalculator.HashString(strBytes, Type);

// Output hash to console
WriteObject( hash );
WriteObject(hash);
}

// This method will be called once at the end of pipeline execution; if no input is received, this method is not called
Expand Down
68 changes: 34 additions & 34 deletions source/MpqNameBreaker/InvokeMpqNameBreaking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace MpqNameBreaker
{
[Cmdlet(VerbsLifecycle.Invoke,"MpqNameBreaking")]
[Cmdlet(VerbsLifecycle.Invoke, "MpqNameBreaking")]
[OutputType(typeof(string))]
public class InvokeMpqNameBreakingCommand : PSCmdlet
{
Expand Down Expand Up @@ -93,15 +93,15 @@ protected override void ProcessRecord()
uint prefixSeed1A, prefixSeed2A, prefixSeed1B, prefixSeed2B;

// Initialize brute force name generator
_bruteForce = new BruteForce( Prefix, Suffix );
_bruteForce = new BruteForce(Prefix, Suffix);
_bruteForce.Initialize();

// Initialize classic CPU hash calculator and pre-calculate prefix seeds
_hashCalculator = new HashCalculator();
if( Prefix.Length > 0 )
if (Prefix.Length > 0)
{
(prefixSeed1A, prefixSeed2A) = _hashCalculator.HashStringOptimizedCalculateSeeds( _bruteForce.PrefixBytes, HashType.MpqHashNameA );
(prefixSeed1B, prefixSeed2B) = _hashCalculator.HashStringOptimizedCalculateSeeds( _bruteForce.PrefixBytes, HashType.MpqHashNameB );
(prefixSeed1A, prefixSeed2A) = _hashCalculator.HashStringOptimizedCalculateSeeds(_bruteForce.PrefixBytes, HashType.MpqHashNameA);
(prefixSeed1B, prefixSeed2B) = _hashCalculator.HashStringOptimizedCalculateSeeds(_bruteForce.PrefixBytes, HashType.MpqHashNameB);
}
else
{
Expand All @@ -116,24 +116,24 @@ protected override void ProcessRecord()
PrintDeviceInfo(_hashCalculatorAccelerated);

// Define the batch size to MaxNumThreads of the accelerator if no custom value has been provided
if( !this.MyInvocation.BoundParameters.ContainsKey("BatchSize") )
if (!this.MyInvocation.BoundParameters.ContainsKey("BatchSize"))
BatchSize = _hashCalculatorAccelerated.Accelerator.MaxNumThreads;

if( !this.MyInvocation.BoundParameters.ContainsKey("BatchCharCount") )
if (!this.MyInvocation.BoundParameters.ContainsKey("BatchCharCount"))
{
if( _hashCalculatorAccelerated.Accelerator.MaxNumThreads < 1024 )
if (_hashCalculatorAccelerated.Accelerator.MaxNumThreads < 1024)
BatchCharCount = 3;
else
BatchCharCount = 4;
}

// Initialize brute force batches name generator
_bruteForceBatches = new BruteForceBatches( BatchSize, BatchCharCount, AdditionalChars, Charset );
_bruteForceBatches = new BruteForceBatches(BatchSize, BatchCharCount, AdditionalChars, Charset);
_bruteForceBatches.Initialize();

// Load kernel (GPU function)
// This function will calculate HashA for each name of a batch and report matches/collisions
var kernel = _hashCalculatorAccelerated.Accelerator.LoadAutoGroupedStreamKernel<
var kernel = _hashCalculatorAccelerated.Accelerator.LoadAutoGroupedStreamKernel<
Index1D,
ArrayView<byte>,
ArrayView<uint>,
Expand All @@ -149,20 +149,20 @@ protected override void ProcessRecord()
int,
int,
ArrayView<int>
>( Mpq.HashCalculatorAccelerated.HashStringsBatchOptimized );
>(Mpq.HashCalculatorAccelerated.HashStringsBatchOptimized);

// Prepare data for the kernel
var charsetBuffer = _hashCalculatorAccelerated.Accelerator.Allocate1D( _bruteForceBatches.CharsetBytes );
var charsetBuffer = _hashCalculatorAccelerated.Accelerator.Allocate1D(_bruteForceBatches.CharsetBytes);

var charsetIndexesBuffer = _hashCalculatorAccelerated.Accelerator.Allocate2DDenseX<int>( new Index2D(BatchSize, BruteForceBatches.MaxGeneratedChars) );
var charsetIndexesBuffer = _hashCalculatorAccelerated.Accelerator.Allocate2DDenseX<int>(new Index2D(BatchSize, BruteForceBatches.MaxGeneratedChars));

// Suffix processing
int suffixLength;
byte[] suffixBytes;
if( Suffix.Length > 0 )
if (Suffix.Length > 0)
{
suffixLength = Suffix.Length;
suffixBytes = Encoding.ASCII.GetBytes( Suffix.ToUpper() );
suffixBytes = Encoding.ASCII.GetBytes(Suffix.ToUpper());
}
else
{
Expand All @@ -174,11 +174,11 @@ protected override void ProcessRecord()

var cryptTableBuffer = _hashCalculatorAccelerated.Accelerator.Allocate1D(_hashCalculatorAccelerated.CryptTable);

int nameCount = (int)Math.Pow( _bruteForceBatches.Charset.Length, BatchCharCount );
int nameCount = (int)Math.Pow(_bruteForceBatches.Charset.Length, BatchCharCount);

// fill result array with -1
int[] foundNameCharsetIndexes = new int[BruteForceBatches.MaxGeneratedChars];
for( int i = 0; i < BruteForceBatches.MaxGeneratedChars; ++i )
int[] foundNameCharsetIndexes = new int[BruteForceBatches.MaxGeneratedChars];
for (int i = 0; i < BruteForceBatches.MaxGeneratedChars; ++i)
foundNameCharsetIndexes[i] = -1;

var foundNameCharsetIndexesBuffer = _hashCalculatorAccelerated.Accelerator.Allocate1D(foundNameCharsetIndexes);
Expand All @@ -196,9 +196,9 @@ protected override void ProcessRecord()

double billionCount = 0;
double tempCount = 0;
double oneBatchBillionCount = ( Math.Pow(_bruteForceBatches.Charset.Length, BatchCharCount) * BatchSize ) / 1_000_000_000;
double oneBatchBillionCount = (Math.Pow(_bruteForceBatches.Charset.Length, BatchCharCount) * BatchSize) / 1_000_000_000;

while ( _bruteForceBatches.NextBatch() )
while (_bruteForceBatches.NextBatch())
{
// Copy char indexes to buffer
charsetIndexesBuffer.CopyFromCPU(_bruteForceBatches.BatchNameSeedCharsetIndexes);
Expand Down Expand Up @@ -226,50 +226,50 @@ protected override void ProcessRecord()
// If name was found
foundNameCharsetIndexes = foundNameCharsetIndexesBuffer.GetAsArray1D();

if( foundNameCharsetIndexes[0] != -1 )
if (foundNameCharsetIndexes[0] != -1)
{
byte[] str = new byte[BruteForceBatches.MaxGeneratedChars];

for( int i = 0; i < BruteForceBatches.MaxGeneratedChars; ++i )
for (int i = 0; i < BruteForceBatches.MaxGeneratedChars; ++i)
{
int idx = foundNameCharsetIndexes[i];
if( idx == -1 )
int idx = foundNameCharsetIndexes[i];

if (idx == -1)
break;

foundName += Convert.ToChar( _bruteForceBatches.CharsetBytes[ idx ] );
foundName += Convert.ToChar(_bruteForceBatches.CharsetBytes[idx]);
}

WriteVerbose($"End: {DateTime.Now:HH:mm:ss.fff}");
TimeSpan elapsed = DateTime.Now - start;
WriteVerbose($"Elapsed: {elapsed}");
WriteVerbose("Name found! ");
WriteObject( Prefix.ToUpper() + foundName + Suffix.ToUpper() );
WriteObject(Prefix.ToUpper() + foundName + Suffix.ToUpper());

return;
}

// Display statistics
billionCount += oneBatchBillionCount;
if( tempCount < billionCount )
if (tempCount < billionCount)
{
tempCount = billionCount + 1;
TimeSpan elapsed = DateTime.Now - start;

string lastName = "";
for( int i = 0; i < BruteForceBatches.MaxGeneratedChars; i++ )
for (int i = 0; i < BruteForceBatches.MaxGeneratedChars; i++)
{
int idx = _bruteForceBatches.BatchNameSeedCharsetIndexes[ BatchSize-1, i ];
int idx = _bruteForceBatches.BatchNameSeedCharsetIndexes[BatchSize - 1, i];

if( idx == -1 )
if (idx == -1)
break;

lastName += Convert.ToChar( _bruteForceBatches.CharsetBytes[ idx ] );
lastName += Convert.ToChar(_bruteForceBatches.CharsetBytes[idx]);
}

WriteVerbose($"Elapsed time: {elapsed} - Name: {Prefix.ToUpper() + lastName + Suffix.ToUpper()} - Name count: {billionCount:N0} billion");
}
}

}

}
Expand Down
34 changes: 17 additions & 17 deletions source/MpqNameBreaker/InvokeMpqNameBreakingNonAccelerated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace MpqNameBreaker
{
[Cmdlet(VerbsLifecycle.Invoke,"MpqNameBreakingNonAccelerated")]
[Cmdlet(VerbsLifecycle.Invoke, "MpqNameBreakingNonAccelerated")]
[OutputType(typeof(string))]
public class InvokeMpqNameBreakingNonAcceleratedCommand : PSCmdlet
{
Expand Down Expand Up @@ -55,48 +55,48 @@ protected override void ProcessRecord()
DateTime start = DateTime.Now;

// Initialize brute force name generator
_bruteForce = new BruteForce( Prefix, Suffix );
_bruteForce = new BruteForce(Prefix, Suffix);
_bruteForce.Initialize();

// Initialize hash calculator
_hashCalculator = new HashCalculator();
// Prepare prefix seeds to speed up calculation
(prefixSeed1A, prefixSeed2A) = _hashCalculator.HashStringOptimizedCalculateSeeds( _bruteForce.PrefixBytes, HashType.MpqHashNameA );
(prefixSeed1B, prefixSeed2B) = _hashCalculator.HashStringOptimizedCalculateSeeds( _bruteForce.PrefixBytes, HashType.MpqHashNameB );
(prefixSeed1A, prefixSeed2A) = _hashCalculator.HashStringOptimizedCalculateSeeds(_bruteForce.PrefixBytes, HashType.MpqHashNameA);
(prefixSeed1B, prefixSeed2B) = _hashCalculator.HashStringOptimizedCalculateSeeds(_bruteForce.PrefixBytes, HashType.MpqHashNameB);


WriteVerbose( DateTime.Now.ToString("HH:mm:ss.fff"));
WriteVerbose(DateTime.Now.ToString("HH:mm:ss.fff"));

long count = 0;
// 38^8 = 4_347_792_138_496
while( _bruteForce.NextName() && count < 4_347_792_138_496 )
while (_bruteForce.NextName() && count < 4_347_792_138_496)
{
//currentHash = _hashCalculator.HashString( _bruteForce.NameBytes, Type );
currentHashA = _hashCalculator.HashStringOptimized( _bruteForce.NameBytes, HashType.MpqHashNameA, _bruteForce.Prefix.Length, prefixSeed1A, prefixSeed2A );
currentHashA = _hashCalculator.HashStringOptimized(_bruteForce.NameBytes, HashType.MpqHashNameA, _bruteForce.Prefix.Length, prefixSeed1A, prefixSeed2A);

if( HashA == currentHashA )
if (HashA == currentHashA)
{
currentHashB = _hashCalculator.HashStringOptimized( _bruteForce.NameBytes, HashType.MpqHashNameB, _bruteForce.Prefix.Length, prefixSeed1B, prefixSeed2B );
currentHashB = _hashCalculator.HashStringOptimized(_bruteForce.NameBytes, HashType.MpqHashNameB, _bruteForce.Prefix.Length, prefixSeed1B, prefixSeed2B);

// Detect collisions
if( HashB == currentHashB )
if (HashB == currentHashB)
{
WriteObject( "Name found: " + _bruteForce.Name );
WriteVerbose( DateTime.Now.ToString("HH:mm:ss.fff"));
WriteObject("Name found: " + _bruteForce.Name);
WriteVerbose(DateTime.Now.ToString("HH:mm:ss.fff"));
break;
}
else
{
WriteWarning( "Hash A collision found on name: " + _bruteForce.Name );
WriteWarning("Hash A collision found on name: " + _bruteForce.Name);
}
}
if( count % 1_000_000_000 == 0 )

if (count % 1_000_000_000 == 0)
{
TimeSpan elapsed = DateTime.Now - start;
WriteVerbose( String.Format("Time: {0} - Name: {1} - Count : {2:N0} billion", elapsed.ToString(), _bruteForce.Name, count/1_000_000_000) );
WriteVerbose(String.Format("Time: {0} - Name: {1} - Count : {2:N0} billion", elapsed.ToString(), _bruteForce.Name, count / 1_000_000_000));
}

count++;
}
}
Expand Down
22 changes: 11 additions & 11 deletions source/MpqNameBreaker/Mpq/HashCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HashCalculator
const uint HashSeed2 = 0xEEEEEEEE;

// Properties
public uint[] CryptTable {get; private set;}
public uint[] CryptTable { get; private set; }

// Fields

Expand All @@ -37,32 +37,32 @@ public void InitializeCryptTable()
CryptTable = new uint[CryptTableSize];

// Go through all the cells of the array
for( index1 = 0; index1 < 0x100; index1++ )
for (index1 = 0; index1 < 0x100; index1++)
{
for( index2 = index1, i = 0; i < 5; i++, index2 += 0x100 )
for (index2 = index1, i = 0; i < 5; i++, index2 += 0x100)
{
uint temp1, temp2;

seed = (seed * 125 + 3) % 0x2AAAAB;
temp1 = (seed & 0xFFFF) << 0x10;
temp1 = (seed & 0xFFFF) << 0x10;

seed = (seed * 125 + 3) % 0x2AAAAB;
temp2 = (seed & 0xFFFF);
temp2 = (seed & 0xFFFF);

CryptTable[index2] = (temp1 | temp2);
}
}
}

public uint HashString( byte[] str, HashType hashType )
public uint HashString(byte[] str, HashType hashType)
{
uint seed1 = HashSeed1;
uint seed2 = HashSeed2;
uint ch;

int type = (int)hashType;

for( int i = 0; i < str.Length; i++ )
for (int i = 0; i < str.Length; i++)
{
ch = str[i];
seed1 = CryptTable[type + ch] ^ (seed1 + seed2);
Expand All @@ -73,15 +73,15 @@ public uint HashString( byte[] str, HashType hashType )
}


public (uint, uint) HashStringOptimizedCalculateSeeds( byte[] str, HashType hashType )
public (uint, uint) HashStringOptimizedCalculateSeeds(byte[] str, HashType hashType)
{
uint seed1 = HashSeed1;
uint seed2 = HashSeed2;
uint ch;

int type = (int)hashType;

for( int i = 0; i < str.Length; i++ )
for (int i = 0; i < str.Length; i++)
{
ch = str[i];
seed1 = CryptTable[type + ch] ^ (seed1 + seed2);
Expand All @@ -92,13 +92,13 @@ public uint HashString( byte[] str, HashType hashType )
}

// Call HashStringOptimizedCalculateSeeds before this method
public uint HashStringOptimized( byte[] str, HashType hashType, int prefixLength, uint seed1, uint seed2 )
public uint HashStringOptimized(byte[] str, HashType hashType, int prefixLength, uint seed1, uint seed2)
{
uint ch;

int type = (int)hashType;

for( int i = prefixLength; i < str.Length; i++ )
for (int i = prefixLength; i < str.Length; i++)
{
ch = str[i];
seed1 = CryptTable[type + ch] ^ (seed1 + seed2);
Expand Down
Loading

1 comment on commit 4ffece0

@heinermann
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Please sign in to comment.