add securestringjsonconverter

This commit is contained in:
schedpas
2020-07-14 15:42:28 +02:00
parent 221e249dc8
commit 4ef6777ba5
3 changed files with 42 additions and 0 deletions

View File

@@ -51,6 +51,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SecureString.cs" /> <Compile Include="SecureString.cs" />
<Compile Include="UniquieID.cs" /> <Compile Include="UniquieID.cs" />
<Compile Include="SecureStringJsonConverter.cs" />
<Compile Include="UniquiIDStringJsonConverter.cs" /> <Compile Include="UniquiIDStringJsonConverter.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
namespace Pilz.Cryptography namespace Pilz.Cryptography
{ {
[JsonConverter(typeof(Json.Converters.SecureStringJsonConverter))]
public class SecureString public class SecureString
{ {
public static ICrypter DefaultCrypter { get; set; } public static ICrypter DefaultCrypter { get; set; }

View File

@@ -0,0 +1,40 @@
using Newtonsoft.Json;
using Pilz.Cryptography;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pilz.Json.Converters
{
public class SecureStringJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(SecureString).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var idString = serializer.Deserialize<string>(reader);
SecureString id;
if (existingValue is object)
id = (SecureString)existingValue;
else
id = new SecureString();
id.EncryptedValue = idString;
return id;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, ((SecureString)value).EncryptedValue);
}
}
}