简体   繁体   中英

Generate Deterministic Guid from a List of Guids

I would like to generate a Guid from a list of other Guids. The generated Guid must have the property that for the same input list of guids the resulting Guid will be the same, no matter how many times i apply the transformation.

Also, it should have the lowest collision possible so different guids at the input generate a different guid at the output.

Can someone help me with this? What should be the best way to go here? Its basically a hash function but over Guids.

You could do some arithmetic on the individual bytes of a Guid - the code below basically adds them up (modulo 256 because of the overflow):

byte[] totalBytes = new byte[16];
foreach (var guid in guids) {
    var bytes = guid.ToByteArray();
    for (int i = 0; i < 16; i++) {
        totalBytes[i] += bytes[i];
    }
}
var totalGuid = new Guid(totalBytes);

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