Skip to content

Allow New-Guid to accept optional byte array #15317

Closed
@chelnak

Description

@chelnak

Summary of the new feature/enhancement

As a user I would like New-Guid to accept an optional custom byte array at creation time, allowing me to control the bytes that are used to create the Guid object.

Proposed technical implementation details (optional)

For example something like...

$Bytes = [System.Byte[]]::new(16)
$RNG = [System.Security.Cryptography.RNGCryptoServiceProvider]::new()
$RNG.GetBytes($Bytes)

$VersionByteIndex = [System.BitConverter]::IsLittleEndian ? 7 : 6
$Bytes[$VersionByteIndex] = $Bytes[$VersionByteIndex] -band [char]0x0F -bxor [char]0x40
$Bytes[9] = $Bytes[9] -band 0x3F -bxor 0x80

New-Guid -Bytes $Bytes

The underlying c# implementation would have a code path that calls new Guid(Byte[]).

https://docs.microsoft.com/en-us/dotnet/api/system.guid.-ctor?view=net-5.0#System_Guid__ctor_System_Byte___

Also if you want to ensure that the result is v4 RFC 4122 compliant then some sort of validation would be needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-AnsweredThe question is answered.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions