I've been testing everything I find for a couple of days to correctly decode the objectGUID. I try to obtain an Object later converting it to a Byte [], I am not sure if it is the correct process to decode it and obtain the value as it is shown in the Active Directory since I tested but at the time of decoding it shows a different value to the which appears in its respective user in AD. If someone could guide me to have the right process by which I will get the right value or can briefly explain what I am doing wrong, I am open to any solution.
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(ldapSearchBase, searchFilter, searchControls);
//atributos [] contains the names of the attributes that I need to consult
while(results.hasMoreElements()){
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
for (int i = 0;i<=atributos.length;i++){
if(i<atributos.length){
if(atributos[i].equals("objectGUID")){
try {
Object c = attributes.get("objectGUID").get();
decodeEntryUUID(c);
content.put(attributes.get(atributos[i]).getID(),decodeEntryUUID(c));
}catch (NullPointerException e){
System.err.println("Problem listing attributes: " + e);
}
public String decodeEntryUUID(Object entryUUID) throws IOException {
String ret = "x";
ret=decodeGuid(serializeObject(entryUUID));
return ret;
}
public static String decodeGuid(byte[] guid) {
byte[] withBigEndian = new byte[] { guid[3], guid[2], guid[1], guid[0],
guid[5], guid[4],
guid[7], guid[6],
guid[8], guid[9], guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]
};
return convertToDashedString(withBigEndian);
}
public static String decodeObjectGUID(byte[] objectGUID) {
StringBuilder displayStr = new StringBuilder();
displayStr.append(convertToDashedString(objectGUID));
System.out.println(displayStr.toString()+" 3");
return displayStr.toString();
}
private static String convertToDashedString(byte[] objectGUID) {
StringBuilder displayStr = new StringBuilder();
displayStr.append(prefixZeros((int) objectGUID[3] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[2] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[1] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[0] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[5] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[4] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[7] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[6] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[8] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[9] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[10] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[11] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[12] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[13] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[14] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[15] & 0xFF));
return displayStr.toString();
}
private static String prefixZeros(int value) {
if (value <= 0xF) {
StringBuilder sb = new StringBuilder("0");
sb.append(Integer.toHexString(value));
return sb.toString();
} else {
return Integer.toHexString(value);
}
}