dictionary - C# Byte Array of lists (or dictionaries) with equal content are not equal -
i have 2 lists same content. first 1 had added 3 items , removed 1 again. second 1 had added remaining 2 items. converted both byte array, byte array not equal. differ in 1 byte. there way have 2 "equal" lists equal byte arrays independent of performed operations?
i need saving list text file rsa signature included. can't have valide signature if list in memory, operations performed on in past, not equal new generated list content of text file.
in application use dictionaries instead of lists, it's same behaviour.
here test code:
static void main(string[] args) { list<string> 1 = new list<string>(); one.add("hallo"); one.add("hallo1"); one.remove("hallo1"); one.add("hallo1"); byte[] test = program.objecttobytearray(one); console.writeline(test.length); list<string> 2 = new list<string>(); two.add("hallo"); two.add("hallo1"); byte[] test1 = program.objecttobytearray(two); console.writeline(test1.length); bool areequal = false; int count = 0; if (test.length == test1.length) { areequal = true; (int = 0; < test.length; i++) { if (test[i] != test1[i]) { areequal = false; count++; } } } console.writeline("are equal?: " + areequal.tostring() + ": " + count.tostring()); console.readline(); } private static byte[] objecttobytearray(object obj) { if (obj == null) return null; system.runtime.serialization.formatters.binary.binaryformatter bf = new system.runtime.serialization.formatters.binary.binaryformatter(); memorystream ms = new memorystream(); bf.serialize(ms, obj); return ms.toarray(); }
edit: in application have class this:
[serializable] class metadatamodel { private dictionary<string, string> readers; private dictionary<string, string> writers; public bool isdirty { get; set; } public string publicreaderkey { get; set; } public string cipherprivatereaderkey { get; set; } public dictionary<string, string> readers { { if (this.readers == null) this.readers = new dictionary<string, string>(); return this.readers; } set { this.readers = value; } } public string publicwriterkey { get; set; } public string cipherprivatewriterkey { get; set; } public dictionary<string, string> writers { { if (this.writers == null) this.writers = new dictionary<string, string>(); return this.writers; } set { this.writers = value; } } public byte[] signature { get; set; } }
and want use this:
metadatamodel metadata = new metadatamodel() //filling properties here metadata.signature = rsasignaturegenerator.hashandsigndata(this.objecttobytearray(metadata), this.rsakeypair[0]);
Comments
Post a Comment