From 242ac7819daf9ef989a87aa773683188d246649b Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Sat, 4 Aug 2012 20:55:29 +0200 Subject: [PATCH] fetcher: Do fingerprint check in managed code --- SparkleLib/SparkleExtensions.cs | 12 +++++------ SparkleLib/SparkleFetcherBase.cs | 37 ++++++++------------------------ 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/SparkleLib/SparkleExtensions.cs b/SparkleLib/SparkleExtensions.cs index c386a52d..4c1d9a42 100755 --- a/SparkleLib/SparkleExtensions.cs +++ b/SparkleLib/SparkleExtensions.cs @@ -37,11 +37,11 @@ namespace SparkleLib { public static string SHA1 (this string s) { - SHA1 sha1 = new SHA1CryptoServiceProvider (); - byte [] bytes = ASCIIEncoding.Default.GetBytes (s); - byte [] enc_bytes = sha1.ComputeHash (bytes); + SHA1 sha1 = new SHA1CryptoServiceProvider (); + byte [] bytes = ASCIIEncoding.Default.GetBytes (s); + byte [] sha1_bytes = sha1.ComputeHash (bytes); - return BitConverter.ToString (enc_bytes).ToLower ().Replace ("-", ""); + return BitConverter.ToString (sha1_bytes).ToLower ().Replace ("-", ""); } @@ -49,9 +49,9 @@ namespace SparkleLib { { MD5 md5 = new MD5CryptoServiceProvider (); byte [] bytes = ASCIIEncoding.Default.GetBytes (s); - byte [] enc_bytes = md5.ComputeHash (bytes); + byte [] md5_bytes = md5.ComputeHash (bytes); - return BitConverter.ToString (enc_bytes).ToLower ().Replace ("-", ""); + return BitConverter.ToString (md5_bytes).ToLower ().Replace ("-", ""); } } } diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index 4ae56b91..52154e6e 100755 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Security.Cryptography; using System.Threading; namespace SparkleLib { @@ -282,41 +283,21 @@ namespace SparkleLib { } - // FIXME: Calculate fingerprint natively: decode base64 -> md5 private string GetFingerprint (string public_key) { - string tmp_file_path = Path.Combine (SparkleConfig.DefaultConfig.TmpPath, "hostkey.tmp"); - File.WriteAllText (tmp_file_path, public_key + Environment.NewLine); - - Process process = new Process () { - EnableRaisingEvents = true - }; - - process.StartInfo.FileName = "ssh-keygen"; - process.StartInfo.Arguments = "-lf \"" + tmp_file_path + "\""; - process.StartInfo.WorkingDirectory = SparkleConfig.DefaultConfig.TmpPath; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.CreateNoWindow = true; - - process.Start (); - - // Reading the standard output HAS to go before - // WaitForExit, or it will hang forever on output > 4096 bytes - string fingerprint = process.StandardOutput.ReadToEnd ().Trim (); - process.WaitForExit (); - - File.Delete (tmp_file_path); - try { - fingerprint = fingerprint.Substring (fingerprint.IndexOf (" ") + 1, 47); + MD5 md5 = new MD5CryptoServiceProvider (); + string key = public_key.Split (" ".ToCharArray ()) [2]; + byte [] b64_bytes = Convert.FromBase64String (key); + byte [] md5_bytes = md5.ComputeHash (b64_bytes); + string fingerprint = BitConverter.ToString (md5_bytes); + + return fingerprint.ToLower ().Replace ("-", ":"); } catch (Exception e) { - SparkleLogger.LogInfo ("Fetcher", "Not a valid fingerprint: " + e.Message); + SparkleLogger.LogInfo ("Fetcher", "Failed creating fingerprint: " + e.Message + e.StackTrace); return null; } - - return fingerprint; }