Search code examples
coldfusionactive-directoryuuid

How to convert Active Directory objectGUID to UUID in Coldfusion


I have a CFLDAP query returning the objectGUID. How can I convert this to a valid UUID string in a 8-4-4-4-12 pattern in ColdFusion?

I have specified returnAsBinary="ObjectGUID", but toString(getLDAP.ObjectGUID) does not return the desired result.

Update:

I tried binaryEncode():

<cfset guid = binaryencode(getLDAP.objectguid,"HEX")> 

Which returns:

18E0CE3388B79C4EA4D73894AE8CD8F6

But I'm expecting this (was extracted and supplied by another process that I cant see their conversion steps).

3cee018-b788-4e9c-a4d7-3894ae8cd8f6 

Hummm... Although they don't match, the last half is the same. a4d7-3894ae8cd8f6.


Solution

  • hummm... the last half is the same

    Interesting. A link from this thread explains why. Apparently, it is more involved than just converting the binary into hex, in one shot:

    1. First, know the sequence of the byte index that forms the Dashed String: [3] [2] [1] [0] - [5] [4] - [7] [6] - [8] [9] - [10] [11] [12] [13] [14] [15]
    2. Next, apply bit masking to each and every single byte value accessed in the array.
    3. Follow by converting to the Hex representation of the value.
    4. Just make sure that the Hex value is a double digit value, meaning instead of "A", it should be "0A"

    Since CF's arrays are 1-based, just add +1 to the positions. This builds the decoded string in the proper sequence (sans dashes, which you can easily add with string functions).

     // Get GUID binary
     bin = yourQuery.objectGUID[rowNumber];
     // Extract bytes in this sequence
     order = [4,3,2,1, 6,5, 8,7, 9,10,11,12,13,14,15,16];
     // Stores converted bytes
     hex = [];
    
     for (pos in order) {
        // Apply mask to current byte and convert to hext 
        arrayAppend(hex, formatBaseN(BitAnd(bin[pos], 255), 16));
     }
    
     writeOutput("Hex string = "& arrayToList(hex, ""));