@@ -1212,6 +1212,8 @@ protected void CloneHashState(HashPhpResource clone)
1212
1212
private byte [ ] buffer = null ;
1213
1213
private int bufferUsage = 0 ;
1214
1214
1215
+ internal delegate void ProcessBlockedDelegate ( HashPhpResource self , ReadOnlySpan < byte > data ) ;
1216
+
1215
1217
/// <summary>
1216
1218
/// Returns blocks of data, using buffered data stored before.
1217
1219
/// Provided data can be too small to fit the block, so they are buffered and processed when more data comes.
@@ -1220,7 +1222,7 @@ protected void CloneHashState(HashPhpResource clone)
1220
1222
/// <param name="blockSize">Block size, when buffered data fits this, they are returned.</param>
1221
1223
/// <param name="callback">Called for each block.</param>
1222
1224
/// <returns>Packs of block, as a pair of byte array and index of first element.</returns>
1223
- internal void ProcessBlocked ( ReadOnlySpan < byte > /*!*/ newData , int blockSize , Action < byte [ ] , int > callback )
1225
+ internal void ProcessBlocked ( ReadOnlySpan < byte > /*!*/ newData , int blockSize , ProcessBlockedDelegate callback )
1224
1226
{
1225
1227
Debug . Assert ( blockSize > 0 ) ;
1226
1228
@@ -1242,7 +1244,7 @@ internal void ProcessBlocked(ReadOnlySpan<byte>/*!*/newData, int blockSize, Acti
1242
1244
}
1243
1245
1244
1246
newData . Slice ( 0 , bytesToFitBuffer ) . CopyTo ( buffer . AsSpan ( bufferUsage , bytesToFitBuffer ) ) ;
1245
- callback ( buffer , 0 ) ; // use the data from buffer
1247
+ callback ( this , buffer ) ; // use the data from buffer
1246
1248
1247
1249
bufferUsage = 0 ; // buffer is empty now
1248
1250
index += bytesToFitBuffer ; // part of newData was used
@@ -1251,7 +1253,7 @@ internal void ProcessBlocked(ReadOnlySpan<byte>/*!*/newData, int blockSize, Acti
1251
1253
// returns blocks from the newData
1252
1254
while ( index + blockSize <= newData . Length )
1253
1255
{
1254
- callback ( newData . ToArray ( ) , index ) ;
1256
+ callback ( this , newData . Slice ( index ) ) ;
1255
1257
index += blockSize ;
1256
1258
}
1257
1259
@@ -1728,16 +1730,16 @@ public override void Init()
1728
1730
Array . Clear ( this . checksum , 0 , this . checksum . Length ) ;
1729
1731
}
1730
1732
1731
- private void TransformBlock ( byte [ ] /*!*/ /*byte[16+startIndex]*/ block , int startIndex )
1733
+ private void TransformBlock ( ReadOnlySpan < byte > /*!*/ /*byte[16+startIndex]*/ block )
1732
1734
{
1733
1735
Debug . Assert ( block != null ) ;
1734
- Debug . Assert ( block . Length >= 16 + startIndex ) ;
1736
+ Debug . Assert ( block . Length >= 16 ) ;
1735
1737
1736
1738
byte i , j , t = 0 ;
1737
1739
1738
1740
for ( i = 0 ; i < 16 ; i ++ )
1739
1741
{
1740
- state [ 16 + i ] = block [ i + startIndex ] ;
1742
+ state [ 16 + i ] = block [ i ] ;
1741
1743
state [ 32 + i ] = ( byte ) ( state [ 16 + i ] ^ state [ i ] ) ;
1742
1744
}
1743
1745
@@ -1757,15 +1759,15 @@ private void TransformBlock(byte[]/*!*//*byte[16+startIndex]*/block, int startIn
1757
1759
t = checksum [ 15 ] ;
1758
1760
for ( i = 0 ; i < 16 ; i ++ )
1759
1761
{
1760
- t = checksum [ i ] ^= MD2_S [ block [ i + startIndex ] ^ t ] ;
1762
+ t = checksum [ i ] ^= MD2_S [ block [ i ] ^ t ] ;
1761
1763
}
1762
1764
}
1763
1765
1764
1766
public override bool Update ( ReadOnlySpan < byte > data )
1765
1767
{
1766
- ProcessBlocked ( data , 16 , ( block , index ) =>
1768
+ ProcessBlocked ( data , 16 , ( self , block ) =>
1767
1769
{
1768
- TransformBlock ( block , index ) ;
1770
+ ( ( MD2 ) self ) . TransformBlock ( block ) ;
1769
1771
} ) ;
1770
1772
1771
1773
return true ;
@@ -1785,8 +1787,8 @@ public override byte[] Final()
1785
1787
buffer [ i ] = remainingBytes ;
1786
1788
1787
1789
//
1788
- TransformBlock ( buffer , 0 ) ;
1789
- TransformBlock ( checksum , 0 ) ;
1790
+ TransformBlock ( buffer . AsSpan ( ) ) ;
1791
+ TransformBlock ( checksum . AsSpan ( ) ) ;
1790
1792
1791
1793
//
1792
1794
byte [ ] hash = new byte [ 16 ] ;
@@ -1815,18 +1817,17 @@ public sealed class MD4 : HashPhpResource
1815
1817
private static void MD4_R1 ( ref uint a , uint b , uint c , uint d , uint xk , byte s ) { unchecked { a = ROTL32 ( s , a + MD4_F ( b , c , d ) + xk ) ; } }
1816
1818
private static void MD4_R2 ( ref uint a , uint b , uint c , uint d , uint xk , byte s ) { unchecked { a = ROTL32 ( s , a + MD4_G ( b , c , d ) + xk + 0x5A827999 ) ; } }
1817
1819
private static void MD4_R3 ( ref uint a , uint b , uint c , uint d , uint xk , byte s ) { unchecked { a = ROTL32 ( s , a + MD4_H ( b , c , d ) + xk + 0x6ED9EBA1 ) ; } }
1818
- private static uint [ ] Decode ( byte [ ] block , int startIndex , int bytesCount )
1820
+ private static uint [ ] Decode ( ReadOnlySpan < byte > block )
1819
1821
{
1820
- Debug . Assert ( bytesCount > 0 ) ;
1821
- Debug . Assert ( ( bytesCount % 4 ) == 0 ) ;
1822
+ Debug . Assert ( block . Length > 0 ) ;
1823
+ Debug . Assert ( ( block . Length % 4 ) == 0 ) ;
1822
1824
1823
- uint [ ] result = new uint [ bytesCount / 4 ] ;
1825
+ uint [ ] result = new uint [ block . Length / 4 ] ;
1824
1826
int index = 0 ;
1825
- while ( bytesCount > 0 )
1827
+ while ( block . Length > 0 )
1826
1828
{
1827
- result [ index ++ ] = BitConverter . ToUInt32 ( block , startIndex ) ;
1828
- startIndex += 4 ;
1829
- bytesCount -= 4 ;
1829
+ result [ index ++ ] = BitConverter . ToUInt32 ( block ) ;
1830
+ block = block . Slice ( 4 ) ;
1830
1831
}
1831
1832
1832
1833
return result ;
@@ -1847,11 +1848,10 @@ private static byte[] Encode(uint[] nums, int startIndex, int bytesCount)
1847
1848
1848
1849
return result ;
1849
1850
}
1850
- private void MD4Transform ( byte [ ] block , int startIndex )
1851
+ private void MD4Transform ( ReadOnlySpan < byte > block )
1851
1852
{
1852
1853
uint a = state [ 0 ] , b = state [ 1 ] , c = state [ 2 ] , d = state [ 3 ] ;
1853
- uint [ ] x = Decode ( block , startIndex , 64 ) ;
1854
-
1854
+ uint [ ] x = Decode ( block . Slice ( 0 , 64 ) ) ;
1855
1855
1856
1856
/* Round 1 */
1857
1857
MD4_R1 ( ref a , b , c , d , x [ 0 ] , 3 ) ;
@@ -1948,9 +1948,9 @@ public override bool Update(ReadOnlySpan<byte> data)
1948
1948
++ count [ 1 ] ;
1949
1949
count [ 1 ] += ( ( uint ) data . Length >> 29 ) ;
1950
1950
1951
- ProcessBlocked ( data , 64 , ( block , index ) =>
1951
+ ProcessBlocked ( data , 64 , ( self , block ) =>
1952
1952
{
1953
- MD4Transform ( block , index ) ;
1953
+ ( ( MD4 ) self ) . MD4Transform ( block ) ;
1954
1954
} ) ;
1955
1955
1956
1956
return true ;
@@ -2075,18 +2075,17 @@ private static void II(ref uint a, uint b, uint c, uint d, uint x, byte s, uint
2075
2075
}
2076
2076
}
2077
2077
2078
- private static uint [ ] Decode ( byte [ ] block , int startIndex , int bytesCount )
2078
+ private static uint [ ] Decode ( ReadOnlySpan < byte > block )
2079
2079
{
2080
- Debug . Assert ( bytesCount > 0 ) ;
2081
- Debug . Assert ( ( bytesCount % 4 ) == 0 ) ;
2080
+ Debug . Assert ( block . Length > 0 ) ;
2081
+ Debug . Assert ( ( block . Length % 4 ) == 0 ) ;
2082
2082
2083
- uint [ ] result = new uint [ bytesCount / 4 ] ;
2083
+ uint [ ] result = new uint [ block . Length / 4 ] ;
2084
2084
int index = 0 ;
2085
- while ( bytesCount > 0 )
2085
+ while ( block . Length > 0 )
2086
2086
{
2087
- result [ index ++ ] = BitConverter . ToUInt32 ( block , startIndex ) ;
2088
- startIndex += 4 ;
2089
- bytesCount -= 4 ;
2087
+ result [ index ++ ] = BitConverter . ToUInt32 ( block ) ;
2088
+ block = block . Slice ( 4 ) ;
2090
2089
}
2091
2090
2092
2091
return result ;
@@ -2112,12 +2111,11 @@ private static byte[] Encode(uint[] nums, int startIndex, int bytesCount)
2112
2111
/// MD5 basic transformation. Transforms state based on block.
2113
2112
/// </summary>
2114
2113
/// <param name="block"></param>
2115
- /// <param name="startIndex"></param>
2116
- private void MD5Transform ( byte [ ] /*[64]*/ block , int startIndex )
2114
+ private void MD5Transform ( ReadOnlySpan < byte > /*[64]*/ block )
2117
2115
{
2118
2116
uint a = state [ 0 ] , b = state [ 1 ] , c = state [ 2 ] , d = state [ 3 ] ;
2119
2117
2120
- uint [ ] x = Decode ( block , startIndex , 64 ) ; // [16]
2118
+ uint [ ] x = Decode ( block . Slice ( 0 , 64 ) ) ; // [16]
2121
2119
Debug . Assert ( x . Length == 16 ) ;
2122
2120
2123
2121
/* Round 1 */
@@ -2234,9 +2232,9 @@ public override bool Update(ReadOnlySpan<byte>/*!*/data)
2234
2232
count [ 1 ] += ( ( uint ) data . Length >> 29 ) ;
2235
2233
2236
2234
// Transform blocks of 64 bytes
2237
- ProcessBlocked ( data , 64 , ( block , index ) =>
2235
+ ProcessBlocked ( data , 64 , ( self , block ) =>
2238
2236
{
2239
- MD5Transform ( block , index ) ;
2237
+ ( ( MD5 ) self ) . MD5Transform ( block ) ;
2240
2238
} ) ;
2241
2239
2242
2240
return true ;
0 commit comments