From: pkanavos Date: Tue, 13 Nov 2012 16:36:43 +0000 (+0200) Subject: Added check and failover of hash algorithms: OpenSSL > Cng > Default X-Git-Url: https://code.grnet.gr/git/pithos-ms-client/commitdiff_plain/0e1acc13aab4f82a94d54515054bfa3eb111bd8f Added check and failover of hash algorithms: OpenSSL > Cng > Default Added larger sleep when Block commits fail due to open files --- diff --git a/trunk/Pithos.Client.WPF/App.xaml.cs b/trunk/Pithos.Client.WPF/App.xaml.cs index 3568ea8..89dd975 100644 --- a/trunk/Pithos.Client.WPF/App.xaml.cs +++ b/trunk/Pithos.Client.WPF/App.xaml.cs @@ -45,6 +45,7 @@ using System.IO; using System.Linq; using System.Net; using System.Reflection; +using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -53,6 +54,7 @@ using Caliburn.Micro; using Pithos.Client.WPF.Configuration; using Pithos.Client.WPF.Properties; using Pithos.Interfaces; +using Pithos.Network; using log4net.Appender; using log4net.Core; using log4net.Repository.Hierarchy; @@ -78,7 +80,8 @@ namespace Pithos.Client.WPF //var instanceMutex=new Mutex() InitializeLogging(); - + RegisterOpenSSL(); + //Detect OS, if Major Version is 6+ use RestartManager OperatingSystem os = Environment.OSVersion; @@ -110,6 +113,37 @@ namespace Pithos.Client.WPF InitializeComponent(); } + private static void RegisterOpenSSL() + { + + //Try to instantiate OpenSSL and hash an empty array + try + { + using(var testHasher=new SHA256OpenSSL()) + { + testHasher.ComputeHash(new byte[0]); + } + CryptoConfig.AddAlgorithm(typeof(SHA256OpenSSL), "SHA256"); + Log.InfoFormat("Registered OpenSSL hash provider"); + } + catch (Exception exc) + { + Log.WarnFormat("OpenSSL instantiation failed. Falling back to CNG provider.\r\n[{0}]",exc); + } + //Running on Vista + ? + if (Environment.OSVersion.Version.Major >= 6) + { + CryptoConfig.AddAlgorithm(typeof(SHA256Cng), "SHA256"); + Log.InfoFormat("Registered CNG hash provider"); + } + else + { + Log.WarnFormat("Running on XP. Falling back to default hash algorithm."); + } + + + } + /* private static string[] platformLibraries = {"libeay32.dll","ssleay32.dll"}; private static Assembly CustomResolve(object sender,ResolveEventArgs args) diff --git a/trunk/Pithos.Client.WPF/Properties/Settings.Designer.cs b/trunk/Pithos.Client.WPF/Properties/Settings.Designer.cs index 03b0c0b..7f98040 100644 --- a/trunk/Pithos.Client.WPF/Properties/Settings.Designer.cs +++ b/trunk/Pithos.Client.WPF/Properties/Settings.Designer.cs @@ -389,21 +389,22 @@ namespace Pithos.Client.WPF.Properties { [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute(@" - - -Pithos Production - https://pithos.okeanos.grnet.gr - https://pithos.okeanos.grnet.gr/login - https://accounts.okeanos.grnet.gr/im/logout - - -Pithos Development - https://pithos.dev.grnet.gr - https://pithos.dev.grnet.gr/login - https://pithos.dev.grnet.gr/im/logout - -")] + [global::System.Configuration.DefaultSettingValueAttribute(@" + + + Pithos Production + https://pithos.okeanos.grnet.gr + https://pithos.okeanos.grnet.gr/login + https://accounts.okeanos.grnet.gr/im/logout + + + Pithos Development + https://pithos.dev.grnet.gr + https://pithos.dev.grnet.gr/login + https://pithos.dev.grnet.gr/im/logout + + + ")] public global::Pithos.Interfaces.ServersCollection Servers { get { return ((global::Pithos.Interfaces.ServersCollection)(this["Servers"])); diff --git a/trunk/Pithos.Client.WPF/Properties/Settings.settings b/trunk/Pithos.Client.WPF/Properties/Settings.settings index 093c3b4..4315ad8 100644 --- a/trunk/Pithos.Client.WPF/Properties/Settings.settings +++ b/trunk/Pithos.Client.WPF/Properties/Settings.settings @@ -99,21 +99,22 @@ False - <?xml version="1.0" encoding="utf-16"?> -<ArrayOfServerSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <ServerSettings> -<ServerName>Pithos Production</ServerName> - <ServerUri>https://pithos.okeanos.grnet.gr</ServerUri> - <LoginUri>https://pithos.okeanos.grnet.gr/login</LoginUri> - <LogoutUri>https://accounts.okeanos.grnet.gr/im/logout</LogoutUri> - </ServerSettings> - <ServerSettings> -<ServerName>Pithos Development</ServerName> - <ServerUri>https://pithos.dev.grnet.gr</ServerUri> - <LoginUri>https://pithos.dev.grnet.gr/login</LoginUri> - <LogoutUri>https://pithos.dev.grnet.gr/im/logout</LogoutUri> - </ServerSettings> -</ArrayOfServerSettings> + + <ArrayOfServerSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + <ServerSettings> + <ServerName>Pithos Production</ServerName> + <ServerUri>https://pithos.okeanos.grnet.gr</ServerUri> + <LoginUri>https://pithos.okeanos.grnet.gr/login</LoginUri> + <LogoutUri>https://accounts.okeanos.grnet.gr/im/logout</LogoutUri> + </ServerSettings> + <ServerSettings> + <ServerName>Pithos Development</ServerName> + <ServerUri>https://pithos.dev.grnet.gr</ServerUri> + <LoginUri>https://pithos.dev.grnet.gr/login</LoginUri> + <LogoutUri>https://pithos.dev.grnet.gr/im/logout</LogoutUri> + </ServerSettings> + </ArrayOfServerSettings> + \ No newline at end of file diff --git a/trunk/Pithos.Client.WPF/app.config b/trunk/Pithos.Client.WPF/app.config index 0730c96..3c03440 100644 --- a/trunk/Pithos.Client.WPF/app.config +++ b/trunk/Pithos.Client.WPF/app.config @@ -138,8 +138,7 @@ - + Pithos Production https://pithos.okeanos.grnet.gr @@ -172,7 +171,7 @@ - + @@ -181,7 +180,7 @@ - + @@ -190,7 +189,7 @@ - + diff --git a/trunk/Pithos.Core/Agents/BlockUpdater.cs b/trunk/Pithos.Core/Agents/BlockUpdater.cs index 03df6bd..44b23fb 100644 --- a/trunk/Pithos.Core/Agents/BlockUpdater.cs +++ b/trunk/Pithos.Core/Agents/BlockUpdater.cs @@ -238,9 +238,9 @@ namespace Pithos.Core.Agents } catch (IOException) { - if (i>=3) + if (i>=2) throw; - Thread.Sleep(100); + Thread.Sleep((i+1)*300); } } } diff --git a/trunk/Pithos.Network/BlockHashAlgorithms.cs b/trunk/Pithos.Network/BlockHashAlgorithms.cs index 38dba4f..c129a34 100644 --- a/trunk/Pithos.Network/BlockHashAlgorithms.cs +++ b/trunk/Pithos.Network/BlockHashAlgorithms.cs @@ -43,11 +43,12 @@ using System.Collections.Concurrent; using System.Diagnostics.Contracts; using System.IO; using System.Reflection; +using System.Security.Cryptography; using System.ServiceModel.Channels; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; -using OpenSSL.Crypto; + namespace Pithos.Network { @@ -251,10 +252,9 @@ namespace Pithos.Network if (size == 0) { var buf = new byte[0]; - using (var hasher = new MessageDigestContext(MessageDigest.CreateByName(algorithm))) - { - hasher.Init(); - hashes[0] = hasher.Digest(buf); + using (var hasher = HashAlgorithm.Create(algorithm)) + { + hashes[0] = hasher.ComputeHash(buf); return hashes; } } @@ -264,13 +264,12 @@ namespace Pithos.Network var actualHashers = parallelism > blocks ? (byte)blocks : parallelism; var buffer = new byte[actualHashers][]; - var hashers = new MessageDigestContext[actualHashers]; + var hashers = new HashAlgorithm[actualHashers]; var bufferManager = GetBufferManager(blockSize, actualHashers); for (var i = 0; i < actualHashers; i++) { buffer[i] = bufferManager.TakeBuffer(blockSize);// new byte[blockSize]; - hashers[i] = new MessageDigestContext(MessageDigest.CreateByName(algorithm)); - hashers[i].Init(); + hashers[i] = HashAlgorithm.Create(algorithm); } try { @@ -307,7 +306,8 @@ namespace Pithos.Network var hasher = hashers[idx]; - byte[] hash; + byte[] hash=hasher.ComputeHash(buffer[idx],0,lastByteIndex+1); +/* if (buffer[idx].Length == lastByteIndex || lastByteIndex==-1) hash = hasher.Digest(buffer[idx]); else @@ -316,6 +316,7 @@ namespace Pithos.Network Buffer.BlockCopy(buffer[idx],0,buf,0,lastByteIndex+1); hash = hasher.Digest(buf); } +*/ diff --git a/trunk/Pithos.Network/SHA1OpenSSL.cs b/trunk/Pithos.Network/SHA1OpenSSL.cs index 7b60a3d..5fa968e 100644 --- a/trunk/Pithos.Network/SHA1OpenSSL.cs +++ b/trunk/Pithos.Network/SHA1OpenSSL.cs @@ -51,4 +51,49 @@ namespace Pithos.Network } } } + + public class SHA256OpenSSL : SHA256 + { + + private MessageDigestContext _context; + + public SHA256OpenSSL() + { + _context = new MessageDigestContext(MessageDigest.CreateByName("SHA256")); + _context.Init(); + } + + public override void Initialize() + { + _context.Init(); + } + + protected override void HashCore(byte[] array, int ibStart, int cbSize) + { + if (array.Length == cbSize) + _context.Update(array); + else + { + var block = new byte[cbSize]; + Buffer.BlockCopy(array, ibStart, block, 0, cbSize); + _context.Update(block); + } + } + + protected override byte[] HashFinal() + { + return _context.DigestFinal(); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + if (disposing) + { + if (_context != null) + _context.Dispose(); + _context = null; + } + } + } }