]> git.ardis.lu - ArdisUtilities/commitdiff
Add Get-SubresourceIntegrity
authorArdis Lu <ardisglu@gmail.com>
Tue, 19 Dec 2023 07:00:06 +0000 (23:00 -0800)
committerArdis Lu <ardisglu@gmail.com>
Tue, 19 Dec 2023 07:00:06 +0000 (23:00 -0800)
ArdisUtilities/ArdisUtilities.psd1
ArdisUtilities/ArdisUtilities.psm1

index 3ec3adfd14c029ada54105fad89cccc106b76aad..65f192d6809b4780a57fa17226ba10ad2765824a 100644 (file)
@@ -4,7 +4,7 @@
   GUID              = '57a6bea3-0ea5-44b3-95d2-71d122772f1c'
   Author            = 'Ardis Lu'
   Description       = 'My personal utility functions.'
-  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperty', 'Get-DocumentationIP', 'Get-DocumentationPhoneNumber', 'Test-TcpPort', 'Get-HelpAsMarkdown', 'Open-ParentFolder')
+  FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperty', 'Get-DocumentationIP', 'Get-DocumentationPhoneNumber', 'Test-TcpPort', 'Get-HelpAsMarkdown', 'Open-ParentFolder', 'Get-SubresourceIntegrity')
   CmdletsToExport   = @()
   VariablesToExport = @()
   AliasesToExport   = @()
index c9ec96aca94aa0a1f6bff822a252c0c9c2398ad9..3fa5f0ca36421b09fb0ec39991e380e85749a1df 100644 (file)
@@ -840,3 +840,91 @@ function Open-ParentFolder {
     explorer.exe ((Get-Command $Command).Source | Split-Path -Parent)
   }
 }
+
+class SubresourceIntegrity {
+  [String]$Algorithm
+  [String]$Hash
+  [String]$Subresource
+  [String]$Integrity
+  [String]$Tag
+}
+
+$SubresourceIntegrityDisplay = @{
+  TypeName                  = 'SubresourceIntegrity'
+  DefaultDisplayPropertySet = 'Subresource', 'Integrity'
+}
+Update-TypeData @SubresourceIntegrityDisplay -Force
+
+function Get-SubresourceIntegrity {
+  <#
+  .SYNOPSIS
+    Gets the hash of a file for use in a subresource integrity (SRI) check.
+
+  .DESCRIPTION
+    Calculates the hash of the given file. Then outputs the hash in base64 with an algorithm prefix,
+    so the hash is ready to be used in a subresource integrity (SRI) check.
+
+  .PARAMETER Subresource
+    A local file to calculate the integrity hash for.
+
+  .PARAMETER Algorithm
+    The hashing algorithm to use. Must be 'SHA256', 'SHA384', or 'SHA512'.
+
+  .EXAMPLE
+    PS> Get-SubresourceIntegrity ./example.js
+    
+    Subresource  Integrity
+    -----------  ---------
+    ./example.js sha512-X/YkDZyjTf4wyc2Vy16YGCPHwAY8rZJY+POgokZjQB2mhIRFJCckEGc6YyX9eNsPfn0PzThEuNs+uaomE5CO6A==
+  
+  .EXAMPLE
+    PS> Get-SubresourceIntegrity ./example.js -Algorithm SHA384
+
+    Subresource  Integrity
+    -----------  ---------
+    ./example.js sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL
+
+  .EXAMPLE
+    PS> @('./example1.js', './example2.js', './example3.js') | Get-SubresourceIntegrity
+
+    Subresource   Integrity
+    -----------   ---------
+    ./example1.js sha512-X/YkDZyjTf4wyc2Vy16YGCPHwAY8rZJY+POgokZjQB2mhIRFJCckEGc6YyX9eNsPfn0PzThEuNs+uaomE5CO6A==
+    ./example2.js sha512-UxkBbtNVXoNNNOwpKb3EdOtjmGNg3YbF6dq3ns6iPdrcy/WxOz+atgbe8USrqZk8vpKu4O2Rdw3U+ctPgcgwZg==
+    ./example3.js sha512-f0Km8/mTw5HtTOWjPoYhh+SCxl7GCgdVE+SvPRzHqNUzzt+QpXTevZzqFC5KKHXSbMny5VR9/qadHYSGluNDEw==
+
+  .LINK
+    https://www.w3.org/TR/SRI/
+
+  .LINK
+    https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
+  #>
+
+  [OutputType([SubresourceIntegrity])]
+  [CmdletBinding()]
+  param(
+    [Parameter(Mandatory, ValueFromPipeline)]
+    [ValidateScript({ Test-Path -LiteralPath $_ -PathType Leaf }, ErrorMessage = "'{0}' is not a valid file.")]
+    [String]$Subresource,
+
+    [ValidateSet('SHA256', 'SHA384', 'SHA512')]
+    [String]$Algorithm = 'SHA512'
+  )
+
+  process {
+    $hash = (Get-FileHash -LiteralPath $Subresource -Algorithm $Algorithm).Hash
+    $bytes = [System.Convert]::FromHexString($hash)
+    $b64 = [System.Convert]::ToBase64String($bytes)
+
+    $integrity = "$($Algorithm.ToLower())-$b64"
+    $tag = "<script href=`"`" integrity=`"$integrity`" crossorigin></script>"
+
+    [SubresourceIntegrity] @{
+      Algorithm   = $Algorithm
+      Hash        = $hash
+      Subresource = $Subresource
+      Integrity   = $integrity
+      Tag         = $tag
+    }
+  }
+}
This page took 0.036916 seconds and 4 git commands to generate.