Skip to content

Get-StringHash (something like Get-FileHash but not file based) #12531

@Conan1989

Description

@Conan1989

Summary of the new feature/enhancement

Hi,
Request a native cmdlet for getting a hash value (MD5, SHA256, etc) of an inputted string.
eg - something like Get-FileHash ... but not file based

justification. I've seen requirements for such a thing around the place, but with incorrectly implemented (hopefully the below isn't too far off) and sub optimal solutions. eg bad code from SE and write to disk, hash from disk with Get-FileHash

Proposed technical implementation details (optional)

something like the following maybe

function Get-StringHash {
    [CmdletBinding()]
    [OutputType([System.String])]
    param (
        [ValidateScript({ ![System.String]::IsNullOrEmpty($PSItem) })][System.String]$inputString,
        [ValidateSet('MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512')][System.String]$hashAlgo
    )
    process {
        $ErrorActionPreference = 'stop'
        Set-StrictMode -Version 'latest'

        $inputBytes    = [System.Text.Encoding]::UTF8.GetBytes($inputString)
        $hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create($hashAlgo)
        # [System.Security.Cryptography.HashAlgorithmName]

        return ( [System.BitConverter]::ToString( $hashAlgorithm.ComputeHash($inputBytes) ) -replace '-' )
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions