Skip to content

Instantly share code, notes, and snippets.

@simonneutert
Forked from Krizzzn/sid.js
Created June 7, 2021 14:56
Show Gist options
  • Select an option

  • Save simonneutert/0e9a037b7036b5fc137f598f72e52e30 to your computer and use it in GitHub Desktop.

Select an option

Save simonneutert/0e9a037b7036b5fc137f598f72e52e30 to your computer and use it in GitHub Desktop.

Revisions

  1. @Krizzzn Krizzzn revised this gist Jun 19, 2017. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions sid.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    /*
    # 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) {
  2. @Krizzzn Krizzzn revised this gist Jun 19, 2017. 2 changed files with 31 additions and 32 deletions.
    32 changes: 0 additions & 32 deletions sid.coffee
    Original file line number Diff line number Diff line change
    @@ -1,32 +0,0 @@
    # 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.
    #

    pad = (s) -> if s.length < 2 then "0#{s}" else s

    sidBufferToString = (buf) ->
    return null unless buf?

    version = buf[0]
    subAuthorityCount = buf[1]
    identifierAuthority = parseInt((buf[i].toString(16) for i in [2..7]).join(''), 16)

    sidString = "S-#{version}-#{identifierAuthority}"

    for i in [0..subAuthorityCount-1]
    subAuthOffset = i * 4
    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)

    sidString
    31 changes: 31 additions & 0 deletions sid.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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;
    };
  3. @vesse vesse revised this gist May 19, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions sid.coffee
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,9 @@
    # buffer to get the string representation that can then be easily
    # used in LDAP search filters, for example.
    #

    pad = (s) -> if s.length < 2 then "0#{s}" else s

    sidBufferToString = (buf) ->
    return null unless buf?

  4. @vesse vesse created this gist May 19, 2015.
    29 changes: 29 additions & 0 deletions sid.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    # 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.
    #
    sidBufferToString = (buf) ->
    return null unless buf?

    version = buf[0]
    subAuthorityCount = buf[1]
    identifierAuthority = parseInt((buf[i].toString(16) for i in [2..7]).join(''), 16)

    sidString = "S-#{version}-#{identifierAuthority}"

    for i in [0..subAuthorityCount-1]
    subAuthOffset = i * 4
    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)

    sidString