Refactoring; fixed code smells

master
Inga 🏳‍🌈 4 years ago
parent f1652c5771
commit 7ffffd1c0c
  1. 12
      Source/BackupEngine/Contracts/IBackupReport.cs
  2. 15
      Source/ContentTransformer/ClearText/BufferHelpers.cs
  3. 27
      Source/ContentTransformer/ClearText/ClearTextContentEncryptor.cs
  4. 56
      Source/ContentTransformer/ClearText/DataWithSignature.cs
  5. 2
      Source/Hasher/SHA1/SHA1ContentHasher.cs
  6. 1
      Source/ReportStorage/InMemoryReportStorage/FileInfo.cs
  7. 1
      Source/SourceStorage/InMemorySourceStorage/SourceStorage.cs
  8. 1
      Source/TargetMetadataStorage/InMemoryMetadataStorage/MetadataStorage.cs
  9. 2
      Tests/ContentTransformer/ClearText/EncryptorAndHasherTests.cs
  10. 4
      Tests/Hasher/SHA1/HasherTests.cs

@ -2,16 +2,16 @@
{ {
using System; using System;
public class IBackupReport public interface IBackupReport
{ {
public long FilesUploaded { get; } long FilesUploaded { get; }
public long BytesUploaded { get; } long BytesUploaded { get; }
public long FilesUpdated { get; } long FilesUpdated { get; }
public long FilesProcessed { get; } long FilesProcessed { get; }
public Exception[] Errors { get; } Exception[] Errors { get; }
} }
} }

@ -0,0 +1,15 @@
namespace EternalArrowBackup.ContentTransformer.ClearText
{
using System;
internal static class BufferHelpers
{
public static void Add(byte[] source, byte[] target, int targetOffset) {
Buffer.BlockCopy(source, 0, target, targetOffset, source.Length);
}
public static void Extract(byte[] source, int sourceOffset, byte[] target) {
Buffer.BlockCopy(source, sourceOffset, target, 0, target.Length);
}
}
}

@ -16,18 +16,12 @@
private IContentHasher Hasher { get; } private IContentHasher Hasher { get; }
public async Task<IDecryptionResult> GetOriginalData(byte[] encryptedData) public async Task<IDecryptionResult> GetOriginalData(byte[] transformedData)
{ {
var hashLength = encryptedData[encryptedData.Length - 1]; var dataWithSignature = DataWithSignature.FromByteArray(transformedData);
var originalData = new byte[encryptedData.Length - hashLength - 1]; var expectedHash = Encoding.UTF8.GetString(dataWithSignature.Signature);
var hashBytes = new byte[hashLength]; using (var stream = new MemoryStream(dataWithSignature.Data))
Buffer.BlockCopy(encryptedData, 0, originalData, 0, originalData.Length);
Buffer.BlockCopy(encryptedData, originalData.Length, hashBytes, 0, hashBytes.Length);
var expectedHash = Encoding.UTF8.GetString(hashBytes);
using (var stream = new MemoryStream(originalData))
{ {
var actualHash = await this.Hasher.ComputeHash(stream); var actualHash = await this.Hasher.ComputeHash(stream);
@ -37,7 +31,7 @@
} }
} }
return new SuccessfulDecryptionResult(originalData); return new SuccessfulDecryptionResult(dataWithSignature.Data);
} }
public async Task<byte[]> TransformData(byte[] originalData) public async Task<byte[]> TransformData(byte[] originalData)
@ -49,16 +43,9 @@
} }
var hashBytes = Encoding.UTF8.GetBytes(hash); var hashBytes = Encoding.UTF8.GetBytes(hash);
if (hashBytes.Length >= 256) var dataWithSignature = new DataWithSignature(originalData, hashBytes);
{
throw new Exception("Hash should be shorter than 256 bytes");
}
var result = new byte[originalData.Length + hashBytes.Length + 1]; return dataWithSignature.AsByteArray();
Buffer.BlockCopy(originalData, 0, result, 0, originalData.Length);
Buffer.BlockCopy(hashBytes, 0, result, originalData.Length, hashBytes.Length);
result[result.Length - 1] = (byte)hashBytes.Length;
return result;
} }
private class FailedDecryptionResult : IDecryptionResult private class FailedDecryptionResult : IDecryptionResult

