Skip to content

Commit 4126125

Browse files
stephenhaunts_cpstephenhaunts_cp
authored andcommitted
Refactored common code from the 1.1 and 1.2 file loader classes into a separate base class.
1 parent 3cba1fe commit 4126125

File tree

5 files changed

+108
-139
lines changed

5 files changed

+108
-139
lines changed

ChangeLog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Version 1.4
77

88
[X] - Upgraded standard document version number to 1.2. The document now saves with a default Password hashing work factor of 100,000 instead of 40,000
99

10-
10+
[X] - Refactored common code from the 1.1 and 1.2 file loader classes into a separate base class.
1111

1212
Version 1.3
1313
------------
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+

2+
3+
using System;
4+
using HauntedHouseSoftware.SecureNotePad.CryptoProviders;
5+
6+
/**
7+
* Safe Pad, a double encrypted note pad that uses 2 passwords to protect your documents and help you keep your privacy.
8+
*
9+
* Copyright (C) 2016 Stephen Haunts
10+
* http://www.stephenhaunts.com
11+
*
12+
* This file is part of Safe Pad.
13+
*
14+
* Safe Pad is free software: you can redistribute it and/or modify it under the terms of the
15+
* GNU General Public License as published by the Free Software Foundation, either version 2 of the
16+
* License, or (at your option) any later version.
17+
*
18+
* Safe Pad is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20+
*
21+
* See the GNU General Public License for more details <http://www.gnu.org/licenses/>.
22+
*
23+
* Authors: Stephen Haunts
24+
*/
25+
namespace HauntedHouseSoftware.SecureNotePad.DomainObjects.FileFormat
26+
{
27+
public class FileLoaderBase
28+
{
29+
protected IAES _aes;
30+
protected ISecureHash _secureHash;
31+
protected IPassword _password;
32+
protected ICompression _compression;
33+
34+
public FileLoaderBase(IPassword password)
35+
{
36+
if (password == null)
37+
{
38+
throw new ArgumentNullException("password");
39+
}
40+
41+
_aes = new AES();
42+
_secureHash = new SecureHash();
43+
_password = password;
44+
_compression = new GZipCompression();
45+
}
46+
47+
public byte[] Load(byte[] byteStream, int workFactor)
48+
{
49+
if (byteStream == null)
50+
{
51+
throw new ArgumentNullException("byteStream");
52+
}
53+
54+
var versionNumber = ByteHelpers.CreateSpecialByteArray(2);
55+
var hash = ByteHelpers.CreateSpecialByteArray(32);
56+
var salt = ByteHelpers.CreateSpecialByteArray(32);
57+
var encrypted = ByteHelpers.CreateSpecialByteArray((byteStream.Length - 66));
58+
59+
SplitFileIntoChunks(byteStream, versionNumber, hash, salt, encrypted);
60+
CheckFileIntegrity(hash, encrypted);
61+
62+
return DecryptData(encrypted, salt, workFactor);
63+
}
64+
65+
private void CheckFileIntegrity(byte[] hash, byte[] encrypted)
66+
{
67+
var computedhash = _secureHash.ComputeHash(encrypted);
68+
69+
if (!ByteHelpers.ByteArrayCompare(computedhash, hash))
70+
{
71+
throw new InvalidOperationException("The signature of the file does not match.");
72+
}
73+
}
74+
75+
private byte[] DecryptData(byte[] encrypted, byte[] salt, int workFactor)
76+
{
77+
78+
var decrypted = _aes.Decrypt(encrypted, Convert.ToBase64String(_password.CombinedPasswords), salt, workFactor);
79+
80+
var decompressed = _compression.Decompress(decrypted);
81+
82+
return decompressed;
83+
}
84+
85+
private static void SplitFileIntoChunks(byte[] buffer, byte[] versionNumber, byte[] hash, byte[] salt, byte[] encrypted)
86+
{
87+
int offset = 0;
88+
Buffer.BlockCopy(buffer, offset, versionNumber, 0, 2);
89+
offset += 2;
90+
91+
Buffer.BlockCopy(buffer, offset, salt, 0, 32);
92+
offset += 32;
93+
94+
Buffer.BlockCopy(buffer, offset, hash, 0, 32);
95+
offset += 32;
96+
97+
Buffer.BlockCopy(buffer, offset, encrypted, 0, encrypted.Length);
98+
}
99+
}
100+
}

