public static function binarySidToString($value)
    {
        // Revision - 8bit unsigned int (C1)
        // Count - 8bit unsigned int (C1)
        // 2 null bytes
        // ID - 32bit unsigned long, big-endian order
        $sid = @unpack('C1rev/C1count/x2/N1id', $value);

        if (! isset($sid['id']) || ! isset($sid['rev'])) {
            return;
        }

        $revisionLevel = $sid['rev'];

        $identifierAuthority = $sid['id'];

        $subs = isset($sid['count']) ? $sid['count'] : 0;

        $sidHex = $subs ? bin2hex($value) : '';

        $subAuthorities = [];

        // The sub-authorities depend on the count, so only get as
        // many as the count, regardless of data beyond it.
        for ($i = 0; $i < $subs; $i++) {
            $data = implode('', array_reverse(
                str_split(
                    substr($sidHex, 16 + ($i * 8), 8),
                    2
                )
            ));

            $subAuthorities[] = hexdec($data);
        }

        // Tack on the 'S-' and glue it all together...
        return 'S-'.$revisionLevel.'-'.$identifierAuthority.implode(
            preg_filter('/^/', '-', $subAuthorities)
        );
    }
public static function binarySidToString($value)
    {
        // Revision - 8bit unsigned int (C1)
        // Count - 8bit unsigned int (C1)
        // 2 null bytes
        // ID - 32bit unsigned long, big-endian order
        $sid = @unpack('C1rev/C1count/x2/N1id', $value);

        if (! isset($sid['id']) || ! isset($sid['rev'])) {
            return;
        }

        $revisionLevel = $sid['rev'];

        $identifierAuthority = $sid['id'];

        $subs = isset($sid['count']) ? $sid['count'] : 0;

        $sidHex = $subs ? bin2hex($value) : '';

        $subAuthorities = [];

        // The sub-authorities depend on the count, so only get as
        // many as the count, regardless of data beyond it.
        for ($i = 0; $i < $subs; $i++) {
            $data = implode('', array_reverse(
                str_split(
                    substr($sidHex, 16 + ($i * 8), 8),
                    2
                )
            ));

            $subAuthorities[] = hexdec($data);
        }

        // Tack on the 'S-' and glue it all together...
        return 'S-'.$revisionLevel.'-'.$identifierAuthority.implode(
            preg_filter('/^/', '-', $subAuthorities)
        );
    }
public static function binarySidToString ( $ value ) { // Revision - 8bit unsigned int (C1) // Count - 8bit unsigned int (C1) // 2 null bytes // ID - 32bit unsigned long, big-endian order $ sid = @ unpack ( 'C1rev/C1count/x2/N1id' , $ value ) ; if ( ! isset ( $ sid [ 'id' ] ) || ! isset ( $ sid [ 'rev' ] ) ) { return ; } $ revisionLevel = $ sid [ 'rev' ] ; $ identifierAuthority = $ sid [ 'id' ] ; $ subs = isset ( $ sid [ 'count' ] ) ? $ sid [ 'count' ] : 0 ; $ sidHex = $ subs ? bin2hex ( $ value ) : '' ; $ subAuthorities = [ ] ; // The sub-authorities depend on the count, so only get as // many as the count, regardless of data beyond it. for ( $ i = 0 ; $ i < $ subs ; $ i ++ ) { $ data = implode ( '' , array_reverse ( str_split ( substr ( $ sidHex , 16 + ( $ i * 8 ) , 8 ) , 2 ) ) ) ; $ subAuthorities [ ] = hexdec ( $ data ) ; } // Tack on the 'S-' and glue it all together... return 'S-' . $ revisionLevel . '-' . $ identifierAuthority . implode ( preg_filter ( '/^/' , '-' , $ subAuthorities ) ) ; }
Convert a binary SID to a string SID.

@author Chad Sikorra

@link https://github.com/ChadSikorra
@link https://stackoverflow.com/questions/39533560/php-ldap-get-user-sid

@param string $value The Binary SID

@return string|null
Convert a binary SID to a string SID .