这是一个使用序列化、sha256 散列和 base64 编码的完整示例(基于 CodesInChaos 答案):
using System;
using System.IO;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
namespace Uniq
{
[Serializable]
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
}
class MainClass
{
public static void Main (string[] args)
{
Address address1 = new Address(){AddressLine1 = "a1"};
Address address2 = new Address(){AddressLine1 = "a1"};
Address address3 = new Address(){AddressLine1 = "a2"};
string unique1 = GetUniqueIdentifier(address1);
string unique2 = GetUniqueIdentifier(address2);
string unique3 = GetUniqueIdentifier(address3);
Console.WriteLine(unique1);
Console.WriteLine(unique2);
Console.WriteLine(unique3);
}
public static string GetUniqueIdentifier(object obj){
if (obj == null) return "0";
SHA256 mySHA256 = SHA256Managed.Create ();
BinaryFormatter formatter = new BinaryFormatter ();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, obj);
byte[] hash = mySHA256.ComputeHash(stream.GetArray());
string uniqId = Convert.ToBase64String(hash);
return uniqId;
}
}
}
编辑:这是一个不使用BinaryFormatter
. 您可以将空表示和字段分隔符替换为适合您需要的任何内容。
public static string GetUniqueIdentifier(object obj){
if (obj == null) return "0";
SHA256 mySHA256 = SHA256Managed.Create ();
StringBuilder stringRep = new StringBuilder();
obj.GetType().GetProperties()
.ToList().ForEach(p=>stringRep.Append(
p.GetValue(obj, null) ?? '¨'
).Append('^'));
Console.WriteLine(stringRep);
Console.WriteLine(stringRep.Length);
byte[] hash = mySHA256.ComputeHash(Encoding.Unicode.GetBytes(stringRep.ToString()));
string uniqId = Convert.ToBase64String(hash);
return uniqId;
}