Added DPAPI json contract resolver

This commit is contained in:
Jaex 2020-05-29 19:13:37 +03:00
parent cdd4fff59e
commit 4371884843
5 changed files with 169 additions and 1 deletions

View file

@ -0,0 +1,54 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2020 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ShareX.HelpersLib
{
public class DPAPIEncryptedStringPropertyResolver : WritablePropertiesOnlyResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
foreach (JsonProperty prop in props.Where(p => p.PropertyType == typeof(string)))
{
PropertyInfo pi = type.GetProperty(prop.UnderlyingName);
if (pi != null && pi.GetCustomAttribute(typeof(JsonEncryptAttribute), true) != null)
{
prop.ValueProvider = new DPAPIEncryptedStringValueProvider(pi);
}
}
return props;
}
}
}

View file

@ -0,0 +1,75 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2020 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using Newtonsoft.Json.Serialization;
using System.Reflection;
namespace ShareX.HelpersLib
{
public class DPAPIEncryptedStringValueProvider : IValueProvider
{
private PropertyInfo targetProperty;
public DPAPIEncryptedStringValueProvider(PropertyInfo targetProperty)
{
this.targetProperty = targetProperty;
}
public object GetValue(object target)
{
string value = (string)targetProperty.GetValue(target);
string encryptedValue = null;
if (!string.IsNullOrEmpty(value))
{
try
{
encryptedValue = DPAPI.Encrypt(value);
}
catch
{
}
}
return encryptedValue;
}
public void SetValue(object target, object value)
{
string decryptedValue = null;
try
{
decryptedValue = DPAPI.Decrypt((string)value);
}
catch
{
}
targetProperty.SetValue(target, decryptedValue);
}
}
}

View file

@ -0,0 +1,34 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2020 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
namespace ShareX.HelpersLib
{
[AttributeUsage(AttributeTargets.Property)]
public class JsonEncryptAttribute : Attribute
{
}
}

View file

@ -31,7 +31,7 @@
namespace ShareX.HelpersLib
{
internal class WritablePropertiesOnlyResolver : DefaultContractResolver
public class WritablePropertiesOnlyResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{

View file

@ -91,6 +91,7 @@
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Security" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -139,6 +140,7 @@
<Compile Include="Controls\ToolStripNumericUpDown.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Cryptographic\DPAPI.cs" />
<Compile Include="Emoji.cs" />
<Compile Include="Forms\DebugForm.cs">
<SubType>Form</SubType>
@ -151,6 +153,9 @@
<Compile Include="PointInfo.cs" />
<Compile Include="Random\RandomCrypto.cs" />
<Compile Include="Random\RandomFast.cs" />
<Compile Include="Settings\DPAPIEncryptedStringPropertyResolver.cs" />
<Compile Include="Settings\DPAPIEncryptedStringValueProvider.cs" />
<Compile Include="Settings\JsonEncryptAttribute.cs" />
<Compile Include="SevenZipManager.cs" />
<Compile Include="Input\HotkeyForm.cs">
<SubType>Form</SubType>