@shi-corp/objectid-converter
Version:
Converts Microsoft Object IDs to and from SIDs.
15 lines (14 loc) • 737 B
JavaScript
import { parse } from 'uuid';
export function convertToSid(objectId) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/gui;
if (typeof objectId !== 'string' || !uuidRegex.test(objectId)) {
throw new Error('The specified object ID is not a valid UUID v4!');
}
const bytes = [...parse(objectId)];
[bytes[4], bytes[5]] = [bytes[5], bytes[4]];
[bytes[6], bytes[7]] = [bytes[7], bytes[6]];
const dataView = new DataView(new Uint8Array(bytes).buffer);
const sidComponents = Array.from({ 'length': 4 }, (_element, index) => dataView.getUint32(index * 4, index > 0));
const resultantSid = `S-1-12-1-${sidComponents.join('-')}`;
return resultantSid;
}