update Pilz.cryptography to net6.0
This commit is contained in:
55
Pilz.Cryptography/Helpers.cs
Normal file
55
Pilz.Cryptography/Helpers.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
{
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public static class Helpers
|
||||
{
|
||||
private static string clientSecret = null;
|
||||
|
||||
public static string CalculateClientSecret()
|
||||
{
|
||||
// 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())
|
||||
{
|
||||
if (entry is ManagementObject wmi_HD && string.IsNullOrEmpty(sn) && wmi_HD["SerialNumber"] != null)
|
||||
sn = wmi_HD["SerialNumber"].ToString()?.Trim();
|
||||
}
|
||||
|
||||
clientSecret = sn;
|
||||
}
|
||||
|
||||
// Fallback to Mashine name
|
||||
clientSecret ??= Environment.MachineName;
|
||||
|
||||
return clientSecret;
|
||||
}
|
||||
|
||||
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,6 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<AssemblyTitle>Pilz.Cryptography</AssemblyTitle>
|
||||
<Product>Pilz.Cryptography</Product>
|
||||
<Copyright>Copyright © 2020</Copyright>
|
||||
@@ -8,7 +8,7 @@
|
||||
<PropertyGroup>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<IncrementVersionOnBuild>1.yyyy.Mdd.Hmm</IncrementVersionOnBuild>
|
||||
<Version>1.2023.918.846</Version>
|
||||
<Version>1.2023.1005.1121</Version>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Management" Version="7.0.2" />
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Pilz.Cryptography
|
||||
{
|
||||
public class SimpleStringCrypter : ICrypter
|
||||
{
|
||||
private TripleDESCryptoServiceProvider TripleDes;
|
||||
private TripleDES TripleDes;
|
||||
public Encoding TextEncoding { get; private set; } = Encoding.Default;
|
||||
|
||||
public SimpleStringCrypter() : this(string.Empty)
|
||||
@@ -24,14 +24,14 @@ namespace Pilz.Cryptography
|
||||
public SimpleStringCrypter(string key, Encoding textEncoding)
|
||||
{
|
||||
TextEncoding = textEncoding;
|
||||
TripleDes = new TripleDESCryptoServiceProvider();
|
||||
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)
|
||||
{
|
||||
SHA1CryptoServiceProvider sha1CryptoServiceProvider = new SHA1CryptoServiceProvider();
|
||||
SHA1 sha1CryptoServiceProvider = SHA1.Create();
|
||||
byte[] bytes = TextEncoding.GetBytes(key);
|
||||
byte[] array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
@@ -75,7 +76,7 @@ namespace Pilz.Cryptography
|
||||
|
||||
protected virtual string GenerateDefault()
|
||||
{
|
||||
return GenerateUniquieID<UniquieID>(currentSimpleID++.ToString());
|
||||
return Helpers.GenerateUniquieID<UniquieID>(currentSimpleID++.ToString());
|
||||
}
|
||||
|
||||
public virtual void GenerateIfNull()
|
||||
@@ -124,44 +125,6 @@ namespace Pilz.Cryptography
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
#region Statics for Generation
|
||||
|
||||
protected 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 Win32_PhysicalMedia_SerialNumber = null;
|
||||
private static string TryGetSerialNumberOfFirstHardDrive()
|
||||
{
|
||||
if (Win32_PhysicalMedia_SerialNumber == null)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
Win32_PhysicalMedia_SerialNumber = sn;
|
||||
}
|
||||
|
||||
return Win32_PhysicalMedia_SerialNumber;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
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) };
|
||||
|
||||
Reference in New Issue
Block a user