Question:
I want to make a string fit for consumption as a stream, by e.g. Get-FileHash
command (using its -InputStream
parameter).
The reason I want this is that I need to digest a string, and not contents of a file. I do not want to temporarily store said string in a file just to digest it.
I know this can be done with some half a dozen .NET objects, which is trivially supported by Powershell, but I am just curious whether there is an existing PS command or combination thereof that turns a string object into a stream, so I can do something like the following (assuming the hypothetical Convert
turns the string into a stream)?
1 2 |
Get-FileHash -InputStream (Convert "Quick brown fox jumps over the lazy dog.") -Algorithm SHA512 |
Answer:
You can use some .NET functions:
1 2 3 |
$stream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes("Quick brown fox jumps over the lazy dog.")) Get-FileHash -InputStream $stream -Algorithm SHA512 |