Skip to content

Commit de07ba3

Browse files
committed
Renamed some fields for clarity.
Refactor out _offset.
1 parent 5a16f85 commit de07ba3

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/SshNet.Security.Cryptography.Shared/SHA1HashProvider.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,28 @@ internal class SHA1HashProvider : HashProviderBase
3131
/// The word buffer.
3232
/// </summary>
3333
private readonly uint[] _words;
34-
private int _offset;
34+
35+
/// <summary>
36+
/// Buffered bytes.
37+
/// </summary>
3538
private readonly byte[] _buffer;
36-
private int _bufferOffset;
39+
40+
/// <summary>
41+
/// The number of bytes in the buffer.
42+
/// </summary>
43+
private int _bufferByteCount;
3744

3845
/// <summary>
3946
/// The number of bytes in the message.
4047
/// </summary>
41-
private long _byteCount;
48+
private long _messageByteCount;
4249

4350
/// <summary>
4451
/// Initializes a new instance of the <see cref="SHA1"/> class.
4552
/// </summary>
4653
public SHA1HashProvider()
4754
{
48-
_buffer = new byte[64];
55+
_buffer = new byte[BlockSize];
4956
_words = new uint[80];
5057

5158
InitializeHashValue();
@@ -101,35 +108,38 @@ public override int OutputBlockSize
101108
/// <param name="cbSize">The number of bytes in the byte array to use as data.</param>
102109
public override void HashCore(byte[] array, int ibStart, int cbSize)
103110
{
104-
_byteCount += cbSize;
111+
_messageByteCount += cbSize;
105112

106113
// when there's an incomplete block, then complete and process it
107-
if (_bufferOffset > 0 && (cbSize + _bufferOffset) >= BlockSize)
114+
if (_bufferByteCount > 0 && (cbSize + _bufferByteCount) >= BlockSize)
108115
{
109-
var bytesToCopy = BlockSize - _bufferOffset;
110-
Buffer.BlockCopy(array, ibStart, _buffer, _bufferOffset, bytesToCopy);
116+
var bytesToCopy = BlockSize - _bufferByteCount;
117+
Buffer.BlockCopy(array, ibStart, _buffer, _bufferByteCount, bytesToCopy);
111118

119+
// process complete block
112120
ProcessBlock(_buffer, 0);
113121

114122
ibStart += bytesToCopy;
115123
cbSize -= bytesToCopy;
116-
_bufferOffset = 0;
124+
125+
// we've processed all buffered bytes
126+
_bufferByteCount = 0;
117127
}
118128

119129
// process whole blocks
120130
while (cbSize >= BlockSize)
121131
{
122132
ProcessBlock(array, ibStart);
123133

124-
ibStart += 64;
125-
cbSize -= 64;
134+
ibStart += BlockSize;
135+
cbSize -= BlockSize;
126136
}
127137

128138
// buffer remaining bytes
129139
if (cbSize > 0)
130140
{
131-
Buffer.BlockCopy(array, ibStart, _buffer, _bufferOffset, cbSize);
132-
_bufferOffset += cbSize;
141+
Buffer.BlockCopy(array, ibStart, _buffer, _bufferByteCount, cbSize);
142+
_bufferByteCount += cbSize;
133143
}
134144
}
135145

@@ -144,10 +154,10 @@ public override byte[] HashFinal()
144154
var output = new byte[DigestSize];
145155

146156
// capture message length in bytes before padding
147-
var bitLength = (_byteCount << 3);
157+
var bitLength = (_messageByteCount << 3);
148158

149159
// total length of the padded message must be a multiple of the block size (64 bytes)
150-
var paddingLength = BlockSize - (_byteCount % BlockSize);
160+
var paddingLength = BlockSize - (_messageByteCount % BlockSize);
151161

152162
// ensure padding can contain 64-bit integer representing the message length
153163
// if necessary another block must be added
@@ -183,14 +193,14 @@ public override void Reset()
183193
{
184194
InitializeHashValue();
185195

186-
_byteCount = 0;
187-
_bufferOffset = 0;
196+
_messageByteCount = 0;
197+
198+
_bufferByteCount = 0;
188199
for (var i = 0; i < _buffer.Length; i++)
189200
{
190201
_buffer[i] = 0;
191202
}
192203

193-
_offset = 0;
194204
for (var i = 0; i != _words.Length; i++)
195205
{
196206
_words[i] = 0;
@@ -225,7 +235,7 @@ private void ProcessBlock(byte[] buffer, int offset)
225235
{
226236
for (var i = 0; i < 16; i++)
227237
{
228-
_words[_offset++] = BigEndianToUInt32(buffer, offset);
238+
_words[i] = BigEndianToUInt32(buffer, offset);
229239
offset += 4;
230240
}
231241

@@ -524,11 +534,6 @@ private void ProcessBlock(byte[] buffer, int offset)
524534
_h3 += c;
525535
_h4 += d;
526536
_h5 += e;
527-
528-
//
529-
// reset start of the buffer.
530-
//
531-
_offset = 0;
532537
}
533538

534539
private static uint BigEndianToUInt32(byte[] bs, int off)

0 commit comments

Comments
 (0)