简体   繁体   中英

Is there a Delphi XE2 string hash function guaranteed to be unique that I can use for lookup?

I am using a TClientDataSet to manage objects and to give me 'database' access to the object data. So far this works well. I have two 'special' (hidden) fields within the dataset - 'ObjectName' and 'ObjectRef'. ObjectName is a conjunctions of the category and name of the object item in the form My category.my object' and is used to get me from inside an object instance to the record number. This field is indexed. 'ObjectRef' is an integer typcast of a pointer to that object's instance and is used for all other object lifetime management.

I have had to choose a size for the 'ObjectName' field in which to fit my expected max possible category and name combination, but this is only an index and I would like to keep this size as small as possible for performance and memory reasons. Is there a 'lossless' function that I can apply to my form 'My category.my name' which would still be unique that I can use as a hash? Hash functions look clever but not being a computer science guru I am never sure how to know whether their output is unique.

Thanks

All the hash functons has risk of collitions, but AFAIK one of the more secure is the SHA-1 algorithm, exist many delphi implementations, for example you can use the Jwscl library (JEDI Windows Security Code Lib) which is a wrapper for the Windows CryptoAPI (you can find a delphi sample on this question SHA1 hashing in Delphi XE ) or use the TIdHashSHA1 class which is part of Indy.

Another alternative is use more simple hash function (non-cryptographic) like the Jenkins hash function which delphi implements in the BobJenkinsHash method.

No. By definition hash functions results are not unique.

You probably need to make a local list to track ObjectNames in your application and associate unique index with every object that is added, so then you could store it in DB instead of ObjectName. Or assign globally unique indexes to your objects upon creation (eg UInt64)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM