Skip to content

Instantly share code, notes, and snippets.

@phbits
Created January 18, 2021 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phbits/ee9da83c7d826ecef4faad8e459d7485 to your computer and use it in GitHub Desktop.
Save phbits/ee9da83c7d826ecef4faad8e459d7485 to your computer and use it in GitHub Desktop.
Function Get-CspSha256Hash
{
<#
.SYNOPSIS
Generates SHA256 hash for Content Security Policy (CSP).
.DESCRIPTION
Creates a SHA256 hash of provided inline text to satisfy CSP.
.EXAMPLE
$Style = "html, body { height: 100%; font-family: 'Courier'; } .listed-item { display: flex; justify-content: center; align-items: center; }"
Get-CspSha256Hash $Style
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
#Text to hash
$Text
)
$CSP = New-Object System.Security.Cryptography.SHA256CryptoServiceProvider
$TextBytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
$HashBytes = $CSP.ComputeHash($TextBytes)
$CSPHash = [System.Convert]::ToBase64String($HashBytes)
$Result = 'sha256-{0}' -f $CSPHash
return $Result
} # End Function Get-CspSha256Hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment