muffinman has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I'm talking to active directory through Net::LDAP, and I want to extract user GUIDs in a readable form.

If I query a user, I don't get a GUID field, but instead a nasty contraption called objectGUID. This, I believe is supposed to be a 128bit binary field. In a debugger, it looks like this:

  'objectGUID' => 'Q?Ot?qM?r/?'

I have a GuidToString routine, filthily stolen from the internets, which I have posted below. In a nutshell, I'm looking for a hex string in the form: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

The below code gives me that, but something is just not right, especially as most of the strings I get back have > 8 0's on the end in most cases.

Has anybody played with this before? Afaik, guid is the only thing that can never change on a users ad account, and I need to record these for a intranet based web app.

Any help much appreciated...

Here's that routine...

sub GuidToString { return sprintf "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%0 +2X", unpack("I", $_[0]), unpack("S", substr($_[0], 4, 2)), unpack("S", substr($_[0], 6, 2)), unpack("C", substr($_[0], 8, 1)), unpack("C", substr($_[0], 9, 1)), unpack("C", substr($_[0], 10, 1)), unpack("C", substr($_[0], 11, 1)), unpack("C", substr($_[0], 12, 1)), unpack("C", substr($_[0], 13, 1)), unpack("C", substr($_[0], 14, 1)), unpack("C", substr($_[0], 15, 1)); }

Replies are listed 'Best First'.
Re: Net::LDAP GUID...
by Anonymous Monk on Apr 06, 2011 at 20:54 UTC
      sub GuidToString
      {
          my $stringGUID = unpack("H*", shift);
          $stringGUID =~ s/^(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w\w\w)}/$4$3$2$1-$6$5-$8$7-$9-/;
          return $stringGUID;
      }
      
        I like the GuidToString sub but the extra curly brace was a little tough to find. This works great. sub GuidToString { my $stringGUID = unpack("H*", shift); $stringGUID =~ s/^(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w)(\w\w\w\w)/$4$3$2$1-$6$5-$8$7-$9-/; return $stringGUID; } Thanks,