从objectSid转换为SID表示的Python代码

2024-10-01 15:43:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从LDAP查询检索base64编码的objectSid到activedirectory数据库,并将它们转换为标准的SID表示形式。你能给我一个Python代码片段吗?在


Tags: 代码数据库编码标准ldap形式base64sid
3条回答

在ldap3中使用或查看实现 ldap-doc
source

ldap3.protocol.formatters.formatters.format_sid

这是@Giovanni Mascellani的答案,适用于Python 3.x:

import struct

def convert(binary):
    version = struct.unpack('B', binary[0:1])[0]
    # I do not know how to treat version != 1 (it does not exist yet)
    assert version == 1, version
    length = struct.unpack('B', binary[1:2])[0]
    authority = struct.unpack(b'>Q', b'\x00\x00' + binary[2:8])[0]
    string = 'S-%d-%d' % (version, authority)
    binary = binary[8:]
    assert len(binary) == 4 * length
    for i in range(length):
        value = struct.unpack('<L', binary[4*i:4*(i+1)])[0]
        string += '-%d' % value
    return string

这应该可以做到:

import struct

def convert(binary):
    version = struct.unpack('B', binary[0])[0]
    # I do not know how to treat version != 1 (it does not exist yet)
    assert version == 1, version
    length = struct.unpack('B', binary[1])[0]
    authority = struct.unpack('>Q', '\x00\x00' + binary[2:8])[0]
    string = 'S-%d-%d' % (version, authority)
    binary = binary[8:]
    assert len(binary) == 4 * length
    for i in xrange(length):
        value = struct.unpack('<L', binary[4*i:4*(i+1)])[0]
        string += '-%d' % value
    return string

引用:http://blogs.msdn.com/b/oldnewthing/archive/2004/03/15/89753.aspx和{a2}。在

相关问题 更多 >

    热门问题