Skip to content

Commit 4371ae1

Browse files
Less allocations in Server.VerifyName
1 parent b108641 commit 4371ae1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

fCraft/System/Server.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,13 +1015,24 @@ internal static bool VerifyName( [NotNull] string name, [NotNull] string hash, [
10151015
if( name == null ) throw new ArgumentNullException( "name" );
10161016
if( hash == null ) throw new ArgumentNullException( "hash" );
10171017
if( salt == null ) throw new ArgumentNullException( "salt" );
1018-
if (hash.Length < 32) hash = hash.PadLeft(32, '0');
1019-
MD5 hasher = MD5.Create();
1020-
StringBuilder sb = new StringBuilder( 32 );
1021-
foreach( byte b in hasher.ComputeHash( Encoding.ASCII.GetBytes( salt + name ) ) ) {
1022-
sb.AppendFormat( "{0:x2}", b );
1018+
if( hash.Length < 32 ) hash = hash.PadLeft( 32, '0' );
1019+
1020+
byte[] md5 = MD5.Create().ComputeHash( Encoding.ASCII.GetBytes( salt + name ) );
1021+
string computedHash = ToHexString( md5 );
1022+
return computedHash.CaselessEquals( hash );
1023+
}
1024+
1025+
static string ToHexString( byte[] array ) {
1026+
char[] hex = new char[array.Length * 2];
1027+
for( int i = 0; i < array.Length; i++ ) {
1028+
int value = array[i];
1029+
int hi = value >> 4, lo = value & 0x0F;
1030+
1031+
// 48 = index of 0, 55 = index of (A - 10).
1032+
hex[i * 2 + 0] = hi < 10 ? (char)(hi + 48) : (char)(hi + 55);
1033+
hex[i * 2 + 1] = lo < 10 ? (char)(lo + 48) : (char)(lo + 55);
10231034
}
1024-
return sb.ToString().CaselessEquals( hash );
1035+
return new String(hex);
10251036
}
10261037

10271038

0 commit comments

Comments
 (0)