/
Decrypter.cs
80 lines (66 loc) · 2.53 KB
/
Decrypter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Hl7.Fhir.Model;
using Newtonsoft.Json.Linq;
using nsoftware.IPWorksOpenPGP;
namespace DotNetOhh
{
public class Decrypter
{
private static readonly string EncryptedAnswersUrl =
"https://api.openhealthhub.com/OpenHealthhub/fhir/4/StructureDefinition/encryptedAnswers";
private static readonly string PgpPassphrase = "api-sandbox";
public void Decrypt(QuestionnaireResponse qr)
{
var encryptedAnswers = qr.GetStringExtension(EncryptedAnswersUrl);
var privateKey = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "sandbox.key"));
var pgp = new Openpgp();
pgp.Keys.Add(new Key(privateKey));
pgp.Keys[0].Passphrase = PgpPassphrase;
pgp.InputMessage = encryptedAnswers;
pgp.Decrypt();
JObject decryptedAnswers = JObject.Parse(pgp.OutputMessage);
var itemComponents = GetNestedItems(qr.Item);
itemComponents.ForEach(component => SetAnswers(component, decryptedAnswers));
}
private static List<QuestionnaireResponse.ItemComponent> GetNestedItems(
List<QuestionnaireResponse.ItemComponent> items)
{
var nestedItems = items.SelectMany(item =>
{
if (item.Item.Count == 0)
{
return Enumerable.Empty<QuestionnaireResponse.ItemComponent>();
}
return GetNestedItems(item.Item);
});
return items.Concat(nestedItems).ToList();
}
private static void SetAnswers(QuestionnaireResponse.ItemComponent component, JObject decryptedAnswers)
{
var jsonArray = decryptedAnswers[component.LinkId] as JArray;
for (int i = 0; i < jsonArray?.Count; i++)
{
SetAnswer(component.Answer[i], jsonArray?[i].ToString());
}
}
private static void SetAnswer(QuestionnaireResponse.AnswerComponent answer, string answerValue)
{
var value = answer.Value;
if (value is PrimitiveType p)
{
p.ObjectValue = answerValue;
}
if (value is Attachment attachment)
{
attachment.Data = Convert.FromBase64String(answerValue);
}
if (value is Coding coding)
{
coding.Code = answerValue;
}
}
}
}