WIP, abandoned: One-way backup tool with versioning and client-side encryption. Your precious data will never get lost!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.9 KiB

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;
}
}
}