@@ -26,31 +26,29 @@ public static uint SHA256Hash32_Lowercase_RT(string strVal, string strSeed = "53
2626 ArgumentNullException . ThrowIfNullOrEmpty ( strVal ) ;
2727 ArgumentNullException . ThrowIfNullOrEmpty ( strSeed ) ;
2828
29- uint hashVal = 5381 ;
29+ var buf = new byte [ 512 ] ;
30+
31+ var sha256Context = SHA256 . Create ( ) ;
3032
3133 // Lower the input value;
3234 strVal = strVal . ToLower ( ) ;
3335 // Lower the input value;
34- strSeed = strVal . ToLower ( ) ;
35-
36- // Length is the strVal + 1 (null terminator)
37- var lenVal = strVal . Length + 1 ;
36+ strSeed = strSeed . ToLower ( ) ;
3837
39- // Add the str into a new buffer with the new size (null terminator)
40- var bufferOfStrVal = new byte [ lenVal ] ;
41- for ( var iBufSize = 0 ; iBufSize < lenVal ; iBufSize ++ )
42- bufferOfStrVal [ iBufSize ] = ( byte ) strVal [ iBufSize ] ;
38+ // Length is the strVal
39+ var lenVal = strVal . Length ;
40+ buf = Resize ( buf , lenVal + 1 ) ; // +1 for the \0 terminator. probably not actually required...
4341
44- // Hash the buffer data once against the buf[0] size * lenVal
45- var bytes = new byte [ 256 ] ;
46- var r = SHA256 . HashData ( new MemoryStream ( Encoding . UTF8 . GetBytes ( strVal ) ) , new Span < byte > ( bytes ) ) ;
47- var r2 = SHA256 . HashData ( bufferOfStrVal ) ;
48- hashVal = ( uint ) BitConverter . ToUInt64 ( r2 , 0 ) ;
42+ // Hash the buffer data once against the buf
43+ buf = sha256Context . ComputeHash ( buf , 0 , 1 * lenVal ) ;
4944
5045 // Hash the buffer data again against the buf[0] size * strSeed.Length
51- // TODO: ^^^^^
46+ var lenSeed = strSeed . Length ;
47+ buf = Resize ( buf , lenSeed ) ;
48+ buf = sha256Context . ComputeHash ( buf , 0 , 1 * lenSeed ) ;
5249
5350 // Finalize the compute
51+ uint hashVal = ( uint ) BitConverter . ToUInt64 ( buf ) ;
5452
5553 // Return the hash as a BigEndian hashed value + 28
5654 return ToBigEndian ( ( hashVal + 28 ) ) ;
@@ -63,6 +61,15 @@ private static uint ToBigEndian(uint littleEndian)
6361 spanage . Reverse ( ) ;
6462 return BitConverter . ToUInt32 ( spanage ) ;
6563 }
64+
65+ private static byte [ ] Resize ( byte [ ] bytes , int length )
66+ {
67+ var buf = new byte [ length ] ;
68+ for ( var iBufSize = 0 ; iBufSize < length && iBufSize < bytes . Length ; iBufSize ++ )
69+ buf [ iBufSize ] = bytes [ iBufSize ] ;
70+
71+ return buf ;
72+ }
6673 //{
6774 // using fixed_string_buf = eastl::fixed_string<char, 512>;
6875 // fixed_string_buf buf;
0 commit comments