Created
January 18, 2021 01:08
-
-
Save phbits/ee9da83c7d826ecef4faad8e459d7485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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