wasSharpNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 4  →  ?path2? @ 5
/Cryptography/SHA1.cs
@@ -5,6 +5,7 @@
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Text;
 
namespace wasSharpNET.Cryptography
{
@@ -18,5 +19,10 @@
{
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", "");
}
 
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data)
{
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", "");
}
}
}
/Network/IPAddressExtensions.cs
@@ -0,0 +1,58 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Net;
 
namespace wasSharpNET.Network
{
/// <summary>
/// IPAddress extensions.
/// </summary>
/// <remarks>https://blogs.msdn.microsoft.com/knom/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks/</remarks>
public static class IPAddressExtensions
{
public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
{
var ipAdressBytes = address.GetAddressBytes();
var subnetMaskBytes = subnetMask.GetAddressBytes();
 
if (ipAdressBytes.Length != subnetMaskBytes.Length)
throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
 
var broadcastAddress = new byte[ipAdressBytes.Length];
for (var i = 0; i < broadcastAddress.Length; i++)
{
broadcastAddress[i] = (byte) (ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
}
return new IPAddress(broadcastAddress);
}
 
public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
{
var ipAdressBytes = address.GetAddressBytes();
var subnetMaskBytes = subnetMask.GetAddressBytes();
 
if (ipAdressBytes.Length != subnetMaskBytes.Length)
throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
 
var broadcastAddress = new byte[ipAdressBytes.Length];
for (var i = 0; i < broadcastAddress.Length; i++)
{
broadcastAddress[i] = (byte) (ipAdressBytes[i] & subnetMaskBytes[i]);
}
return new IPAddress(broadcastAddress);
}
 
public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
{
var network1 = address.GetNetworkAddress(subnetMask);
var network2 = address2.GetNetworkAddress(subnetMask);
 
return network1.Equals(network2);
}
}
}
/Network/SubnetMask.cs
@@ -0,0 +1,64 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Net;
 
namespace wasSharpNET.Network
{
/// <summary>
/// Subnet Mask.
/// </summary>
/// <remarks>https://blogs.msdn.microsoft.com/knom/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks/</remarks>
public static class SubnetMask
{
public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0");
public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0");
public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0");
 
public static IPAddress CreateByHostBitLength(int hostpartLength)
{
var hostPartLength = hostpartLength;
var netPartLength = 32 - hostPartLength;
 
if (netPartLength < 2)
throw new ArgumentException("Number of hosts is to large for IPv4");
 
var binaryMask = new byte[4];
 
for (var i = 0; i < 4; i++)
{
if (i*8 + 8 <= netPartLength)
binaryMask[i] = 255;
else if (i*8 > netPartLength)
binaryMask[i] = 0;
else
{
var oneLength = netPartLength - i*8;
var binaryDigit =
string.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
binaryMask[i] = Convert.ToByte(binaryDigit, 2);
}
}
return new IPAddress(binaryMask);
}
 
public static IPAddress CreateByNetBitLength(int netpartLength)
{
var hostPartLength = 32 - netpartLength;
return CreateByHostBitLength(hostPartLength);
}
 
public static IPAddress CreateByHostNumber(int numberOfHosts)
{
var maxNumber = numberOfHosts + 1;
 
var b = Convert.ToString(maxNumber, 2);
 
return CreateByHostBitLength(b.Length);
}
}
}
/wasSharpNET.csproj
@@ -43,6 +43,8 @@
<ItemGroup>
<Compile Include="Cryptography\AES.cs" />
<Compile Include="Cryptography\SHA1.cs" />
<Compile Include="Network\IPAddressExtensions.cs" />
<Compile Include="Network\SubnetMask.cs" />
<Compile Include="Process.cs" />
<Compile Include="Reflection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />