/* # Convert binary encoded object SID to a string # (eg. S-1-5-21-1004336348-1177238915-682003330-512) # # SID format: https://technet.microsoft.com/en-us/library/cc962011.aspx # # ldapjs `searchEntry` has attribute `raw` that holds the raw # values, including the `objectSid` buffer when one exists. Pass that # buffer to get the string representation that can then be easily # used in LDAP search filters, for example. # */ let pad = function(s) { if (s.length < 2) { return `0${s}`; } else { return s; } }; let sidBufferToString = function(buf) { let asc, end; let i; if (buf == null) { return null; } let version = buf[0]; let subAuthorityCount = buf[1]; let identifierAuthority = parseInt(((() => { let result = []; for (i = 2; i <= 7; i++) { result.push(buf[i].toString(16)); } return result; })()).join(''), 16); let sidString = `S-${version}-${identifierAuthority}`; for (i = 0, end = subAuthorityCount-1, asc = 0 <= end; asc ? i <= end : i >= end; asc ? i++ : i--) { let subAuthOffset = i * 4; let tmp = pad(buf[11 + subAuthOffset].toString(16)) + pad(buf[10 + subAuthOffset].toString(16)) + pad(buf[9 + subAuthOffset].toString(16)) + pad(buf[8 + subAuthOffset].toString(16)); sidString += `-${parseInt(tmp, 16)}`; } return sidString; };