Skip to content

Instantly share code, notes, and snippets.

@simonneutert
Created June 7, 2021 14:58
Show Gist options
  • Select an option

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

Select an option

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

Revisions

  1. simonneutert created this gist Jun 7, 2021.
    22 changes: 22 additions & 0 deletions formatObjectGUID.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // source: https://github.com/ldapjs/node-ldapjs/issues/297#issuecomment-137765214

    const formatGUID = function (objectGUID) {
    var data = Buffer.from(objectGUID, 'binary');

    // GUID_FORMAT_D
    var template = '{3}{2}{1}{0}-{5}{4}-{7}{6}-{8}{9}-{10}{11}{12}{13}{14}{15}';

    // check each byte
    for (var i = 0; i < data.length; i++) {
    // get the current character from that byte
    var dataStr = data[i].toString(16);
    dataStr = data[i] >= 16 ? dataStr : '0' + dataStr;

    // insert that character into the template
    template = template.replace(new RegExp('\\{' + i + '\\}', 'g'), dataStr);
    }

    return template;
    };

    module.exports = formatGUID;