From b475e21f03cace2597f80a9b11bd9a5015d96bd0 Mon Sep 17 00:00:00 2001 From: "Rodweil, Theodor" Date: Sun, 6 Aug 2023 22:29:27 +0200 Subject: [PATCH] feat(String): add Sting Helper functions --- PSConfluencePublisher/String.Tests.ps1 | 21 +++++++++++++ PSConfluencePublisher/String.psm1 | 42 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 PSConfluencePublisher/String.Tests.ps1 create mode 100755 PSConfluencePublisher/String.psm1 diff --git a/PSConfluencePublisher/String.Tests.ps1 b/PSConfluencePublisher/String.Tests.ps1 new file mode 100755 index 0000000..de93dfa --- /dev/null +++ b/PSConfluencePublisher/String.Tests.ps1 @@ -0,0 +1,21 @@ +#!/usr/bin/env pwsh +$ErrorActionPreference = "Stop" + +BeforeAll { + Import-Module (Join-Path $PSScriptRoot 'PSConfluencePublisher.psd1') -Force +} + +Describe 'Get-StringHash' ` +{ + Context 'default' ` + { + It 'works' ` + { + $result = Get-StringHash 'foobar' + + $result.Hash | Should -Be ( + 'C3AB8FF13720E8AD9047DD3946' + ` + '6B3C8974E592C2FA383D4A3960714CAEF0C4F2') + } + } +} diff --git a/PSConfluencePublisher/String.psm1 b/PSConfluencePublisher/String.psm1 new file mode 100755 index 0000000..0eb6565 --- /dev/null +++ b/PSConfluencePublisher/String.psm1 @@ -0,0 +1,42 @@ +#!/usr/bin/env pwsh + +function Get-StringHash +{ + <# + .SYNOPSIS + Get hash value of a string + + .DESCRIPTION + The Get-StringHash function is just a wrapper around the + Get-FileHash function and utilizes a stream for providing said + function with proper input values. + + .OUTPUTS + Same as the Get-FileHash function + + .EXAMPLE + Get-StringHash 'foobar' -Algorithm 'SHA256' + #> + Param( + [Parameter(Mandatory, Position = 0)] [String] $InputString, + [Parameter()] [String] $Algorithm = 'SHA256' + ) + + Begin + { + $stream = [IO.MemoryStream]::New() + + $writer = [IO.StreamWriter]::New($stream) + + $writer.Write($InputString) + + $writer.Flush() + + $stream.Position = 0 + } + + Process + { + Get-FileHash -InputStream $stream -Algorithm $Algorithm + } +}