How to convert Windows SID into ObjectId in nodejs?

178 views Asked by At

I need to convert Windows security identifier into the object Id. It is available in power shell but not in nodejs

This is power shell https://github.com/okieselbach/Intune/blob/master/Convert-AzureAdSidToObjectId.ps1

sid is "S-1-12-1-1943430372-1249052806-2496021943-3034400218"

objectId 73d664e4-0886-4a73-b745-c694da45ddb4

2

There are 2 answers

0
Etay Ceder On

It would be best if you imported the GUID library since UUID can't parse GUIDs. I used the js-guid library

function convertToObjectId(sid) {
  const sidText = sid.replace('S-1-12-1-', ''); // $text = $sid.Replace('S-1-12-1-', '')
  const numbers = new Uint32Array(sidText.split('-').map(str => parseInt(str))); // $array = [UInt32[]]$text.Split('-')
  const bytes = new Uint8Array(numbers.buffer); //     [Buffer]::BlockCopy($array, 0, $bytes, 0, 16)
  return Guid.parse(bytes); //     [Guid]$guid = $bytes
}

0
Josjr87 On

For those who are interested, the reverse version:

const objectId = this._formData.objectId;
const ar8 = Guid.parse(objectId) as Uint8Array;
const ar32 = new Uint32Array(ar8.buffer);
const sid = `S-1-12-1-${ar32.join('-')}`;