Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
* Monsoon/Makefile.am:
Browse files Browse the repository at this point in the history
* Monsoon/Monsoon.csproj:
* Monsoon/OpenSSLSha1.cs:
* Monsoon/TorrentController.cs: Monsoon will use openssh for sha1
  hashing if it is available, otherwise fall back to the standard hash
  function.

svn path=/trunk/monsoon/; revision=136661
  • Loading branch information
alanmcgovern committed Jun 22, 2009
1 parent 8de5ea5 commit 3f240cc
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Monsoon/ChangeLog
@@ -1,3 +1,12 @@
2009-06-23 Alan McGovern <amcgovern@novell.com>

* Makefile.am:
* Monsoon.csproj:
* OpenSSLSha1.cs:
* TorrentController.cs: Monsoon will use openssh for sha1
hashing if it is available, otherwise fall back to the
standard hash function.

2009-06-23 Alan McGovern <amcgovern@novell.com>

* TorrentController.cs:
Expand Down
5 changes: 3 additions & 2 deletions Monsoon/Makefile.am
Expand Up @@ -5,7 +5,7 @@ EXTRA_DIST = $(NLOG_DLL_SOURCE)

if ENABLE_DEBUG
ASSEMBLY_COMPILER_COMMAND = gmcs
ASSEMBLY_COMPILER_FLAGS = -noconfig -codepage:utf8 -warn:4 -optimize+ -debug -define:DEBUG
ASSEMBLY_COMPILER_FLAGS = -noconfig -codepage:utf8 -warn:4 -optimize+ -debug -define:DEBUG -unsafe
ASSEMBLY = bin/Debug/Monsoon.exe
ASSEMBLY_MDB = $(ASSEMBLY).mdb
COMPILE_TARGET = exe
Expand All @@ -28,7 +28,7 @@ endif

if ENABLE_RELEASE
ASSEMBLY_COMPILER_COMMAND = gmcs
ASSEMBLY_COMPILER_FLAGS = -noconfig -codepage:utf8 -warn:4 -optimize+ -debug
ASSEMBLY_COMPILER_FLAGS = -noconfig -codepage:utf8 -warn:4 -optimize+ -debug -unsafe
ASSEMBLY = bin/Release/Monsoon.exe
ASSEMBLY_MDB =
COMPILE_TARGET = exe
Expand Down Expand Up @@ -134,6 +134,7 @@ FILES = \
Model/State.cs \
Model/StateChangedEventArgs.cs \
Model/TorrentFileModel.cs \
OpenSSLSha1.cs \
PeerTreeView.cs \
PiecesTreeView.cs \
PreferencesDialog.cs \
Expand Down
3 changes: 3 additions & 0 deletions Monsoon/Monsoon.csproj
Expand Up @@ -19,6 +19,7 @@
<WarningLevel>4</WarningLevel>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<AssemblyKeyFile>.</AssemblyKeyFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
Expand All @@ -28,6 +29,7 @@
<WarningLevel>4</WarningLevel>
<AssemblyKeyFile>.</AssemblyKeyFile>
<Commandlineparameters>-d</Commandlineparameters>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -146,6 +148,7 @@
<Compile Include="Model\State.cs" />
<Compile Include="Model\StateChangedEventArgs.cs" />
<Compile Include="Extensions\DhtExtension.cs" />
<Compile Include="OpenSSLSha1.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="icons\rss.png">
Expand Down
56 changes: 56 additions & 0 deletions Monsoon/OpenSSLSha1.cs
@@ -0,0 +1,56 @@

using System;
using System.Runtime.InteropServices;

namespace Monsoon
{
public class OpenSSLSha1 : System.Security.Cryptography.SHA1
{
IntPtr context;
public OpenSSLSha1 ()
{
// The native SHA1 structure is 96 bytes in length
context = Marshal.AllocHGlobal (96);
if (SHA1_Init (context) != 1)
throw new Exception ("Could not init context");
}

public override void Initialize ()
{
if (SHA1_Init (context) != 1)
throw new Exception ("Could not init context");
}

protected override unsafe void HashCore (byte[] array, int ibStart, int cbSize)
{
if (ibStart > array.Length)
throw new IndexOutOfRangeException ("ibStart");
if ((array.Length - ibStart) < cbSize)
throw new IndexOutOfRangeException ("cbSize");

if (cbSize == 0)
return;

fixed (byte *ptr = array)
if (SHA1_Update (context, ptr + ibStart, (ulong) cbSize) != 1)
throw new Exception ("Could not hash data");
}

protected override byte[] HashFinal ()
{
// Result must be at least of length 20
byte [] result = new byte [20];
if (SHA1_Final (result, context) != 1)
throw new Exception ("Could not hash final chunk");
return result;
}

// All return '1' for success, '0' for failure
[DllImport ("libssl.so.0.9.8")]
static extern int SHA1_Init (IntPtr context);
[DllImport ("libssl.so.0.9.8")]
static extern unsafe int SHA1_Update (IntPtr context, byte *data, ulong len);
[DllImport ("libssl.so.0.9.8")]
static extern int SHA1_Final (byte [] buffer, IntPtr context);
}
}
15 changes: 15 additions & 0 deletions Monsoon/TorrentController.cs
Expand Up @@ -73,6 +73,21 @@ public List<FastResume> FastResume
private static NLog.Logger logger = MainClass.DebugEnabled ? NLog.LogManager.GetCurrentClassLogger () : new EmptyLogger ();

TorrentSettings defaultTorrentSettings;

static TorrentController ()
{
try {
using (OpenSSLSha1 native = new OpenSSLSha1 ()) {
native.ComputeHash (new byte[1024]);
HashAlgoFactory.Register (typeof (SHA1), typeof (SHA1CryptoServiceProvider));
logger.Debug ("Using OpenSSL for SHA1 hashing");
}
} catch {
// If an exception occurs it means that the native SHA1 function is unusable
// so don't register it then
}
}

public TorrentController()
{
this.defaultTorrentSettings = SettingsManager.DefaultTorrentSettings;
Expand Down

0 comments on commit 3f240cc

Please sign in to comment.