Skip to content

Commit 2d0bda4

Browse files
author
Adam Nierzad
authored
Merge pull request #67 from cantarus/development
Development
2 parents c73eed2 + f590517 commit 2d0bda4

File tree

73 files changed

+2538
-448
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2538
-448
lines changed

DeployClient/API.cs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class API
1616

1717
private static HttpClient BuildClient()
1818
{
19+
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
20+
1921
HttpClient client = new HttpClient()
2022
{
2123
BaseAddress = new Uri(new Uri(Program.Options.TargetUri), "DesktopModules/PolyDeploy/API/")

DeployClient/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("0.8.0.0")]
36-
[assembly: AssemblyFileVersion("0.8.0.0")]
35+
[assembly: AssemblyVersion("0.9.3.0")]
36+
[assembly: AssemblyFileVersion("0.9.3.0")]

DeployClient/gulpfile.js

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ gulp.task('post-build-Release', [
3333
'build-release'
3434
]);
3535

36+
gulp.task('pre-build-Debug');
37+
gulp.task('post-build-Debug');
38+
39+
gulp.task('pre-build-Clients');
40+
gulp.task('post-build-Clients');
41+
3642
/*
3743
Clean Bin
3844
You can't rely on Visual Studio to clean the bin folder, even when calling

DeployClient/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deploy-client",
3-
"version": "0.8.0",
3+
"version": "0.9.3",
44
"main": "gulpfile.js",
55
"license": "Apache-2.0",
66
"private": true,

DeployClient/project.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
MODULE_VERSION: '00.08.00'
2+
MODULE_VERSION: '00.09.03'
33
};

Encryption/Crypto.cs

+33-21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public class Crypto
1717
// This is divided by 8 later to get the equivalent number of bytes.
1818
private const int KeySize = 256;
1919

20+
// The AES specification states that the block size must be 128.
21+
private const int BlockSize = 128;
22+
23+
// Initialisation vector size.
24+
private const int IvSize = 128;
25+
26+
// Salt size.
27+
private const int SaltSize = 256;
28+
2029
// Determines the number of iterations used during password generation.
2130
private const int DerivationIterations = 1000;
2231

@@ -60,8 +69,8 @@ public static string Encrypt(string plainText, string passPhrase)
6069
public static byte[] Encrypt(byte[] plainBytes, string passPhrase)
6170
{
6271
// Bytes for salt and initialisation vector are generated randomly each time.
63-
byte[] saltBytes = Generate256BitsOfRandomEntropy();
64-
byte[] ivBytes = Generate256BitsOfRandomEntropy();
72+
byte[] saltBytes = GenerateRandomEntropy(SaltSize);
73+
byte[] ivBytes = GenerateRandomEntropy(IvSize);
6574

6675
// Prepare store for encrypted bytes.
6776
byte[] encryptedBytes;
@@ -70,9 +79,9 @@ public static byte[] Encrypt(byte[] plainBytes, string passPhrase)
7079
{
7180
byte[] keyBytes = password.GetBytes(KeySize / 8);
7281

73-
using (RijndaelManaged symmetricKey = new RijndaelManaged())
82+
using (AesManaged symmetricKey = new AesManaged())
7483
{
75-
symmetricKey.BlockSize = 256;
84+
symmetricKey.BlockSize = BlockSize;
7685
symmetricKey.Mode = CipherMode.CBC;
7786
symmetricKey.Padding = PaddingMode.PKCS7;
7887

@@ -144,14 +153,22 @@ public static string Decrypt(string encryptedText, string passPhrase)
144153

145154
public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhrase)
146155
{
147-
// Get the salt bytes by extracting the first 32 bytes.
148-
byte[] saltBytes = encryptedBytesWithSaltAndIv.Take(KeySize / 8).ToArray();
149-
150-
// Get the initialisation vector bytes by extracting the next 32 bytes after the salt.
151-
byte[] ivBytes = encryptedBytesWithSaltAndIv.Skip(KeySize / 8).Take(KeySize / 8).ToArray();
152-
153-
// Get the actual encrypted bytes by removing the first 64 bytes.
154-
byte[] encryptedBytes = encryptedBytesWithSaltAndIv.Skip((KeySize / 8) * 2).Take(encryptedBytesWithSaltAndIv.Length - ((KeySize / 8) * 2)).ToArray();
156+
// Get the salt bytes by extracting the first (SaltSize / 8) bytes.
157+
byte[] saltBytes = encryptedBytesWithSaltAndIv
158+
.Take(SaltSize / 8)
159+
.ToArray();
160+
161+
// Get the initialisation vector bytes by extracting the next (IvSize / 8) bytes after the salt.
162+
byte[] ivBytes = encryptedBytesWithSaltAndIv
163+
.Skip(SaltSize / 8)
164+
.Take(IvSize / 8)
165+
.ToArray();
166+
167+
// Get the actual encrypted bytes by removing the salt and iv bytes.
168+
byte[] encryptedBytes = encryptedBytesWithSaltAndIv
169+
.Skip((SaltSize / 8) + (IvSize / 8))
170+
.Take(encryptedBytesWithSaltAndIv.Length - ((SaltSize / 8) + (IvSize / 8)))
171+
.ToArray();
155172

156173
// Prepare store for decrypted string and bytes read.
157174
byte[] plainTextBytes;
@@ -161,9 +178,9 @@ public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhra
161178
{
162179
byte[] keyBytes = password.GetBytes(KeySize / 8);
163180

164-
using (RijndaelManaged symmetricKey = new RijndaelManaged())
181+
using (AesManaged symmetricKey = new AesManaged())
165182
{
166-
symmetricKey.BlockSize = 256;
183+
symmetricKey.BlockSize = BlockSize;
167184
symmetricKey.Mode = CipherMode.CBC;
168185
symmetricKey.Padding = PaddingMode.PKCS7;
169186

@@ -187,14 +204,9 @@ public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIv, string passPhra
187204
return plainTextBytes.Take(decryptedByteCount).ToArray();
188205
}
189206

190-
private static byte[] Generate256BitsOfRandomEntropy()
207+
private static byte[] GenerateRandomEntropy(int bitCount)
191208
{
192-
byte[] randomBytes = new byte[32];
193-
194-
using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
195-
{
196-
rngCsp.GetBytes(randomBytes);
197-
}
209+
byte[] randomBytes = CryptoUtilities.GenerateRandomBytes(bitCount / 8);
198210

199211
return randomBytes;
200212
}

Encryption/CryptoUtilities.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Security.Cryptography;
2+
using System.Text;
3+
4+
namespace Cantarus.Libraries.Encryption
5+
{
6+
/// <summary>
7+
/// Provides useful utility methods which may not easily be grouped
8+
/// elsewhere.
9+
/// </summary>
10+
public static class CryptoUtilities
11+
{
12+
/// <summary>
13+
/// Generates a byte array of the length specified filled with random
14+
/// bytes.
15+
/// </summary>
16+
/// <param name="length"></param>
17+
/// <returns></returns>
18+
public static byte[] GenerateRandomBytes(int length)
19+
{
20+
// Create a new byte array of the size required.
21+
byte[] bytes = new byte[length];
22+
23+
using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider())
24+
{
25+
// Fill it with random bytes.
26+
rngCsp.GetBytes(bytes);
27+
}
28+
29+
return bytes;
30+
}
31+
32+
public static string SHA256HashString(string value)
33+
{
34+
byte[] bytes = SHA256HashBytes(value);
35+
36+
string hash = "";
37+
38+
for(int i = 0; i< bytes.Length; i++)
39+
{
40+
hash = string.Format("{0}{1:X2}", hash, bytes[i]);
41+
}
42+
43+
return hash;
44+
}
45+
46+
/// <summary>
47+
/// Hashes the passed value using the SHA256 algorithm.
48+
/// </summary>
49+
/// <param name="value"></param>
50+
/// <returns></returns>
51+
public static byte[] SHA256HashBytes(string value)
52+
{
53+
// Convert string to byte array.
54+
byte[] bytes = Encoding.UTF8.GetBytes(value);
55+
56+
byte[] hashedBytes;
57+
58+
using (SHA256 sha = new SHA256Managed())
59+
{
60+
// Hash bytes.
61+
hashedBytes = sha.ComputeHash(bytes);
62+
}
63+
64+
return hashedBytes;
65+
}
66+
}
67+
}

Encryption/Encryption.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<ItemGroup>
4444
<Compile Include="Crypto.cs" />
4545
<Compile Include="Properties\AssemblyInfo.cs" />
46+
<Compile Include="CryptoUtilities.cs" />
4647
</ItemGroup>
4748
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4849
</Project>

EncryptionTests/EncryptionTests.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Cantarus.Libraries.Encryption;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace EncryptionTests
7+
{
8+
[TestClass]
9+
public class EncryptionTests
10+
{
11+
private const int Iterations = 100;
12+
13+
[TestMethod]
14+
public void EncryptString_RandomString_ObfuscatedAfterEncryption()
15+
{
16+
for (int i = 0; i < Iterations; i++)
17+
{
18+
string passPhrase = TestUtilities.GeneratePassPhrase();
19+
string beforeString = Encoding.UTF8.GetString(TestUtilities.GeneratePayload());
20+
21+
string encryptedString = Crypto.Encrypt(beforeString, passPhrase);
22+
23+
Assert.AreNotEqual(beforeString, encryptedString);
24+
}
25+
}
26+
27+
[TestMethod]
28+
public void EncryptBytes_RandomBytes_ObfuscatedAfterEncryption()
29+
{
30+
for (int i = 0; i < Iterations; i++)
31+
{
32+
string passPhrase = TestUtilities.GeneratePassPhrase();
33+
byte[] beforeBytes = TestUtilities.GeneratePayload();
34+
35+
byte[] encryptedBytes = Crypto.Encrypt(beforeBytes, passPhrase);
36+
37+
CollectionAssert.AreNotEqual(beforeBytes, encryptedBytes);
38+
}
39+
}
40+
41+
[TestMethod]
42+
public void EncryptStream_StreamOfRandomBytes_ObfuscatedAfterEncryption()
43+
{
44+
for (int i = 0; i < Iterations; i++)
45+
{
46+
string passPhrase = TestUtilities.GeneratePassPhrase();
47+
byte[] beforeBytes = TestUtilities.GeneratePayload();
48+
byte[] encryptedBytes;
49+
50+
using (MemoryStream plainStream = new MemoryStream(beforeBytes))
51+
{
52+
using (MemoryStream encryptedSteam = (MemoryStream)Crypto.Encrypt(plainStream, passPhrase))
53+
{
54+
encryptedBytes = encryptedSteam.ToArray();
55+
}
56+
}
57+
58+
CollectionAssert.AreNotEqual(beforeBytes, encryptedBytes);
59+
}
60+
}
61+
}
62+
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{566818D4-11AE-4C96-821E-91C1C616D19B}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>EncryptionTests</RootNamespace>
11+
<AssemblyName>EncryptionTests</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
16+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
17+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
18+
<IsCodedUITest>False</IsCodedUITest>
19+
<TestProjectType>UnitTest</TestProjectType>
20+
<NuGetPackageImportStamp>
21+
</NuGetPackageImportStamp>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
24+
<DebugSymbols>true</DebugSymbols>
25+
<DebugType>full</DebugType>
26+
<Optimize>false</Optimize>
27+
<OutputPath>bin\Debug\</OutputPath>
28+
<DefineConstants>DEBUG;TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
33+
<DebugType>pdbonly</DebugType>
34+
<Optimize>true</Optimize>
35+
<OutputPath>bin\Release\</OutputPath>
36+
<DefineConstants>TRACE</DefineConstants>
37+
<ErrorReport>prompt</ErrorReport>
38+
<WarningLevel>4</WarningLevel>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
42+
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45+
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System" />
48+
<Reference Include="System.Core" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Compile Include="EncryptionTests.cs" />
52+
<Compile Include="TestUtilities.cs" />
53+
<Compile Include="RoundTripTests.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="packages.config" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<ProjectReference Include="..\Encryption\Encryption.csproj">
61+
<Project>{ab5a3320-f260-42ee-8f19-ccf7546ca511}</Project>
62+
<Name>Encryption</Name>
63+
</ProjectReference>
64+
</ItemGroup>
65+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
68+
<PropertyGroup>
69+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
70+
</PropertyGroup>
71+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props'))" />
72+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets'))" />
73+
</Target>
74+
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" />
75+
</Project>
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
[assembly: AssemblyTitle("EncryptionTests")]
6+
[assembly: AssemblyDescription("")]
7+
[assembly: AssemblyConfiguration("")]
8+
[assembly: AssemblyCompany("")]
9+
[assembly: AssemblyProduct("EncryptionTests")]
10+
[assembly: AssemblyCopyright("Copyright © 2019")]
11+
[assembly: AssemblyTrademark("")]
12+
[assembly: AssemblyCulture("")]
13+
14+
[assembly: ComVisible(false)]
15+
16+
[assembly: Guid("566818d4-11ae-4c96-821e-91c1c616d19b")]
17+
18+
// [assembly: AssemblyVersion("1.0.*")]
19+
[assembly: AssemblyVersion("1.0.0.0")]
20+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)