@ -0,0 +1,56 @@
namespace EternalArrowBackup.ContentTransformer.ClearText
{
using System;
public class DataWithSignature
{
public DataWithSignature(byte[] data, byte[] signature) {
if (data == null) {
throw new ArgumentNullException(nameof(data));
}
if (signature == null) {
throw new ArgumentNullException(nameof(signature));
}
if (signature.Length > 0xffff) {
throw new ArgumentOutOfRangeException(nameof(signature), "Signature should be shorter than 64k");
}
this.Data = data;
this.Signature = signature;
}
public byte[] Data { get; }
public byte[] Signature { get; }
public static DataWithSignature FromByteArray(byte[] byteArray) {
var signatureLengthBytes = new byte[2];
BufferHelpers.Extract(byteArray, byteArray.Length - 2, signatureLengthBytes);
var signatureLength = (signatureLengthBytes[0] * 0x100) + signatureLengthBytes[1];
var data = new byte[byteArray.Length - signatureLength - 2];
var signature = new byte[signatureLength];
BufferHelpers.Extract(byteArray, 0, data);
BufferHelpers.Extract(byteArray, data.Length, signature);
return new DataWithSignature(data, signature);
}
public byte[] AsByteArray() {
var signatureLengthBytes = new[] {
(byte)(this.Signature.Length / 0x100),
(byte)(this.Signature.Length % 0x100),
};
var result = new byte[this.Data.Length + this.Signature.Length + 2];
BufferHelpers.Add(this.Data, result, 0);
BufferHelpers.Add(this.Signature, result, this.Data.Length);
BufferHelpers.Add(signatureLengthBytes, result, this.Data.Length + this.Signature.Length);
return result;
}
}
}

@ -5,7 +5,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using EternalArrowBackup.Hasher.Contracts; using EternalArrowBackup.Hasher.Contracts;
public class SHA1ContentHasher : IContentHasher public class Sha1ContentHasher : IContentHasher
{ {
public Task<string> ComputeHash(Stream content) public Task<string> ComputeHash(Stream content)
{ {

@ -1,6 +1,5 @@
namespace EternalArrowBackup.ReportStorage.InMemoryReportStorage namespace EternalArrowBackup.ReportStorage.InMemoryReportStorage
{ {
using System;
using EternalArrowBackup.ReportStorage.Contracts; using EternalArrowBackup.ReportStorage.Contracts;
class FileInfo : IFileInfo class FileInfo : IFileInfo

@ -1,6 +1,5 @@
namespace EternalArrowBackup.SourceStorage.InMemorySourceStorage namespace EternalArrowBackup.SourceStorage.InMemorySourceStorage
{ {
using System;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

@ -1,6 +1,5 @@
namespace EternalArrowBackup.TargetMetadataStorage.InMemoryMetadataStorage namespace EternalArrowBackup.TargetMetadataStorage.InMemoryMetadataStorage
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

@ -15,7 +15,7 @@
public static async Task TestCorrectDecryption(string message) public static async Task TestCorrectDecryption(string message)
{ {
var messageBytes = Encoding.ASCII.GetBytes(message); var messageBytes = Encoding.ASCII.GetBytes(message);
var hasher = new SHA1ContentHasher(); var hasher = new Sha1ContentHasher();
var encryptor = new ClearTextContentEncryptor(hasher); var encryptor = new ClearTextContentEncryptor(hasher);
var encrypted = await encryptor.TransformData(messageBytes); var encrypted = await encryptor.TransformData(messageBytes);

@ -21,7 +21,7 @@
public static async Task TestHashes(string expectedHash, string inputMessage) public static async Task TestHashes(string expectedHash, string inputMessage)
{ {
var expectedHashFormatted = expectedHash.Replace(" ", string.Empty).ToLowerInvariant(); var expectedHashFormatted = expectedHash.Replace(" ", string.Empty).ToLowerInvariant();
var hasher = new SHA1ContentHasher(); var hasher = new Sha1ContentHasher();
var messageBytes = Encoding.ASCII.GetBytes(inputMessage); var messageBytes = Encoding.ASCII.GetBytes(inputMessage);
using (var stream = new MemoryStream(messageBytes)) using (var stream = new MemoryStream(messageBytes))
{ {
@ -51,7 +51,7 @@
var expectedTimeLow = length / (MegabytesPerSecondHighSpeed * 1000); var expectedTimeLow = length / (MegabytesPerSecondHighSpeed * 1000);
var expectedTimeHigh = 1 + length / (MegabytesPerSecondLowSpeed * 1000); var expectedTimeHigh = 1 + length / (MegabytesPerSecondLowSpeed * 1000);
var hasher = new SHA1ContentHasher(); var hasher = new Sha1ContentHasher();
using (var stream = new MemoryStream(messageBytes)) using (var stream = new MemoryStream(messageBytes))
{ {
await hasher.ComputeHash(stream); await hasher.ComputeHash(stream);

Loading…
Cancel
Save