Safe Pad Client Library/DomainObjects/FileFormat/Version11Loader.cs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,82 +17,16 @@
1717
*
1818
* Authors: Stephen Haunts
1919
*/
20-
using System;
21-
using HauntedHouseSoftware.SecureNotePad.CryptoProviders;
2220

2321
namespace HauntedHouseSoftware.SecureNotePad.DomainObjects.FileFormat
2422
{
25-
public class Version11Loader : IFileFormatLoader
23+
public class Version11Loader : FileLoaderBase, IFileFormatLoader
2624
{
27-
private readonly IAES _aes;
28-
private readonly ISecureHash _secureHash;
29-
private readonly IPassword _password;
30-
private readonly ICompression _compression;
31-
32-
public Version11Loader(IPassword password)
33-
{
34-
if (password == null)
35-
{
36-
throw new ArgumentNullException(nameof(password));
37-
}
38-
39-
_aes = new AES();
40-
_secureHash = new SecureHash();
41-
_password = password;
42-
_compression = new GZipCompression();
43-
}
25+
public Version11Loader(IPassword password) : base(password) { }
4426

4527
public byte[] Load(byte[] byteStream)
4628
{
47-
if (byteStream == null)
48-
{
49-
throw new ArgumentNullException(nameof(byteStream));
50-
}
51-
52-
var versionNumber = ByteHelpers.CreateSpecialByteArray(2);
53-
var hash = ByteHelpers.CreateSpecialByteArray(32);
54-
var salt = ByteHelpers.CreateSpecialByteArray(32);
55-
var encrypted = ByteHelpers.CreateSpecialByteArray((byteStream.Length - 66));
56-
57-
SplitFileIntoChunks(byteStream, versionNumber, hash, salt, encrypted);
58-
CheckFileIntegrity(hash, encrypted);
59-
60-
return DecryptData(encrypted, salt, 40000);
61-
}
62-
63-
private void CheckFileIntegrity(byte[] hash, byte[] encrypted)
64-
{
65-
var computedhash = _secureHash.ComputeHash(encrypted);
66-
67-
if (!ByteHelpers.ByteArrayCompare(computedhash, hash))
68-
{
69-
throw new InvalidOperationException("The signature of the file does not match.");
70-
}
71-
}
72-
73-
private byte[] DecryptData(byte[] encrypted, byte[] salt, int workFactor)
74-
{
75-
76-
var decrypted = _aes.Decrypt(encrypted, Convert.ToBase64String(_password.CombinedPasswords), salt, workFactor);
77-
78-
var decompressed = _compression.Decompress(decrypted);
79-
80-
return decompressed;
81-
}
82-
83-
private static void SplitFileIntoChunks(byte[] buffer, byte[] versionNumber, byte[] hash, byte[] salt, byte[] encrypted)
84-
{
85-
int offset = 0;
86-
Buffer.BlockCopy(buffer, offset, versionNumber, 0, 2);
87-
offset += 2;
88-
89-
Buffer.BlockCopy(buffer, offset, salt, 0, 32);
90-
offset += 32;
91-
92-
Buffer.BlockCopy(buffer, offset, hash, 0, 32);
93-
offset += 32;
94-
95-
Buffer.BlockCopy(buffer, offset, encrypted, 0, encrypted.Length);
29+
return Load(byteStream, 40000);
9630
}
9731
}
9832
}

