Skip to content

Commit

Permalink
use fixed size for checkBuffer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pictos committed Jun 14, 2024
1 parent ea600ac commit d559729
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions LiteDB/Engine/Disk/Streams/AesStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public AesStream(string password, Stream stream)
// start stream from zero position
_stream.Position = 0;

var checkBuffer = _bufferPool.Rent(32);
const int checkBufferSize = 32;

var checkBuffer = _bufferPool.Rent(checkBufferSize);
var msBuffer = _bufferPool.Rent(16);

try
Expand Down Expand Up @@ -114,28 +116,28 @@ public AesStream(string password, Stream stream)
if (!isNew)
{
// check whether bytes 32 to 64 is empty. This indicates LiteDb was unable to write encrypted 1s during last attempt.
_stream.Read(checkBuffer, 0, checkBuffer.Length);
_stream.Read(checkBuffer, 0, checkBufferSize);
isNew = checkBuffer.All(x => x == 0);

// reset checkBuffer and stream position
Array.Clear(checkBuffer, 0, checkBuffer.Length);
Array.Clear(checkBuffer, 0, checkBufferSize);
_stream.Position = 32;
}

// fill checkBuffer with encrypted 1 to check when open
if (isNew)
{
checkBuffer.Fill(1, 0, checkBuffer.Length);
checkBuffer.Fill(1, 0, checkBufferSize);

_writer.Write(checkBuffer, 0, checkBuffer.Length);
_writer.Write(checkBuffer, 0, checkBufferSize);

//ensure that the "hidden" page in encrypted files is created correctly
_stream.Position = PAGE_SIZE - 1;
_stream.WriteByte(0);
}
else
{
_reader.Read(checkBuffer, 0, checkBuffer.Length);
_reader.Read(checkBuffer, 0, checkBufferSize);

if (!checkBuffer.All(x => x == 1))
{
Expand Down

0 comments on commit d559729

Please sign in to comment.