Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unique error codes to encryption-related exceptions #2259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions LiteDB/Engine/Disk/Streams/AesStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public AesStream(string password, Stream stream)

if (isEncrypted != 1)
{
throw new LiteException(0, "This file is not encrypted");
throw LiteException.FileNotEncrypted();
}

_stream.Read(this.Salt, 0, ENCRYPTION_SALT_SIZE);
Expand Down Expand Up @@ -116,7 +116,7 @@ public AesStream(string password, Stream stream)

if (!checkBuffer.All(x => x == 1))
{
throw new LiteException(0, "Invalid password");
throw LiteException.InvalidPassword();
}
}

Expand Down
6 changes: 3 additions & 3 deletions LiteDB/Engine/FileReader/FileReaderV7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public FileReaderV7(Stream stream, string password)

if (password == null && _header["salt"].AsBinary.IsFullZero() == false)
{
throw new LiteException(0, "Current data file requires password");
throw LiteException.InvalidPassword();
}
else if (password != null)
{
if (_header["salt"].AsBinary.IsFullZero())
{
throw new LiteException(0, "Current data file has no encryption - do not use password");
throw LiteException.FileNotEncrypted();
}

var hash = AesEncryption.HashSHA1(password);

if (hash.SequenceEqual(_header["password"].AsBinary) == false)
{
throw new LiteException(0, "Invalid password");
throw LiteException.InvalidPassword();
}
}

Expand Down
12 changes: 12 additions & 0 deletions LiteDB/Utils/LiteException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class LiteException : Exception
public const int INVALID_FREE_SPACE_PAGE = 213;
public const int DATA_TYPE_NOT_ASSIGNABLE = 214;
public const int AVOID_USE_OF_PROCESS = 215;
public const int NOT_ENCRYPTED = 216;
public const int INVALID_PASSWORD = 217;

#endregion

Expand Down Expand Up @@ -312,6 +314,16 @@ internal static LiteException DataTypeNotAssignable(string type1, string type2)
{
return new LiteException(DATA_TYPE_NOT_ASSIGNABLE, $"Data type {type1} is not assignable from data type {type2}");
}

internal static LiteException FileNotEncrypted()
{
return new LiteException(NOT_ENCRYPTED, "File is not encrypted.");
}

internal static LiteException InvalidPassword()
{
return new LiteException(INVALID_PASSWORD, "Invalid password.");
}

internal static LiteException AvoidUseOfProcess()
{
Expand Down