Skip to content

Commit

Permalink
Merge pull request #2259 from arcanelab/Issue-2257-Add-Unique-Excepti…
Browse files Browse the repository at this point in the history
…on-Codes

Thanks @arcanelab
  • Loading branch information
mbdavid authored Jul 20, 2023
2 parents 90c5d8d + af75062 commit 81619ac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions LiteDB/Engine/Disk/Streams/AesStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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 @@ -134,7 +134,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

0 comments on commit 81619ac

Please # to comment.