Consolidate SHA1 helpers

- Issue MatterHackers/MCCentral#2879
Ensure consistency and consolidate all sha1 helpers
This commit is contained in:
John Lewin 2018-03-12 17:02:18 -07:00
parent 5e26259918
commit c09e0731ef
4 changed files with 9 additions and 51 deletions

View file

@ -1143,28 +1143,6 @@ namespace MatterHackers.MatterControl
}
}
public string ComputeFileSha1(string filePath)
{
using (var stream = File.OpenRead(filePath))
{
return GenerateSha1(stream);
}
}
private string GenerateSha1(Stream stream)
{
// var timer = Stopwatch.StartNew();
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
byte[] hash = sha1.ComputeHash(stream);
string SHA1 = BitConverter.ToString(hash).Replace("-", String.Empty);
// Console.WriteLine("{0} {1} {2}", SHA1, timer.ElapsedMilliseconds, filePath);
return SHA1;
}
}
/// <summary>
/// Compute hash for string encoded as UTF8
/// </summary>

View file

@ -22,6 +22,7 @@ using System.Runtime.InteropServices;
[assembly: InternalsVisibleTo("MatterControl.Tests")]
[assembly: InternalsVisibleTo("MatterControl.AutomationTests")]
[assembly: InternalsVisibleTo("CloudServices.Tests")]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1558c103-dff3-49bd-854b-97d57339d662")]

View file

@ -33,6 +33,7 @@ using System.Globalization;
using System.IO;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.Library;
@ -102,7 +103,7 @@ namespace MatterHackers.MatterControl.PrintQueue
{
if (File.Exists(this.FileLocation))
{
return ApplicationController.Instance.ComputeFileSha1(this.FileLocation);
return Object3D.ComputeSHA1(this.FileLocation);
}
return "file-missing";

View file

@ -39,6 +39,7 @@ using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.ContactForm;
using MatterHackers.MatterControl.SettingsManagement;
@ -335,29 +336,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
public List<string> MaterialSettingsKeys { get; set; } = new List<string>();
private string GenerateSha1()
{
// Maybe be UTF8 encoded, may not...
using (var fileStream = new FileStream(DocumentPath, FileMode.Open))
using (var bufferedStream = new BufferedStream(fileStream, 1200000))
{
return GenerateSha1(bufferedStream);
}
}
private string GenerateSha1(Stream stream)
{
// var timer = Stopwatch.StartNew();
using (var sha1 = System.Security.Cryptography.SHA1.Create())
{
byte[] hash = sha1.ComputeHash(stream);
string SHA1 = BitConverter.ToString(hash).Replace("-", String.Empty);
// Console.WriteLine("{0} {1} {2}", SHA1, timer.ElapsedMilliseconds, filePath);
return SHA1;
}
}
[JsonIgnore]
public string DocumentPath => ProfileManager.Instance.ProfilePath(this.ID);
@ -384,7 +362,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
var printerInfo = ProfileManager.Instance[this.ID];
if (printerInfo != null)
{
printerInfo.ContentSHA1 = this.ComputeSha1(json);
printerInfo.ContentSHA1 = this.ComputeSHA1(json);
ProfileManager.Instance.Save();
}
@ -397,17 +375,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
}
}
public string ComputeSha1()
internal string ComputeSHA1()
{
return ComputeSha1(this.ToJson());
return ComputeSHA1(this.ToJson());
}
private string ComputeSha1(string json)
private string ComputeSHA1(string json)
{
// SHA1 value is based on UTF8 encoded file contents
using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
return GenerateSha1(memoryStream);
return Object3D.ComputeSHA1(memoryStream);
}
}