code optimization
This commit is contained in:
@@ -1,55 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public static class Helpers
|
||||
{
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public static class Helpers
|
||||
private static string clientSecret = null;
|
||||
|
||||
public static string CalculateClientSecret()
|
||||
{
|
||||
private static string clientSecret = null;
|
||||
|
||||
public static string CalculateClientSecret()
|
||||
// Try getting serial number of C drive
|
||||
if (clientSecret == null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
// Try getting serial number of C drive
|
||||
if (clientSecret == null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
ManagementObjectSearcher searcher = new("SELECT * FROM Win32_PhysicalMedia");
|
||||
string sn = null;
|
||||
|
||||
foreach (var entry in searcher.Get())
|
||||
{
|
||||
ManagementObjectSearcher searcher = new("SELECT * FROM Win32_PhysicalMedia");
|
||||
string sn = null;
|
||||
|
||||
foreach (var entry in searcher.Get())
|
||||
{
|
||||
if (entry is ManagementObject wmi_HD && string.IsNullOrEmpty(sn) && wmi_HD["SerialNumber"] != null)
|
||||
sn = wmi_HD["SerialNumber"].ToString()?.Trim();
|
||||
}
|
||||
|
||||
clientSecret = sn;
|
||||
if (entry is ManagementObject wmi_HD && string.IsNullOrEmpty(sn) && wmi_HD["SerialNumber"] != null)
|
||||
sn = wmi_HD["SerialNumber"].ToString()?.Trim();
|
||||
}
|
||||
|
||||
// Fallback to Mashine name
|
||||
clientSecret ??= Environment.MachineName;
|
||||
|
||||
return clientSecret;
|
||||
clientSecret = sn;
|
||||
}
|
||||
|
||||
public static string GenerateUniquieID<T>(string var)
|
||||
{
|
||||
var sn = CalculateClientSecret();
|
||||
var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmssfffffff");
|
||||
var type = typeof(T).ToString();
|
||||
var together = sn + dateTime + type + var;
|
||||
// Fallback to Mashine name
|
||||
clientSecret ??= Environment.MachineName;
|
||||
|
||||
var md5 = MD5.Create();
|
||||
var hash = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(together))).Replace("-", string.Empty);
|
||||
md5.Dispose();
|
||||
return clientSecret;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
public static string GenerateUniquieID<T>(string var)
|
||||
{
|
||||
var sn = CalculateClientSecret();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
public interface ICrypter
|
||||
{
|
||||
public interface ICrypter
|
||||
{
|
||||
string Encrypt(string plainValue);
|
||||
string Decrypt(string encryptedValue);
|
||||
}
|
||||
string Encrypt(string plainValue);
|
||||
string Decrypt(string encryptedValue);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
[JsonConverter(typeof(Json.Converters.UniquieIDStringJsonConverter))]
|
||||
public interface IUniquieID
|
||||
{
|
||||
[JsonConverter(typeof(Json.Converters.UniquieIDStringJsonConverter))]
|
||||
public interface IUniquieID
|
||||
{
|
||||
bool HasID { get; }
|
||||
string ID { get; }
|
||||
bool HasID { get; }
|
||||
string ID { get; }
|
||||
|
||||
void GenerateIfNull();
|
||||
void Generate();
|
||||
bool Equals(object obj);
|
||||
}
|
||||
void GenerateIfNull();
|
||||
void Generate();
|
||||
bool Equals(object obj);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
/// <summary>
|
||||
/// Can be implemented on objects that provides an UniquieID.
|
||||
/// </summary>
|
||||
public interface IUniquieIDHost
|
||||
{
|
||||
/// <summary>
|
||||
/// Can be implemented on objects that provides an UniquieID.
|
||||
/// </summary>
|
||||
public interface IUniquieIDHost
|
||||
{
|
||||
UniquieID ID { get; }
|
||||
}
|
||||
UniquieID ID { get; }
|
||||
}
|
||||
|
||||
@@ -1,38 +1,35 @@
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.Cryptography;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Pilz.Json.Converters
|
||||
namespace Pilz.Json.Converters;
|
||||
|
||||
public class UniquieIDStringJsonConverter : JsonConverter
|
||||
{
|
||||
public class UniquieIDStringJsonConverter : JsonConverter
|
||||
public static bool EnableCheckForDepricatedTypes { get; set; } = true;
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
public static bool EnableCheckForDepricatedTypes { get; set; } = true;
|
||||
return typeof(IUniquieID).IsAssignableFrom(objectType);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return typeof(IUniquieID).IsAssignableFrom(objectType);
|
||||
}
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var idString = serializer.Deserialize<string>(reader);
|
||||
UniquieID id;
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var idString = serializer.Deserialize<string>(reader);
|
||||
UniquieID id;
|
||||
if (existingValue is UniquieID existingID && (!EnableCheckForDepricatedTypes || existingID.GetType() == typeof(UniquieID)))
|
||||
id = existingID;
|
||||
else
|
||||
id = new UniquieID();
|
||||
|
||||
if (existingValue is UniquieID existingID && (!EnableCheckForDepricatedTypes || existingID.GetType() == typeof(UniquieID)))
|
||||
id = existingID;
|
||||
else
|
||||
id = new UniquieID();
|
||||
id.ID = idString;
|
||||
|
||||
id.ID = idString;
|
||||
return id;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, ((UniquieID)value).ID);
|
||||
}
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, ((UniquieID)value).ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||
|
||||
@@ -1,78 +1,73 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
[JsonConverter(typeof(Json.Converters.SecureStringJsonConverter))]
|
||||
public class SecureString
|
||||
{
|
||||
[JsonConverter(typeof(Json.Converters.SecureStringJsonConverter))]
|
||||
public class SecureString
|
||||
public static ICrypter DefaultCrypter { get; set; }
|
||||
public ICrypter Crypter { get; set; }
|
||||
public string EncryptedValue { get; set; }
|
||||
public string Value
|
||||
{
|
||||
public static ICrypter DefaultCrypter { get; set; }
|
||||
public ICrypter Crypter { get; set; }
|
||||
public string EncryptedValue { get; set; }
|
||||
public string Value
|
||||
{
|
||||
get => GetCrypter()?.Decrypt(EncryptedValue);
|
||||
set => EncryptedValue = GetCrypter().Encrypt(value);
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
private SecureString(JsonConstructorAttribute dummyAttribute)
|
||||
{
|
||||
}
|
||||
|
||||
public SecureString() :
|
||||
this(string.Empty, true)
|
||||
{
|
||||
}
|
||||
|
||||
public SecureString(string value, bool isEncrypted) :
|
||||
this(value, isEncrypted, null)
|
||||
{
|
||||
}
|
||||
|
||||
public SecureString(string value, bool isEncrypted, ICrypter crypter)
|
||||
{
|
||||
Crypter = crypter;
|
||||
|
||||
if (isEncrypted)
|
||||
EncryptedValue = value;
|
||||
else
|
||||
Value = value;
|
||||
}
|
||||
|
||||
private ICrypter GetCrypter()
|
||||
{
|
||||
if (Crypter == null)
|
||||
{
|
||||
if (DefaultCrypter == null)
|
||||
DefaultCrypter = new SimpleStringCrypter(string.Empty);
|
||||
Crypter = DefaultCrypter;
|
||||
}
|
||||
return Crypter;
|
||||
}
|
||||
|
||||
public override string ToString() => Value;
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var @string = obj as SecureString;
|
||||
return @string != null &&
|
||||
EncryptedValue == @string.EncryptedValue;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return -2303024 + EqualityComparer<string>.Default.GetHashCode(EncryptedValue);
|
||||
}
|
||||
|
||||
public static implicit operator string(SecureString value) => value?.Value;
|
||||
public static implicit operator SecureString(string value) => new SecureString(value, false);
|
||||
|
||||
public static bool operator ==(SecureString left, SecureString right) => left?.EncryptedValue == right?.EncryptedValue;
|
||||
public static bool operator !=(SecureString left, SecureString right) => left?.EncryptedValue != right?.EncryptedValue;
|
||||
get => GetCrypter()?.Decrypt(EncryptedValue);
|
||||
set => EncryptedValue = GetCrypter().Encrypt(value);
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
private SecureString(JsonConstructorAttribute dummyAttribute)
|
||||
{
|
||||
}
|
||||
|
||||
public SecureString() :
|
||||
this(string.Empty, true)
|
||||
{
|
||||
}
|
||||
|
||||
public SecureString(string value, bool isEncrypted) :
|
||||
this(value, isEncrypted, null)
|
||||
{
|
||||
}
|
||||
|
||||
public SecureString(string value, bool isEncrypted, ICrypter crypter)
|
||||
{
|
||||
Crypter = crypter;
|
||||
|
||||
if (isEncrypted)
|
||||
EncryptedValue = value;
|
||||
else
|
||||
Value = value;
|
||||
}
|
||||
|
||||
private ICrypter GetCrypter()
|
||||
{
|
||||
if (Crypter == null)
|
||||
{
|
||||
if (DefaultCrypter == null)
|
||||
DefaultCrypter = new SimpleStringCrypter(string.Empty);
|
||||
Crypter = DefaultCrypter;
|
||||
}
|
||||
return Crypter;
|
||||
}
|
||||
|
||||
public override string ToString() => Value;
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var @string = obj as SecureString;
|
||||
return @string != null &&
|
||||
EncryptedValue == @string.EncryptedValue;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return -2303024 + EqualityComparer<string>.Default.GetHashCode(EncryptedValue);
|
||||
}
|
||||
|
||||
public static implicit operator string(SecureString value) => value?.Value;
|
||||
public static implicit operator SecureString(string value) => new SecureString(value, false);
|
||||
|
||||
public static bool operator ==(SecureString left, SecureString right) => left?.EncryptedValue == right?.EncryptedValue;
|
||||
public static bool operator !=(SecureString left, SecureString right) => left?.EncryptedValue != right?.EncryptedValue;
|
||||
}
|
||||
|
||||
@@ -1,41 +1,34 @@
|
||||
using Newtonsoft.Json;
|
||||
using Pilz.Cryptography;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pilz.Json.Converters
|
||||
namespace Pilz.Json.Converters;
|
||||
|
||||
public class SecureStringJsonConverter : JsonConverter
|
||||
{
|
||||
public class SecureStringJsonConverter : JsonConverter
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
return typeof(SecureString).IsAssignableFrom(objectType);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var idString = serializer.Deserialize<string>(reader);
|
||||
SecureString id;
|
||||
|
||||
if (existingValue is SecureString)
|
||||
{
|
||||
return typeof(SecureString).IsAssignableFrom(objectType);
|
||||
id = (SecureString)existingValue;
|
||||
id.EncryptedValue = idString;
|
||||
}
|
||||
else
|
||||
id = new SecureString(idString, true);
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var idString = serializer.Deserialize<string>(reader);
|
||||
SecureString id;
|
||||
return id;
|
||||
}
|
||||
|
||||
if (existingValue is SecureString)
|
||||
{
|
||||
id = (SecureString)existingValue;
|
||||
id.EncryptedValue = idString;
|
||||
}
|
||||
else
|
||||
id = new SecureString(idString, true);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, ((SecureString)value).EncryptedValue);
|
||||
}
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
serializer.Serialize(writer, ((SecureString)value).EncryptedValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
/* Nicht gemergte Änderung aus Projekt "Pilz.Cryptography (net6.0)"
|
||||
Vor:
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
Nach:
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
*/
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
public class SimpleStringCrypter : ICrypter
|
||||
{
|
||||
public class SimpleStringCrypter : ICrypter
|
||||
private readonly TripleDES TripleDes;
|
||||
public Encoding TextEncoding { get; private set; } = Encoding.Default;
|
||||
|
||||
public SimpleStringCrypter() : this(string.Empty)
|
||||
{
|
||||
private readonly TripleDES TripleDes;
|
||||
public Encoding TextEncoding { get; private set; } = Encoding.Default;
|
||||
}
|
||||
|
||||
public SimpleStringCrypter() : this(string.Empty)
|
||||
{
|
||||
}
|
||||
public SimpleStringCrypter(string key) : this(key, Encoding.Default)
|
||||
{
|
||||
}
|
||||
|
||||
public SimpleStringCrypter(string key) : this(key, Encoding.Default)
|
||||
{
|
||||
}
|
||||
public SimpleStringCrypter(string key, Encoding textEncoding)
|
||||
{
|
||||
TextEncoding = textEncoding;
|
||||
TripleDes = TripleDES.Create();
|
||||
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
|
||||
TripleDes.IV = TruncateHash(string.Empty, TripleDes.BlockSize / 8);
|
||||
}
|
||||
|
||||
public SimpleStringCrypter(string key, Encoding textEncoding)
|
||||
{
|
||||
TextEncoding = textEncoding;
|
||||
TripleDes = TripleDES.Create();
|
||||
TripleDes.Key = TruncateHash(key,TripleDes.KeySize / 8);
|
||||
TripleDes.IV = TruncateHash(string.Empty, TripleDes.BlockSize / 8);
|
||||
}
|
||||
private byte[] TruncateHash(string key, int length)
|
||||
{
|
||||
SHA1 sha1CryptoServiceProvider = SHA1.Create();
|
||||
var bytes = TextEncoding.GetBytes(key);
|
||||
var array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
||||
|
||||
private byte[] TruncateHash(string key, int length)
|
||||
{
|
||||
SHA1 sha1CryptoServiceProvider = SHA1.Create();
|
||||
var bytes = TextEncoding.GetBytes(key);
|
||||
var array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
||||
var output = new byte[length];
|
||||
var lowerLength = Math.Min(array.Length, output.Length);
|
||||
|
||||
var output = new byte[length];
|
||||
var lowerLength = Math.Min(array.Length, output.Length);
|
||||
|
||||
for (int i = 0; i < lowerLength; i++)
|
||||
output[i] = array[i];
|
||||
for (int i = 0; i < lowerLength; i++)
|
||||
output[i] = array[i];
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
private string EncryptData(string plaintext)
|
||||
{
|
||||
var bytes = TextEncoding.GetBytes(plaintext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(bytes, 0, bytes.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return Convert.ToBase64String(memoryStream.ToArray());
|
||||
}
|
||||
private string EncryptData(string plaintext)
|
||||
{
|
||||
var bytes = TextEncoding.GetBytes(plaintext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(bytes, 0, bytes.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return Convert.ToBase64String(memoryStream.ToArray());
|
||||
}
|
||||
|
||||
private string DecryptData(string encryptedtext)
|
||||
{
|
||||
var array = Convert.FromBase64String(encryptedtext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(array, 0, array.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return TextEncoding.GetString(memoryStream.ToArray());
|
||||
}
|
||||
private string DecryptData(string encryptedtext)
|
||||
{
|
||||
var array = Convert.FromBase64String(encryptedtext);
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);
|
||||
cryptoStream.Write(array, 0, array.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
return TextEncoding.GetString(memoryStream.ToArray());
|
||||
}
|
||||
|
||||
public string Encrypt(string plainValue)
|
||||
{
|
||||
return EncryptData(plainValue ?? string.Empty);
|
||||
}
|
||||
public string Encrypt(string plainValue)
|
||||
{
|
||||
return EncryptData(plainValue ?? string.Empty);
|
||||
}
|
||||
|
||||
public string Decrypt(string encryptedValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(encryptedValue))
|
||||
return string.Empty;
|
||||
else
|
||||
return DecryptData(encryptedValue);
|
||||
}
|
||||
public string Decrypt(string encryptedValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(encryptedValue))
|
||||
return string.Empty;
|
||||
else
|
||||
return DecryptData(encryptedValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,135 +1,130 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
[JsonConverter(typeof(Json.Converters.UniquieIDStringJsonConverter))]
|
||||
public class UniquieID : IUniquieID
|
||||
{
|
||||
[JsonConverter(typeof(Json.Converters.UniquieIDStringJsonConverter))]
|
||||
public class UniquieID : IUniquieID
|
||||
protected static ulong currentSimpleID = 0;
|
||||
|
||||
[JsonProperty(nameof(ID))]
|
||||
protected string _iD;
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual bool SimpleMode { get; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual bool GenerateOnGet { get; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual bool HasID => !string.IsNullOrEmpty(_iD);
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual string ID
|
||||
{
|
||||
protected static ulong currentSimpleID = 0;
|
||||
|
||||
[JsonProperty(nameof(ID))]
|
||||
protected string _iD;
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual bool SimpleMode { get; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual bool GenerateOnGet { get; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual bool HasID => !string.IsNullOrEmpty(_iD);
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual string ID
|
||||
get
|
||||
{
|
||||
get
|
||||
{
|
||||
if (GenerateOnGet)
|
||||
GenerateIfNull();
|
||||
return _iD;
|
||||
}
|
||||
internal set
|
||||
=> _iD = value;
|
||||
}
|
||||
|
||||
public UniquieID() : this(UniquieIDGenerationMode.None)
|
||||
{
|
||||
}
|
||||
|
||||
public UniquieID(UniquieIDGenerationMode mode) : this(mode, false)
|
||||
{
|
||||
}
|
||||
|
||||
public UniquieID(UniquieIDGenerationMode mode, bool simpleMode)
|
||||
{
|
||||
SimpleMode = simpleMode;
|
||||
|
||||
if (mode == UniquieIDGenerationMode.GenerateOnInit)
|
||||
if (GenerateOnGet)
|
||||
GenerateIfNull();
|
||||
else if (mode == UniquieIDGenerationMode.GenerateOnGet)
|
||||
GenerateOnGet = true;
|
||||
return _iD;
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public UniquieID(bool autoGenerate) : this(autoGenerate ? UniquieIDGenerationMode.GenerateOnInit : UniquieIDGenerationMode.None)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Generate()
|
||||
{
|
||||
if (SimpleMode)
|
||||
ID = GenerateSimple();
|
||||
else
|
||||
ID = GenerateDefault();
|
||||
}
|
||||
|
||||
protected virtual string GenerateSimple()
|
||||
{
|
||||
return new Random().Next().ToString() + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + currentSimpleID++.ToString();
|
||||
}
|
||||
|
||||
protected virtual string GenerateDefault()
|
||||
{
|
||||
return Helpers.GenerateUniquieID<UniquieID>(currentSimpleID++.ToString());
|
||||
}
|
||||
|
||||
public virtual void GenerateIfNull()
|
||||
{
|
||||
if (!HasID) Generate();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ID;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return -1430039477 + EqualityComparer<string>.Default.GetHashCode(_iD);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is UniquieID iD)
|
||||
{
|
||||
if (ReferenceEquals(obj, iD))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
var leftHasID = iD.HasID;
|
||||
var rightHasID = HasID;
|
||||
|
||||
if (!leftHasID && iD.GenerateOnGet)
|
||||
{
|
||||
iD.Generate();
|
||||
leftHasID = iD.HasID;
|
||||
}
|
||||
|
||||
if (!rightHasID && GenerateOnGet)
|
||||
{
|
||||
Generate();
|
||||
rightHasID = HasID;
|
||||
}
|
||||
|
||||
if (leftHasID && rightHasID)
|
||||
return _iD.Equals(iD._iD);
|
||||
}
|
||||
}
|
||||
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
public static implicit operator string(UniquieID id) => id.ID;
|
||||
public static implicit operator UniquieID(string id) => new UniquieID() { ID = id };
|
||||
public static implicit operator UniquieID(int id) => new UniquieID() { ID = Convert.ToString(id) };
|
||||
|
||||
public static bool operator ==(UniquieID left, UniquieID right) => left.ID.Equals(right.ID);
|
||||
public static bool operator !=(UniquieID left, UniquieID right) => !left.ID.Equals(right.ID);
|
||||
internal set
|
||||
=> _iD = value;
|
||||
}
|
||||
|
||||
public UniquieID() : this(UniquieIDGenerationMode.None)
|
||||
{
|
||||
}
|
||||
|
||||
public UniquieID(UniquieIDGenerationMode mode) : this(mode, false)
|
||||
{
|
||||
}
|
||||
|
||||
public UniquieID(UniquieIDGenerationMode mode, bool simpleMode)
|
||||
{
|
||||
SimpleMode = simpleMode;
|
||||
|
||||
if (mode == UniquieIDGenerationMode.GenerateOnInit)
|
||||
GenerateIfNull();
|
||||
else if (mode == UniquieIDGenerationMode.GenerateOnGet)
|
||||
GenerateOnGet = true;
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public UniquieID(bool autoGenerate) : this(autoGenerate ? UniquieIDGenerationMode.GenerateOnInit : UniquieIDGenerationMode.None)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Generate()
|
||||
{
|
||||
if (SimpleMode)
|
||||
ID = GenerateSimple();
|
||||
else
|
||||
ID = GenerateDefault();
|
||||
}
|
||||
|
||||
protected virtual string GenerateSimple()
|
||||
{
|
||||
return new Random().Next().ToString() + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + currentSimpleID++.ToString();
|
||||
}
|
||||
|
||||
protected virtual string GenerateDefault()
|
||||
{
|
||||
return Helpers.GenerateUniquieID<UniquieID>(currentSimpleID++.ToString());
|
||||
}
|
||||
|
||||
public virtual void GenerateIfNull()
|
||||
{
|
||||
if (!HasID) Generate();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ID;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return -1430039477 + EqualityComparer<string>.Default.GetHashCode(_iD);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is UniquieID iD)
|
||||
{
|
||||
if (ReferenceEquals(obj, iD))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
var leftHasID = iD.HasID;
|
||||
var rightHasID = HasID;
|
||||
|
||||
if (!leftHasID && iD.GenerateOnGet)
|
||||
{
|
||||
iD.Generate();
|
||||
leftHasID = iD.HasID;
|
||||
}
|
||||
|
||||
if (!rightHasID && GenerateOnGet)
|
||||
{
|
||||
Generate();
|
||||
rightHasID = HasID;
|
||||
}
|
||||
|
||||
if (leftHasID && rightHasID)
|
||||
return _iD.Equals(iD._iD);
|
||||
}
|
||||
}
|
||||
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
public static implicit operator string(UniquieID id) => id.ID;
|
||||
public static implicit operator UniquieID(string id) => new UniquieID() { ID = id };
|
||||
public static implicit operator UniquieID(int id) => new UniquieID() { ID = Convert.ToString(id) };
|
||||
|
||||
public static bool operator ==(UniquieID left, UniquieID right) => left.ID.Equals(right.ID);
|
||||
public static bool operator !=(UniquieID left, UniquieID right) => !left.ID.Equals(right.ID);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
namespace Pilz.Cryptography;
|
||||
|
||||
namespace Pilz.Cryptography
|
||||
public enum UniquieIDGenerationMode
|
||||
{
|
||||
public enum UniquieIDGenerationMode
|
||||
{
|
||||
None,
|
||||
GenerateOnGet,
|
||||
GenerateOnInit
|
||||
}
|
||||
None,
|
||||
GenerateOnGet,
|
||||
GenerateOnInit
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user