From fd9f1b27ccc87a0cfaaf5bf2524e81498e302f2c Mon Sep 17 00:00:00 2001
From: Klaus Fischer <klaus.fischer@eloware.com>
Date: Thu, 16 Jun 2022 15:21:51 +0200
Subject: [PATCH] Added filename to attachment and calculated attachment hash
 in subscriber

---
 FitConnect/FluentSubscriber.cs  | 9 +++++++--
 FitConnect/Models/Attachment.cs | 2 +-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/FitConnect/FluentSubscriber.cs b/FitConnect/FluentSubscriber.cs
index ee3aa498..b628dbe0 100644
--- a/FitConnect/FluentSubscriber.cs
+++ b/FitConnect/FluentSubscriber.cs
@@ -1,5 +1,6 @@
 using System.Buffers.Text;
 using System.Security.Authentication;
+using System.Security.Cryptography;
 using System.Security.Cryptography.X509Certificates;
 using FitConnect.Models;
 using FitConnect.Services;
@@ -107,8 +108,12 @@ public class FluentSubscriber : Subscriber, IFluentApi<FluentSubscriber> {
             var encryptedAttachment = SubmissionService.GetAttachment(Submission.Id, id);
 
             var content = Encryption.DecryptData(Convert.FromBase64String(encryptedAttachment));
-            // TODO Where to get the HASH from?
-            attachments.Add(new Attachment(id, content, "HASH"));
+            
+            // TODO where do I get the hash from the server to verify the attachment?
+            var hash = MD5.Create(HashAlgorithmName.SHA512.ToString())?.ComputeHash(content) ??
+                       Array.Empty<byte>();
+            
+            attachments.Add(new Attachment(id, content, Convert.ToBase64String(hash), "dummy.pdf"));
         }
 
         Submission.Attachments = attachments;
diff --git a/FitConnect/Models/Attachment.cs b/FitConnect/Models/Attachment.cs
index d432c105..2c1e5da5 100644
--- a/FitConnect/Models/Attachment.cs
+++ b/FitConnect/Models/Attachment.cs
@@ -1,3 +1,3 @@
 namespace FitConnect.Models;
 
-public record Attachment(string Id, byte[] Content, string Hash);
+public record Attachment(string Id, byte[] Content, string Hash, string Filename);
-- 
GitLab