Safe Pad Client Library/DomainObjects/FileFormat/Version12Loader.cs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,82 +17,16 @@
1717
*
1818
* Authors: Stephen Haunts
1919
*/
20-
using System;
21-
using HauntedHouseSoftware.SecureNotePad.CryptoProviders;
2220

2321
namespace HauntedHouseSoftware.SecureNotePad.DomainObjects.FileFormat
2422
{
25-
public class Version12Loader : IFileFormatLoader
23+
public class Version12Loader : FileLoaderBase, IFileFormatLoader
2624
{
27-
private readonly IAES _aes;
28-
private readonly ISecureHash _secureHash;
29-
private readonly IPassword _password;
30-
private readonly ICompression _compression;
31-
32-
public Version12Loader(IPassword password)
33-
{
34-
if (password == null)
35-
{
36-
throw new ArgumentNullException("password");
37-
}
38-
39-
_aes = new AES();
40-
_secureHash = new SecureHash();
41-
_password = password;
42-
_compression = new GZipCompression();
43-
}
25+
public Version12Loader(IPassword password) : base(password){ }
4426

4527
public byte[] Load(byte[] byteStream)
4628
{
47-
if (byteStream == null)
48-
{
49-
throw new ArgumentNullException("byteStream");
50-
}
51-
52-
var versionNumber = ByteHelpers.CreateSpecialByteArray(2);
53-
var hash = ByteHelpers.CreateSpecialByteArray(32);
54-
var salt = ByteHelpers.CreateSpecialByteArray(32);
55-
var encrypted = ByteHelpers.CreateSpecialByteArray((byteStream.Length - 66));
56-
57-
SplitFileIntoChunks(byteStream, versionNumber, hash, salt, encrypted);
58-
CheckFileIntegrity(hash, encrypted);
59-
60-
return DecryptData(encrypted, salt, 100000);
61-
}
62-
63-
private void CheckFileIntegrity(byte[] hash, byte[] encrypted)
64-
{
65-
var computedhash = _secureHash.ComputeHash(encrypted);
66-
67-
if (!ByteHelpers.ByteArrayCompare(computedhash, hash))
68-
{
69-
throw new InvalidOperationException("The signature of the file does not match.");
70-
}
71-
}
72-
73-
private byte[] DecryptData(byte[] encrypted, byte[] salt, int workFactor)
74-
{
75-
76-
var decrypted = _aes.Decrypt(encrypted, Convert.ToBase64String(_password.CombinedPasswords), salt, workFactor);
77-
78-
var decompressed = _compression.Decompress(decrypted);
79-
80-
return decompressed;
81-
}
82-
83-
private static void SplitFileIntoChunks(byte[] buffer, byte[] versionNumber, byte[] hash, byte[] salt, byte[] encrypted)
84-
{
85-
int offset = 0;
86-
Buffer.BlockCopy(buffer, offset, versionNumber, 0, 2);
87-
offset += 2;
88-
89-
Buffer.BlockCopy(buffer, offset, salt, 0, 32);
90-
offset += 32;
91-
92-
Buffer.BlockCopy(buffer, offset, hash, 0, 32);
93-
offset += 32;
94-
95-
Buffer.BlockCopy(buffer, offset, encrypted, 0, encrypted.Length);
29+
return Load(byteStream, 100000);
9630
}
9731
}
9832
}

Safe Pad Client Library/Safe Pad Client Library.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="CryptoProviders\SecureHash.cs" />
5757
<Compile Include="DomainObjects\Application Settings\ApplicationSettings.cs" />
5858
<Compile Include="DomainObjects\ByteHelpers.cs" />
59+
<Compile Include="DomainObjects\FileFormat\FileLoaderBase.cs" />
5960
<Compile Include="DomainObjects\FileFormat\Version12Loader.cs" />
6061
<Compile Include="DomainObjects\Interfaces\IFileFormatLoader.cs" />
6162
<Compile Include="DomainObjects\Interfaces\ILoaderFactory.cs" />

0 commit comments

Comments
 (0)