Skip to content

Commit

Permalink
Test credentials decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
poulad committed Sep 3, 2018
1 parent b5c45db commit 40dd003
Show file tree
Hide file tree
Showing 4 changed files with 530 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/Telegram.Bot.Extensions.Passport/Decryption/Decrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ RSA key
throw new ArgumentNullException(nameof(encryptedCredentials));
if (key is null)
throw new ArgumentNullException(nameof(key));
if (encryptedCredentials.Data is null)
throw new ArgumentNullException(nameof(encryptedCredentials.Data));
if (encryptedCredentials.Secret is null)
throw new ArgumentNullException(nameof(encryptedCredentials.Secret));
if (encryptedCredentials.Hash is null)
throw new ArgumentNullException(nameof(encryptedCredentials.Hash));

byte[] data = Convert.FromBase64String(encryptedCredentials.Data);
if (data.Length == 0)
throw new ArgumentException("Data is empty.", nameof(encryptedCredentials.Data));
if (data.Length % 16 != 0)
throw new PassportDataDecryptionException($"Invalid data length: {data.Length}");
throw new PassportDataDecryptionException
($"Data length is not divisible by 16: {data.Length}.");

byte[] encryptedSecret = Convert.FromBase64String(encryptedCredentials.Secret);

byte[] hash = Convert.FromBase64String(encryptedCredentials.Hash);
if (hash.Length != 32)
throw new PassportDataDecryptionException($"Invalid hash length: {hash.Length}");
throw new PassportDataDecryptionException($"Hash length is not 32: {hash.Length}.");

byte[] encryptedSecret = Convert.FromBase64String(encryptedCredentials.Secret);
byte[] secret = key.Decrypt(encryptedSecret, RSAEncryptionPadding.OaepSHA1);

byte[] decryptedData = DecryptDataBytes(data, secret, hash);
Expand All @@ -50,7 +60,7 @@ public TValue DecryptData<TValue>(
string encryptedData,
DataCredentials dataCredentials
)
where TValue : IDecryptedValue
where TValue : class, IDecryptedValue
{
if (encryptedData is null)
throw new ArgumentNullException(nameof(encryptedData));
Expand Down Expand Up @@ -102,7 +112,6 @@ FileCredentials fileCredentials

byte[] dataSecret = Convert.FromBase64String(fileCredentials.Secret);
byte[] dataHash = Convert.FromBase64String(fileCredentials.FileHash);

if (dataHash.Length != 32)
throw new PassportDataDecryptionException($"Hash length is not 32: {dataHash.Length}.");

Expand Down Expand Up @@ -139,7 +148,6 @@ public Task DecryptFileAsync(

byte[] dataSecret = Convert.FromBase64String(fileCredentials.Secret);
byte[] dataHash = Convert.FromBase64String(fileCredentials.FileHash);

if (dataHash.Length != 32)
throw new PassportDataDecryptionException($"Hash length is not 32: {dataHash.Length}.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TValue DecryptData<TValue>(
string encryptedData,
DataCredentials dataCredentials
)
where TValue : IDecryptedValue;
where TValue : class, IDecryptedValue;

byte[] DecryptFile(
byte[] encryptedContent,
Expand Down
Loading

0 comments on commit 40dd003

Please sign in to comment.