Skip to content

Commit 075a113

Browse files
committed
Splitting project into a lib that can be exported by other projects
1 parent 9ae4fcd commit 075a113

22 files changed

Lines changed: 980 additions & 331 deletions
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "LibUserPreferences.h"
2+
3+
#include "../UserPreferences.Shared/Common.h"
4+
#include "../UserPreferences.Shared/Encryption.h"
5+
6+
DllExport APIResult DecryptData(
7+
_In_ const uint8_t *encrypted_buffer,
8+
_In_ size_t encrypted_buffer_size,
9+
_In_ const char *machine_guid,
10+
_In_ const char *salt_string,
11+
_Out_ uint8_t *out_decrypted_buffer,
12+
_Inout_ size_t *out_decrypted_buffer_size)
13+
{
14+
if (encrypted_buffer == nullptr || out_decrypted_buffer_size == nullptr)
15+
{
16+
return kResult_GeneralFailure;
17+
}
18+
19+
std::string guid;
20+
if (machine_guid == nullptr)
21+
{
22+
guid = UserPreferences::Common::GetMachineGuid();
23+
}
24+
else
25+
{
26+
guid = machine_guid;
27+
}
28+
29+
std::string salt;
30+
if (salt_string == nullptr)
31+
{
32+
salt = UserPreferences::Common::GetDefaultSalt();
33+
}
34+
else
35+
{
36+
salt = salt_string;
37+
}
38+
39+
const auto encrypted = std::vector<uint8_t>(&encrypted_buffer[0], &encrypted_buffer[encrypted_buffer_size]);
40+
const auto decrypted = UserPreferences::Encryption::DecryptData(encrypted, guid, salt);
41+
42+
if (out_decrypted_buffer == nullptr || *out_decrypted_buffer_size < decrypted.size())
43+
{
44+
*out_decrypted_buffer_size = decrypted.size();
45+
return kResult_BufferTooSmall;
46+
}
47+
48+
*out_decrypted_buffer_size = decrypted.size();
49+
memcpy(out_decrypted_buffer, &decrypted[0], *out_decrypted_buffer_size);
50+
return kResult_GeneralSuccess;
51+
}
52+
53+
DllExport APIResult EncryptData(
54+
_In_ const uint8_t *plaintext_data,
55+
_In_ size_t plaintext_data_size,
56+
_In_ const char *machine_guid,
57+
_In_ const char *salt_string,
58+
_Out_ uint8_t *out_encrypted_buffer,
59+
_Inout_ size_t *out_encrypted_buffer_size)
60+
{
61+
if (plaintext_data == nullptr || out_encrypted_buffer_size == nullptr)
62+
{
63+
return kResult_GeneralFailure;
64+
}
65+
66+
std::string guid;
67+
if (machine_guid == nullptr)
68+
{
69+
guid = UserPreferences::Common::GetMachineGuid();
70+
}
71+
else
72+
{
73+
guid = machine_guid;
74+
}
75+
76+
std::string salt;
77+
if (salt_string == nullptr)
78+
{
79+
salt = UserPreferences::Common::GetDefaultSalt();
80+
}
81+
else
82+
{
83+
salt = salt_string;
84+
}
85+
86+
const auto plaintext = std::vector<uint8_t>(&plaintext_data[0], &plaintext_data[plaintext_data_size]);
87+
const auto encrypted = UserPreferences::Encryption::EncryptData(plaintext, guid, salt);
88+
89+
if (out_encrypted_buffer == nullptr || *out_encrypted_buffer_size < encrypted.size())
90+
{
91+
*out_encrypted_buffer_size = encrypted.size();
92+
return kResult_BufferTooSmall;
93+
}
94+
95+
*out_encrypted_buffer_size = encrypted.size();
96+
memcpy(out_encrypted_buffer, &encrypted[0], *out_encrypted_buffer_size);
97+
return kResult_GeneralSuccess;
98+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
#define DllExport __declspec( dllexport )
3+
4+
#include <sal.h>
5+
#include <cstdint>
6+
7+
extern "C"
8+
{
9+
enum APIResult {
10+
kResult_GeneralSuccess = 0,
11+
kResult_GeneralFailure = -1,
12+
kResult_BufferTooSmall = -2
13+
};
14+
15+
/// <summary>
16+
/// Decrypts encrypted data
17+
/// </summary>
18+
/// <param name="encrypted_buffer">Encrypted bytes buffer to decrypt</param>
19+
/// <param name="encrypted_buffer_size">Encrypted bytes buffer size in bytes</param>
20+
/// <param name="machine_guid">Optional machine GUID. Defaults to current machine GUID if not supplied.</param>
21+
/// <param name="salt_string">Optional salt. Defaults to last known salt if not supplied.</param>
22+
/// <param name="out_decrypted_buffer">Optional decrypted buffer</param>
23+
/// <param name="out_encrypted_buffer_size">Decrypted buffer size in bytes. Will be set to the required size if decrypted buffer is NULL. </param>
24+
/// <returns>
25+
/// kResult_GeneralSuccess on success.
26+
/// kResult_BufferTooSmall if decrypted buffer size is too small or decrypted buffer was not provided.
27+
/// kResult_GeneralFailure on failure.
28+
///</returns>
29+
DllExport APIResult DecryptData(
30+
_In_ const uint8_t *encrypted_buffer,
31+
_In_ size_t encrypted_buffer_size,
32+
_In_opt_ const char *machine_guid,
33+
_In_opt_ const char *salt_string,
34+
_Out_opt_ uint8_t *out_decrypted_buffer,
35+
_Inout_ size_t *out_decrypted_buffer_size
36+
);
37+
38+
/// <summary>
39+
/// Encrypts data
40+
/// </summary>
41+
/// <param name="plaintext_data">Plaintext to enctypt</param>
42+
/// <param name="plaintext_data_size">Plaintext size in bytes</param>
43+
/// <param name="machine_guid">Optional machine GUID. Defaults to current machine GUID if not supplied.</param>
44+
/// <param name="salt_string">Optional salt. Defaults to last known salt if not supplied.</param>
45+
/// <param name="out_encrypted_buffer">Optional enctypted buffer</param>
46+
/// <param name="out_encrypted_buffer_size">Encrypted buffer size in bytes. Will be set to the required size if enctypted buffer is NULL. </param>
47+
/// <returns>
48+
/// kResult_GeneralSuccess on success.
49+
/// kResult_BufferTooSmall if encrypted buffer size is too small or encrypted buffer was not provided.
50+
/// kResult_GeneralFailure on failure.
51+
///</returns>
52+
DllExport APIResult EncryptData(
53+
_In_ const uint8_t *plaintext_data,
54+
_In_ size_t plaintext_data_size,
55+
_In_opt_ const char *machine_guid,
56+
_In_opt_ const char *salt_string,
57+
_Out_opt_ uint8_t *out_encrypted_buffer,
58+
_Inout_ size_t *out_encrypted_buffer_size
59+
);
60+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>15.0</VCProjectVersion>
23+
<ProjectGuid>{E8A8F157-EBAE-43E4-90AE-32A8DAC51096}</ProjectGuid>
24+
<Keyword>Win32Proj</Keyword>
25+
<RootNamespace>LibUserPreferences</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>DynamicLibrary</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v141</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>DynamicLibrary</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v141</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>DynamicLibrary</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v141</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>DynamicLibrary</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v141</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
77+
<LinkIncremental>true</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
80+
<LinkIncremental>false</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
</PropertyGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<Optimization>Disabled</Optimization>
89+
<SDLCheck>true</SDLCheck>
90+
<PreprocessorDefinitions>_DEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
91+
<ConformanceMode>true</ConformanceMode>
92+
</ClCompile>
93+
<Link>
94+
<GenerateDebugInformation>true</GenerateDebugInformation>
95+
<SubSystem>Windows</SubSystem>
96+
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
97+
</Link>
98+
</ItemDefinitionGroup>
99+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
100+
<ClCompile>
101+
<WarningLevel>Level3</WarningLevel>
102+
<Optimization>Disabled</Optimization>
103+
<SDLCheck>true</SDLCheck>
104+
<PreprocessorDefinitions>WIN32;_DEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
105+
<ConformanceMode>true</ConformanceMode>
106+
</ClCompile>
107+
<Link>
108+
<GenerateDebugInformation>true</GenerateDebugInformation>
109+
<SubSystem>Windows</SubSystem>
110+
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
111+
</Link>
112+
</ItemDefinitionGroup>
113+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
114+
<ClCompile>
115+
<WarningLevel>Level3</WarningLevel>
116+
<Optimization>MaxSpeed</Optimization>
117+
<FunctionLevelLinking>true</FunctionLevelLinking>
118+
<IntrinsicFunctions>true</IntrinsicFunctions>
119+
<SDLCheck>true</SDLCheck>
120+
<PreprocessorDefinitions>WIN32;NDEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
121+
<ConformanceMode>true</ConformanceMode>
122+
</ClCompile>
123+
<Link>
124+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
125+
<OptimizeReferences>true</OptimizeReferences>
126+
<GenerateDebugInformation>true</GenerateDebugInformation>
127+
<SubSystem>Windows</SubSystem>
128+
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
129+
</Link>
130+
</ItemDefinitionGroup>
131+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
132+
<ClCompile>
133+
<WarningLevel>Level3</WarningLevel>
134+
<Optimization>MaxSpeed</Optimization>
135+
<FunctionLevelLinking>true</FunctionLevelLinking>
136+
<IntrinsicFunctions>true</IntrinsicFunctions>
137+
<SDLCheck>true</SDLCheck>
138+
<PreprocessorDefinitions>NDEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
139+
<ConformanceMode>true</ConformanceMode>
140+
</ClCompile>
141+
<Link>
142+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
143+
<OptimizeReferences>true</OptimizeReferences>
144+
<GenerateDebugInformation>true</GenerateDebugInformation>
145+
<SubSystem>Windows</SubSystem>
146+
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
147+
</Link>
148+
</ItemDefinitionGroup>
149+
<ItemGroup>
150+
<ClInclude Include="LibUserPreferences.h" />
151+
</ItemGroup>
152+
<ItemGroup>
153+
<ClCompile Include="LibUserPreferences.cpp" />
154+
</ItemGroup>
155+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
156+
<ImportGroup Label="ExtensionTargets">
157+
</ImportGroup>
158+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="LibUserPreferences.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
</ItemGroup>
22+
<ItemGroup>
23+
<ClCompile Include="LibUserPreferences.cpp">
24+
<Filter>Source Files</Filter>
25+
</ClCompile>
26+
</ItemGroup>
27+
</Project>
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
#include <unordered_map>
55

6-
#include "../UserPreferencesExplorer/Encryption.cpp"
7-
#include "../UserPreferencesExplorer/Utils.cpp"
6+
#include "../UserPreferences.Shared/Encryption.h"
7+
#include "../UserPreferences.Shared/Common.h"
8+
9+
#include "../UserPreferences.Shared/Encryption.cpp"
10+
#include "../UserPreferences.Shared/Common.cpp"
811

912
TEST_CASE("Encryption")
1013
{
@@ -26,14 +29,14 @@ TEST_CASE("Encryption")
2629
auto data_to_encrypt = std::vector<uint8_t>(plaintext.begin(), plaintext.end());
2730
data_to_encrypt.push_back('\0');
2831

29-
auto result = Encryption::EncryptData(data_to_encrypt, guid, salt);
32+
auto result = UserPreferences::Encryption::EncryptData(data_to_encrypt, guid, salt);
3033

3134
REQUIRE(result == ciphertext);
3235
}
3336

3437
SECTION("Decrypt")
3538
{
36-
auto result = Encryption::DecryptData(ciphertext, guid, salt);
39+
auto result = UserPreferences::Encryption::DecryptData(ciphertext, guid, salt);
3740
auto result_string = std::string(result.begin(), result.end() - 1);
3841

3942
REQUIRE(result_string == plaintext);

UserPreferencesExplorer.Tests/UserPreferencesExplorer.Tests.vcxproj.filters renamed to UserPreferences.Shared.Tests/UserPreferences.Shared.Tests.filters

File renamed without changes.

0 commit comments

Comments
 (0)