Skip to content

Commit 4e13ee8

Browse files
committed
Small optimization.
1 parent faa3ea9 commit 4e13ee8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Kdf/PBKDF2.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,28 @@ protected override void Dispose(bool disposing)
123123
byte[] inputBuffer = new byte[4];
124124
byte[] Func()
125125
{
126-
new Utils.IntStruct { UintValue = this.block }.ToBEBytes(inputBuffer);
126+
// localize members
127+
var _inputBuffer = this.inputBuffer;
128+
var _hmac = this.hmac;
129+
var _hmac2 = this.hmac2;
130+
131+
new Utils.IntStruct { UintValue = this.block }.ToBEBytes(_inputBuffer);
127132
this.hmac.TransformBlock(inputBuffer: this.salt, inputOffset: 0, inputCount: this.salt.Length, outputBuffer: null, outputOffset: 0);
128-
this.hmac.TransformBlock(inputBuffer: inputBuffer, inputOffset: 0, inputCount: inputBuffer.Length, outputBuffer: null, outputOffset: 0);
129-
this.hmac.TransformFinalBlock(inputBuffer: inputBuffer, inputOffset: 0, inputCount: 0);
133+
this.hmac.TransformBlock(inputBuffer: _inputBuffer, inputOffset: 0, inputCount: _inputBuffer.Length, outputBuffer: null, outputOffset: 0);
134+
this.hmac.TransformFinalBlock(inputBuffer: _inputBuffer, inputOffset: 0, inputCount: 0);
130135
byte[] hash = this.hmac.Hash; // creates a copy
131136
this.hmac.Initialize();
132137
byte[] buffer3 = hash;
133138

134139
for (int i = 2, blockSize = BlockSize, j = this.iterations; i <= j; i++)
135140
{
136-
if (hmac2 != null)
141+
if (_hmac2 != null)
137142
{
138-
hmac2.TransformBlock(hash, 0, blockSize, null, 0);
139-
hmac2.TransformFinalBlock(hash, 0, 0);
140-
hash = hmac2.HashInner;
143+
_hmac2.TransformBlock(hash, 0, blockSize, null, 0);
144+
_hmac2.TransformFinalBlock(hash, 0, 0);
145+
hash = _hmac2.HashInner;
141146
}
142-
else hash = this.hmac.ComputeHash(hash);
147+
else hash = _hmac.ComputeHash(hash);
143148
Utils.Xor(dest: buffer3, destOffset: 0, left: hash, leftOffset: 0, byteCount: blockSize);
144149
}
145150
this.block++;

0 commit comments

Comments
 (0)