add Pilz.Cryptography
This commit is contained in:
110
Pilz.Cryptography/UniquieID.cs
Normal file
110
Pilz.Cryptography/UniquieID.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SM64Lib
|
||||
{
|
||||
public class UniquieID<TargetType>
|
||||
{
|
||||
private static int currentSimpleID = 0;
|
||||
|
||||
[JsonProperty(nameof(ID))]
|
||||
private string _iD;
|
||||
|
||||
[JsonIgnore]
|
||||
public string ID
|
||||
{
|
||||
get
|
||||
{
|
||||
if (GenerateOnGet)
|
||||
GenerateIfNull();
|
||||
return _iD;
|
||||
}
|
||||
internal set
|
||||
=> _iD = value;
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public bool HasID { get => !string.IsNullOrEmpty(_iD); }
|
||||
[JsonIgnore]
|
||||
public bool SimpleMode { get; set; } = false;
|
||||
[JsonIgnore]
|
||||
public bool GenerateOnGet { get; set; } = false;
|
||||
|
||||
public UniquieID() : this(false)
|
||||
{
|
||||
}
|
||||
|
||||
public UniquieID(bool autoGenerate)
|
||||
{
|
||||
if (autoGenerate)
|
||||
GenerateIfNull();
|
||||
}
|
||||
|
||||
public void Generate()
|
||||
{
|
||||
if (SimpleMode)
|
||||
ID = typeof(TargetType).ToString() + currentSimpleID++.ToString();
|
||||
else
|
||||
ID = GenerateUniquieID<TargetType>(string.Empty);
|
||||
}
|
||||
public void GenerateIfNull()
|
||||
{
|
||||
if (!HasID) Generate();
|
||||
}
|
||||
|
||||
private static string GenerateUniquieID<T>(string var)
|
||||
{
|
||||
var sn = TryGetSerialNumberOfFirstHardDrive();
|
||||
var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmssfffffff");
|
||||
var type = typeof(T).ToString();
|
||||
var together = sn + dateTime + type + var;
|
||||
|
||||
var md5 = MD5.Create();
|
||||
var hash = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(together))).Replace("-", string.Empty);
|
||||
md5.Dispose();
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
private static string TryGetSerialNumberOfFirstHardDrive()
|
||||
{
|
||||
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
|
||||
var sn = string.Empty;
|
||||
|
||||
foreach (ManagementObject wmi_HD in searcher.Get())
|
||||
{
|
||||
if (string.IsNullOrEmpty(sn) && wmi_HD["SerialNumber"] != null)
|
||||
sn = wmi_HD["SerialNumber"].ToString().Trim();
|
||||
}
|
||||
|
||||
return sn;
|
||||
}
|
||||
|
||||
public override string ToString() => ID;
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var iD = obj as UniquieID<TargetType>;
|
||||
return iD != null &&
|
||||
_iD == iD._iD;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return -1430039477 + EqualityComparer<string>.Default.GetHashCode(_iD);
|
||||
}
|
||||
|
||||
public static implicit operator string(UniquieID<TargetType> id) => id.ID;
|
||||
public static implicit operator UniquieID<TargetType>(string id) => new UniquieID<TargetType>() { ID = id };
|
||||
public static implicit operator UniquieID<TargetType>(int id) => new UniquieID<TargetType>() { ID = Convert.ToString(id) };
|
||||
|
||||
public static bool operator ==(UniquieID<TargetType> left, UniquieID<TargetType> right) => left.ID == right.ID;
|
||||
public static bool operator !=(UniquieID<TargetType> left, UniquieID<TargetType> right) => left.ID != right.ID;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user