Skip to content

Instantly share code, notes, and snippets.

@heaths
Created January 27, 2018 17:36
Show Gist options
  • Save heaths/7470fc582dcfcc9c87f65e013432d3c3 to your computer and use it in GitHub Desktop.
Save heaths/7470fc582dcfcc9c87f65e013432d3c3 to your computer and use it in GitHub Desktop.
How to generate a durable GUID
<#
Copyright 2018 Heath Stewart
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
[CmdletBinding()]
[OutputType([System.Guid])]
param (
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[string[]] $InputObject,
[Parameter()]
[switch] $CaseSensitive,
[Parameter()]
[switch] $Fips
)
begin {
$AllInputObjects = @()
}
process {
$AllInputObjects += foreach ($str in $InputObject) {
if ($CaseSensitive) {
$str
} else {
$str.ToLower()
}
}
}
end {
$hashAlg = if ($Fips) {
[System.Security.Cryptography.SHA1]::Create()
} else {
try {
[System.Security.Cryptography.MD5]::Create()
} catch [System.InvalidOperationException] {
Write-Warning 'FIPS-140 machine policy requires switching to FIPS-compliant algorithm'
[System.Security.Cryptography.SHA1]::Create()
}
}
$buffer = foreach ($str in $AllInputObjects) {
[System.Text.Encoding]::Unicode.GetBytes($str)
}
[byte[]] $hash = $hashAlg.ComputeHash($buffer)[0..15]
New-Object System.Guid -ArgumentList @(,$hash)
}
<#
.SYNOPSIS
Computes a new GUID that will be the same when given the same input.
.DESCRIPTION
Globally-unique identifiers (GUIDs) are generally useful when you need a unique identifier. Often times, however, they are used as a key and must be the same when representing the same keyed object. Using this cmdlet you can compute such a GUID that will be the same when given the same input and using the same algorithm (see documentation for the -Fips parameter).
.PARAMETER InputObject
One or more strings used to compute a durable GUID.
.PARAMETER CaseSensitive
Computes unique durable GUIDs for inputs of different cases. The default is case-insensitive computation.
.PARAMETER Fips
Force computation of a durable GUID using a FIPS-compliant algorithm. This is automatic on systems configured for FIPS compliance. Since FIPS-compliant algorithms generate longer hashes, using FIPS-compliant GUIDs decreases entropy and has a higher probability of collisions.
.EXAMPLE
New-DurableGuid foo
Generates a durable GUID for 'foo'.
.EXAMPLE
New-DurableGuid salt, foo
Generates a durable GUID for 'foo'. You can specify a salt or namespace to distinguish GUIDs when the input value might otherwise be th same.
.EXAMPLE
New-DurableGuid foo -Fips
Generates a durable GUID using a FIPS-compliant algorithm. This is automatic on systems configured for FIPS compliance.
#>
<#
Copyright 2018 Heath Stewart
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
#Requires -Modules @{ModuleName = 'Pester'; ModuleVersion = '4.1.1'}
Describe 'New-DurableGuid.ps1' {
Context 'CaseSensitive' {
It 'Computes durable GUID for lower-case parameter' {
.\New-DurableGuid.ps1 -CaseSensitive -Fips foo | Should -Be ([guid]'9b0b1cb9-3401-6817-775b-9133b367c86c')
}
It 'Computes durable GUID for upper-case parameter' {
.\New-DurableGuid.ps1 -CaseSensitive -Fips FOO | Should -Be ([guid]'9d5065b5-074f-2db8-590b-45d1e58592de')
}
}
Context 'Fips' {
It 'Computes durable GUID for single unnamed parameter' {
.\New-DurableGuid.ps1 -Fips foo | Should -Be ([guid]'9b0b1cb9-3401-6817-775b-9133b367c86c')
}
It 'Computes durable GUID for single named parameter' {
.\New-DurableGuid.ps1 -Fips -InputObject foo | Should -Be ([guid]'9b0b1cb9-3401-6817-775b-9133b367c86c')
}
It 'Computes durable GUID for multiple parameter' {
.\New-DurableGuid.ps1 -Fips foo, bar | Should -Be ([guid]'e90390ea-9459-ea48-b0db-3ae9bc1bf665')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment