LdapUtils { public static String convertBinarySidToString(byte[] sid) { StringBuffer sidAsString = new StringBuffer("S-"); sidAsString.append(sid[0]).append('-'); StringBuffer sb = new StringBuffer(); for (int t = 2; t <= 7; t++) { String hexString = Integer.toHexString(sid[t] & 0xFF); sb.append(hexString); } sidAsString.append(Long.parseLong(sb.toString(), HEX)); int count = sid[1]; for (int i = 0; i < count; i++) { int currSubAuthOffset = i * 4; sb.setLength(0); sb.append(toHexString((byte) (sid[11 + currSubAuthOffset] & 0xFF))); sb.append(toHexString((byte) (sid[10 + currSubAuthOffset] & 0xFF))); sb.append(toHexString((byte) (sid[9 + currSubAuthOffset] & 0xFF))); sb.append(toHexString((byte) (sid[8 + currSubAuthOffset] & 0xFF))); sidAsString.append('-').append(Long.parseLong(sb.toString(), HEX)); } return sidAsString.toString(); } private  LdapUtils(); static void closeContext(DirContext context); static NamingException convertLdapException(javax.naming.NamingException ex); static Class getActualTargetClass(DirContext context); static void collectAttributeValues(Attributes attributes, String name, Collection<Object> collection); static void collectAttributeValues(
            Attributes attributes, String name, Collection<T> collection, Class<T> clazz); static void iterateAttributeValues(Attribute attribute, AttributeValueCallbackHandler callbackHandler); static String convertCompositeNameToString(
			CompositeName compositeName); static LdapName newLdapName(Name name); static LdapName newLdapName(String distinguishedName); static LdapName removeFirst(Name dn, Name pathToRemove); static LdapName prepend(Name dn, Name pathToPrepend); static LdapName emptyLdapName(); static Rdn getRdn(Name name, String key); static Object getValue(Name name, String key); static Object getValue(Name name, int index); static String getStringValue(Name name, int index); static String getStringValue(Name name, String key); static String convertBinarySidToString(byte[] sid); static byte[] convertStringSidToBinary(String string);  }
@Test public void testConvertBinarySidToString() throws Exception { byte[] sid = {(byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x15, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xe9, (byte) 0x67, (byte) 0xbb, (byte) 0x98, (byte) 0xd6, (byte) 0xb7, (byte) 0xd7, (byte) 0xbf, (byte) 0x82, (byte) 0x05, (byte) 0x1e, (byte) 0x6c, (byte) 0x28, (byte) 0x06, (byte) 0x00, (byte) 0x00}; String result = LdapUtils.convertBinarySidToString(sid); assertThat(result).isEqualTo("S-1-5-21-2562418665-3218585558-1813906818-1576"); }
@Test public void testConvertAnotherBinarySidToString() throws Exception { byte[] sid = {(byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x15, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa0, (byte) 0x65, (byte) 0xcf, (byte) 0x7e, (byte) 0x78, (byte) 0x4b, (byte) 0x9b, (byte) 0x5f, (byte) 0xe7, (byte) 0x7c, (byte) 0x87, (byte) 0x70, (byte) 0x09, (byte) 0x1c, (byte) 0x01, (byte) 0x00}; String result = LdapUtils.convertBinarySidToString(sid); assertThat(result).isEqualTo("S-1-5-21-2127521184-1604012920-1887927527-72713"); }
@Test public void testConvertHandCraftedBinarySidToString() throws Exception { byte[] sid = {(byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x15, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00}; String result = LdapUtils.convertBinarySidToString(sid); assertThat(result).isEqualTo("S-1-5-21-1-2-3-4"); }