Skip to content

Commit 3c05ffa

Browse files
committed
Hash encryption.
1 parent 46cad40 commit 3c05ffa

19 files changed

Lines changed: 644 additions & 0 deletions

GodSharp.Encryption.sln

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26228.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{684FCB96-A6FC-45B4-8BE7-13C03D1F464C}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B76AC3AF-1E8E-4849-9943-67DF77651A6F}"
9+
EndProject
10+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "GodSharp.Shared", "src\GodSharp.Shared\GodSharp.Shared.shproj", "{8F6E52F3-DB35-40B8-867A-AD312791BB63}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GodSharp.Encryption", "src\GodSharp.Encryption\GodSharp.Encryption.csproj", "{778633B1-7023-4975-8028-EB8645CD9BA9}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{3889C1B2-746D-40F8-90F3-5F1E819DBFBB}"
15+
EndProject
16+
Global
17+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
18+
src\GodSharp.Shared\GodSharp.Shared.projitems*{8f6e52f3-db35-40b8-867a-ad312791bb63}*SharedItemsImports = 13
19+
EndGlobalSection
20+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
21+
Debug|Any CPU = Debug|Any CPU
22+
Release|Any CPU = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
25+
{778633B1-7023-4975-8028-EB8645CD9BA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{778633B1-7023-4975-8028-EB8645CD9BA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{778633B1-7023-4975-8028-EB8645CD9BA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{778633B1-7023-4975-8028-EB8645CD9BA9}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
GlobalSection(NestedProjects) = preSolution
34+
{8F6E52F3-DB35-40B8-867A-AD312791BB63} = {3889C1B2-746D-40F8-90F3-5F1E819DBFBB}
35+
{778633B1-7023-4975-8028-EB8645CD9BA9} = {684FCB96-A6FC-45B4-8BE7-13C03D1F464C}
36+
{3889C1B2-746D-40F8-90F3-5F1E819DBFBB} = {684FCB96-A6FC-45B4-8BE7-13C03D1F464C}
37+
EndGlobalSection
38+
EndGlobal
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{778633B1-7023-4975-8028-EB8645CD9BA9}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>GodSharp</RootNamespace>
11+
<AssemblyName>GodSharp.Encryption</AssemblyName>
12+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Data" />
37+
</ItemGroup>
38+
<ItemGroup>
39+
<Compile Include="..\GodSharp.Shared\Encryption\Asymmetric\RSA.cs">
40+
<Link>Asymmetric\RSA.cs</Link>
41+
</Compile>
42+
<Compile Include="..\GodSharp.Shared\Encryption\Hash\Base64.cs">
43+
<Link>Hash\Base64.cs</Link>
44+
</Compile>
45+
<Compile Include="..\GodSharp.Shared\Encryption\Hash\MD5.cs">
46+
<Link>Hash\MD5.cs</Link>
47+
</Compile>
48+
<Compile Include="..\GodSharp.Shared\Encryption\Hash\SHA1.cs">
49+
<Link>Hash\SHA1.cs</Link>
50+
</Compile>
51+
<Compile Include="..\GodSharp.Shared\Encryption\Hash\SHA256.cs">
52+
<Link>Hash\SHA256.cs</Link>
53+
</Compile>
54+
<Compile Include="..\GodSharp.Shared\Encryption\Hash\SHA384.cs">
55+
<Link>Hash\SHA384.cs</Link>
56+
</Compile>
57+
<Compile Include="..\GodSharp.Shared\Encryption\Hash\SHA512.cs">
58+
<Link>Hash\SHA512.cs</Link>
59+
</Compile>
60+
<Compile Include="..\GodSharp.Shared\Encryption\Symmetric\AES.cs">
61+
<Link>Symmetric\AES.cs</Link>
62+
</Compile>
63+
<Compile Include="..\GodSharp.Shared\Encryption\Symmetric\DES.cs">
64+
<Link>Symmetric\DES.cs</Link>
65+
</Compile>
66+
<Compile Include="Properties\AssemblyInfo.cs" />
67+
</ItemGroup>
68+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
69+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("GodSharp.Cryptography")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("GodSharp.Cryptography")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("778633b1-7023-4975-8028-eb8645cd9ba9")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace GodSharp.Encryption
5+
{
6+
/// <summary>
7+
/// Hash/Base64 encryption.
8+
/// </summary>
9+
public class Base64
10+
{
11+
/// <summary>
12+
/// Base64 encryption.
13+
/// </summary>
14+
/// <param name="data">The string to be encrypted,not null.</param>
15+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
16+
/// <returns>The encrypted string.</returns>
17+
public static string Encrypt(string data, Encoding encoding = null)
18+
{
19+
if (data == null)
20+
{
21+
throw new ArgumentNullException(nameof(data));
22+
}
23+
if (encoding == null)
24+
{
25+
encoding = Encoding.UTF8;
26+
}
27+
28+
return Convert.ToBase64String(encoding.GetBytes(data));
29+
}
30+
31+
/// <summary>
32+
/// Base64 decryption.
33+
/// </summary>
34+
/// <param name="data">The string to be decrypted,not null.</param>
35+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
36+
/// <returns>The decrypted string.</returns>
37+
public static string Decrypt(string data, Encoding encoding = null)
38+
{
39+
if (data == null)
40+
{
41+
throw new ArgumentNullException(nameof(data));
42+
}
43+
if (encoding == null)
44+
{
45+
encoding = Encoding.UTF8;
46+
}
47+
48+
return encoding.GetString(Convert.FromBase64String(data));
49+
}
50+
}
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
5+
namespace GodSharp.Encryption
6+
{
7+
/// <summary>
8+
/// Hash/HMAC encryption.
9+
/// </summary>
10+
public abstract class HMAC
11+
{
12+
/// <summary>
13+
/// HMAC encryption.
14+
/// </summary>
15+
/// <typeparam name="T">The <see cref="T:System.Security.Cryptography.HMAC"/> sub-class.</typeparam>
16+
/// <param name="data">The string to be encrypted,not null.</param>
17+
/// <param name="key">Encryption key,not null.</param>
18+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
19+
/// <returns>The encrypted string.</returns>
20+
internal static string Encrypt<T>(string data, string key,Encoding encoding=null) where T : KeyedHashAlgorithm, new()
21+
{
22+
if (data==null)
23+
{
24+
throw new ArgumentNullException(nameof(data));
25+
}
26+
if (key==null)
27+
{
28+
throw new ArgumentNullException(nameof(key));
29+
}
30+
if (encoding==null)
31+
{
32+
encoding = Encoding.UTF8;
33+
}
34+
35+
byte[] keys = encoding.GetBytes(key);
36+
byte[] datas = encoding.GetBytes(data);
37+
38+
using (T hash = new T())
39+
{
40+
hash.Key = keys;
41+
byte[] result = hash.ComputeHash(datas);
42+
return Convert.ToBase64String(result);
43+
}
44+
}
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
3+
namespace GodSharp.Encryption
4+
{
5+
/// <summary>
6+
/// Hash/HMAC/HMACMD5 encryption.
7+
/// </summary>
8+
public class HMACMD5 : HMAC
9+
{
10+
/// <summary>
11+
/// HMAC encryption.
12+
/// </summary>
13+
/// <param name="data">The string to be encrypted,not null.</param>
14+
/// <param name="key">Encryption key,not null.</param>
15+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
16+
/// <returns>The encrypted string.</returns>
17+
public static string Encrypt(string data, string key, Encoding encoding = null)
18+
{
19+
return Encrypt<System.Security.Cryptography.HMACMD5>(data, key, encoding);
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
3+
namespace GodSharp.Encryption
4+
{
5+
/// <summary>
6+
/// Hash/HMAC/HMACRIPEMD160 encryption.
7+
/// </summary>
8+
public class HMACRIPEMD160:HMAC
9+
{
10+
/// <summary>
11+
/// HMACRIPEMD160 encryption.
12+
/// </summary>
13+
/// <param name="data">The string to be encrypted,not null.</param>
14+
/// <param name="key">Encryption key,not null.</param>
15+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
16+
/// <returns>The encrypted string.</returns>
17+
public static string Encrypt(string data, string key, Encoding encoding = null)
18+
{
19+
return Encrypt<System.Security.Cryptography.HMACRIPEMD160>(data, key, encoding);
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
3+
namespace GodSharp.Encryption
4+
{
5+
/// <summary>
6+
/// Hash/HMAC/HMACSHA1 encryption.
7+
/// </summary>
8+
public class HMACSHA1: HMAC
9+
{
10+
/// <summary>
11+
/// HMACSHA1 encryption.
12+
/// </summary>
13+
/// <param name="data">The string to be encrypted,not null.</param>
14+
/// <param name="key">Encryption key,not null.</param>
15+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
16+
/// <returns>The encrypted string.</returns>
17+
public static string Encrypt(string data,string key, Encoding encoding = null)
18+
{
19+
return Encrypt<System.Security.Cryptography.HMACSHA1>(data, key, encoding);
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
3+
namespace GodSharp.Encryption
4+
{
5+
/// <summary>
6+
/// Hash/HMAC/HMACSHA256 encryption.
7+
/// </summary>
8+
public class HMACSHA256: HMAC
9+
{
10+
/// <summary>
11+
/// HMACSHA256 encryption.
12+
/// </summary>
13+
/// <param name="data">The string to be encrypted,not null.</param>
14+
/// <param name="key">Encryption key,not null.</param>
15+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
16+
/// <returns>The encrypted string.</returns>
17+
public static string Encrypt(string data, string key, Encoding encoding = null)
18+
{
19+
return Encrypt<System.Security.Cryptography.HMACSHA256>(data, key, encoding);
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text;
2+
3+
namespace GodSharp.Encryption
4+
{
5+
/// <summary>
6+
/// Hash/HMAC/HMACSHA384 encryption.
7+
/// </summary>
8+
public class HMACSHA384 : HMAC
9+
{
10+
/// <summary>
11+
/// HMACSHA384 encryption.
12+
/// </summary>
13+
/// <param name="data">The string to be encrypted,not null.</param>
14+
/// <param name="key">Encryption key,not null.</param>
15+
/// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
16+
/// <returns>The encrypted string.</returns>
17+
public static string Encrypt(string data, string key, Encoding encoding = null)
18+
{
19+
return Encrypt<System.Security.Cryptography.HMACSHA384>(data, key, encoding);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)