How to hash a set of GUIDs in Swift

1 Answer

0 votes
import Foundation

// A GUID (Globally Unique Identifier) and a UUID (Universally Unique Identifier)
// are essentially the same, both being 128-bit values used to uniquely identify
// information in computer systems.

struct HashGuidsExample {

    static func main() {

        // Create a Set containing three randomly generated UUIDs.
        // UUID() produces a type‑4 GUID.
        let guids: Set<UUID> = [
            UUID(),
            UUID(),
            UUID()
        ]

        // Compute a hash value for the entire set.
        // Using Swift's Hasher for combining values into a single hash.
        var hasher = Hasher()
        hasher.combine(guids)
        let hash = hasher.finalize()

        // Print the GUIDs so we can see what was hashed.
        print("GUIDs in the set:")
        for g in guids {
            print(g)
        }

        // Print the resulting hash value.
        print("\nHash of the GUID set: \(hash)")
    }
}

HashGuidsExample.main()



/*
run:

GUIDs in the set:
882C6390-F172-46C0-BEB9-E39757166BE9
A02E8FA5-6017-4491-8F06-E9827657B22F
C30CE7E7-88C8-4364-B79A-F2A90883887F

Hash of the GUID set: 4183881310265061123

*/

 



answered Jan 7 by avibootz
edited Jan 8 by avibootz
...