public async Task Should_Decrypt_Front_Side_File() { PassportData passportData = GetPassportData(); IDecrypter decrypter = new Decrypter(); Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey); byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/driver_license-front_side.jpg.enc"); byte[] content = decrypter.DecryptFile( encryptedContent, credentials.SecureData.DriverLicense.FrontSide ); Assert.NotEmpty(content); await System.IO.File.WriteAllBytesAsync("Files/driver_license-front_side.jpg", content); using (System.IO.MemoryStream encryptedFileStream = new System.IO.MemoryStream(encryptedContent), decryptedFileStream = new System.IO.MemoryStream() ) { await decrypter.DecryptFileAsync( encryptedFileStream, credentials.SecureData.DriverLicense.FrontSide, decryptedFileStream ); Assert.Equal(content, decryptedFileStream.ToArray()); } }
public async Task Should_Decrypt_Identity_Card_Element_Selfie() { Update update = _classFixture.Entity; PassportData passportData = update.Message.PassportData; RSA key = EncryptionKey.ReadAsRsa(); EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card"); IDecrypter decrypter = new Decrypter(); Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key); byte[] encryptedContent; using (System.IO.MemoryStream stream = new System.IO.MemoryStream(idCardEl.Selfie.FileSize)) { await BotClient.GetInfoAndDownloadFileAsync( idCardEl.Selfie.FileId, stream ); encryptedContent = stream.ToArray(); } byte[] content = decrypter.DecryptFile( encryptedContent, credentials.SecureData.IdentityCard.Selfie ); Assert.NotEmpty(content); }
public void Should_Throw_If_Null_Secret() { IDecrypter decrypter = new Decrypter(); Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile(new byte[0], new FileCredentials()) ); Assert.Matches(@"^Value cannot be null\.\s+Parameter name: Secret$", exception.Message); Assert.IsType <ArgumentNullException>(exception); }
public void Should_Throw_If_Null_EncryptedContent() { IDecrypter decrypter = new Decrypter(); Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile(null, null) ); Assert.Matches(@"^Value cannot be null\.\s+Parameter name: encryptedContent$", exception.Message); Assert.IsType <ArgumentNullException>(exception); }
public void Should_Throw_If_Invalid_Hash(string fileHash) { FileCredentials fileCredentials = new FileCredentials { Secret = "", FileHash = fileHash }; IDecrypter decrypter = new Decrypter(); Assert.ThrowsAny <FormatException>(() => decrypter.DecryptFile(new byte[16], fileCredentials) ); }
public void Should_Throw_If_Invalid_Secret(string secret) { FileCredentials fileCredentials = new FileCredentials { Secret = secret, FileHash = "" }; IDecrypter decrypter = new Decrypter(); Assert.Throws <FormatException>(() => decrypter.DecryptFile(new byte[16], fileCredentials) ); }
public void Should_Throw_If_Invalid_Hash_Length(string fileHash) { FileCredentials fileCredentials = new FileCredentials { Secret = "", FileHash = fileHash }; IDecrypter decrypter = new Decrypter(); Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile(new byte[16], fileCredentials) ); Assert.Matches(@"^Hash length is not 32: \d+\.$", exception.Message); Assert.IsType <PassportDataDecryptionException>(exception); }
public void Should_Throw_If_Invalid_Data_Bytes_Length() { IDecrypter decrypter = new Decrypter(); FileCredentials fileCredentials = new FileCredentials { Secret = "", FileHash = "" }; Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile(new byte[16 + 1], fileCredentials) ); Assert.Equal("Data length is not divisible by 16: 17.", exception.Message); Assert.IsType <PassportDataDecryptionException>(exception); }
public void Should_Throw_If_Empty_Data_Bytes_Length() { IDecrypter decrypter = new Decrypter(); FileCredentials fileCredentials = new FileCredentials { Secret = "", FileHash = "" }; Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile(new byte[0], fileCredentials) ); Assert.Matches(@"^Data array is empty\.\s+Parameter name: encryptedContent$", exception.Message); Assert.IsType <ArgumentException>(exception); }
public async Task Should_Decrypt_Translation_File() { Update update = _classFixture.Entity; PassportData passportData = update.Message.PassportData; RSA key = EncryptionKey.ReadAsRsa(); EncryptedPassportElement element = passportData.Data.Single(); IDecrypter decrypter = new Decrypter(); Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key); for (int i = 0; i < element.Translation.Length; i++) { PassportFile passportFile = element.Translation[i]; FileCredentials fileCredentials = credentials.SecureData.DriverLicense.Translation[i]; byte[] encryptedContent; { File encryptedFileInfo = await BotClient.GetFileAsync(passportFile.FileId); Assert.NotEmpty(encryptedFileInfo.FilePath); Assert.NotEmpty(encryptedFileInfo.FileId); Assert.InRange(encryptedFileInfo.FileSize, 1_000, 50_000_000); using (System.IO.MemoryStream stream = new System.IO.MemoryStream(encryptedFileInfo.FileSize)) { await BotClient.DownloadFileAsync(encryptedFileInfo.FilePath, stream); encryptedContent = stream.ToArray(); } } byte[] translationContent = decrypter.DecryptFile( encryptedContent, fileCredentials ); Assert.NotEmpty(translationContent); string decryptedFilePath = System.IO.Path.GetTempFileName(); await System.IO.File.WriteAllBytesAsync(decryptedFilePath, translationContent); _output.WriteLine("Translation JPEG file is written to \"{0}\".", decryptedFilePath); } }
public async Task Should_Decrypt_Utility_Bill_Element_Translation() { PassportData passportData = GetPassportData(); EncryptedPassportElement billElement = Assert.Single(passportData.Data, el => el.Type == "utility_bill"); Assert.NotNull(billElement.Translation); PassportFile translationFile = Assert.Single(billElement.Translation); Assert.Equal("DgADAQADOwADGV9BRP4b7RLGAtUKAg", translationFile.FileId); Assert.InRange(translationFile.FileDate, new DateTime(2018, 8, 30), new DateTime(2018, 8, 31)); Assert.Equal(0, translationFile.FileSize); IDecrypter decrypter = new Decrypter(); Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey); FileCredentials translationFileCredentials = Assert.Single(credentials.SecureData.UtilityBill.Translation); byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/utility_bill-translation.jpg.enc"); byte[] content = decrypter.DecryptFile( encryptedContent, translationFileCredentials ); Assert.NotEmpty(content); await System.IO.File.WriteAllBytesAsync("Files/utility_bill-translation.jpg", content); using (System.IO.MemoryStream encryptedFileStream = new System.IO.MemoryStream(encryptedContent), decryptedFileStream = new System.IO.MemoryStream() ) { await decrypter.DecryptFileAsync( encryptedFileStream, translationFileCredentials, decryptedFileStream ); Assert.Equal(content, decryptedFileStream.ToArray()); } }
public async Task Should_Decrypt_Selfie_File() { Update update = _classFixture.Entity; PassportData passportData = update.Message.PassportData; RSA key = EncryptionKey.ReadAsRsa(); EncryptedPassportElement element = passportData.Data.Single(); IDecrypter decrypter = new Decrypter(); Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, key); byte[] encryptedContent; { File encryptedFileInfo = await BotClient.GetFileAsync(element.Selfie.FileId); Assert.NotEmpty(encryptedFileInfo.FilePath); Assert.NotEmpty(encryptedFileInfo.FileId); Assert.InRange(encryptedFileInfo.FileSize, 1_000, 50_000_000); using (System.IO.MemoryStream stream = new System.IO.MemoryStream(encryptedFileInfo.FileSize)) { await BotClient.DownloadFileAsync(encryptedFileInfo.FilePath, stream); encryptedContent = stream.ToArray(); } } byte[] selfieContent = decrypter.DecryptFile( encryptedContent, credentials.SecureData.DriverLicense.Selfie ); Assert.NotEmpty(selfieContent); using (System.IO.Stream stream = new System.IO.MemoryStream(selfieContent)) { await BotClient.SendPhotoAsync( _fixture.SupergroupChat, stream, "selfie with driver license", replyToMessageId : update.Message.MessageId ); } }
public async Task Should_Decrypt_From_Bytes() { FileCredentials fileCredentials = new FileCredentials { FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=", Secret = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=", }; IDecrypter decrypter = new Decrypter(); byte[] encContent = await File.ReadAllBytesAsync("Files/bytes_dec1.driver_license-selfie.jpg.enc"); byte[] content = decrypter.DecryptFile( encContent, fileCredentials ); Assert.NotEmpty(content); Assert.InRange(content.Length, encContent.Length - 256, encContent.Length - 33); }
public async Task Should_Decrypt_Identity_Card_Element_Reverse_Side() { PassportData passportData = GetPassportData(); EncryptedPassportElement idCardEl = Assert.Single(passportData.Data, el => el.Type == "identity_card"); Assert.NotNull(idCardEl.ReverseSide); Assert.Equal("DgADAQADKAADNfRARK9jbzh5AAFqvAI", idCardEl.ReverseSide.FileId); Assert.InRange(idCardEl.ReverseSide.FileDate, new DateTime(2018, 8, 30), new DateTime(2018, 8, 31)); Assert.Equal(0, idCardEl.ReverseSide.FileSize); IDecrypter decrypter = new Decrypter(); Credentials credentials = decrypter.DecryptCredentials(passportData.Credentials, EncryptionKey.RsaPrivateKey); byte[] encryptedContent = await System.IO.File.ReadAllBytesAsync("Files/identity_card-reverse_side.jpg.enc"); byte[] content = decrypter.DecryptFile( encryptedContent, credentials.SecureData.IdentityCard.ReverseSide ); Assert.NotEmpty(content); await System.IO.File.WriteAllBytesAsync("Files/identity_card-reverse_side.jpg", encryptedContent); using (System.IO.MemoryStream encryptedFileStream = new System.IO.MemoryStream(encryptedContent), decryptedFileStream = new System.IO.MemoryStream() ) { await decrypter.DecryptFileAsync( encryptedFileStream, credentials.SecureData.IdentityCard.ReverseSide, decryptedFileStream ); Assert.Equal(content, decryptedFileStream.ToArray()); } }
public async Task Should_Throw_Decrypting_Bytes_With_Wrong_FileCredentials() { FileCredentials wrongFileCredentials = new FileCredentials { FileHash = "THTjgv2FU7kff/29Vty/IcqKPmOGkL7F35fAzmkfZdI=", Secret = "a+jxJoKPEaz77VCjRvDVcYHfIO3+h+oI+ruZh+KkYa0=", }; IDecrypter decrypter = new Decrypter(); byte[] encContent = await File.ReadAllBytesAsync("Files/bytes_dec3.driver_license-selfie.jpg.enc"); Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile( encContent, wrongFileCredentials ) ); Assert.Matches(@"^Data hash mismatch at position \d+\.$", exception.Message); Assert.IsType <PassportDataDecryptionException>(exception); }
public void Should_Throw_Decrypting_Unencrypted_File_Valid_Length() { FileCredentials fileCredentials = new FileCredentials { FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=", Secret = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=", }; IDecrypter decrypter = new Decrypter(); byte[] encContent = new byte[2048]; // data length is divisible by 16 Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile( encContent, fileCredentials ) ); Assert.Matches(@"^Data hash mismatch at position \d+\.$", exception.Message); Assert.IsType <PassportDataDecryptionException>(exception); }
public async Task Should_Throw_Decrypting_Unencrypted_File_Invalid_Length() { FileCredentials fileCredentials = new FileCredentials { FileHash = "v3q47iscI6TS94CMo7HGQUOxw28LIf82NJBkImzP57c=", Secret = "vF7nut7clg/H/pEaTJigo4mQJ0s8B+HGCWKTWtOTIdo=", }; IDecrypter decrypter = new Decrypter(); byte[] encContent = await File.ReadAllBytesAsync("Files/bytes_dec2.driver_license-selfie.jpg"); Exception exception = Assert.ThrowsAny <Exception>(() => decrypter.DecryptFile( encContent, fileCredentials ) ); Assert.Matches(@"^Data length is not divisible by 16: \d+\.$", exception.Message); Assert.IsType <PassportDataDecryptionException>(exception); }