7

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)?

Get-FileHash -InputStream (Convert "Quick brown fox jumps over the lazy dog.") -Algorithm SHA512
2
  • 3
    Get-FileHash -InputStream ([IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes("Quick brown fox jumps over the lazy dog."))) -Algorithm SHA512 Commented Jan 18, 2018 at 14:08
  • 1
    Please make it an answer, as it rightfully should be! Commented Jan 18, 2018 at 14:11

2 Answers 2

13

You can use some .NET functions:

$stream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes("Quick brown fox jumps over the lazy dog."))
Get-FileHash -InputStream $stream -Algorithm SHA512
Sign up to request clarification or add additional context in comments.

Comments

1

There is a script in the Microsoft Script Gallery that does exactly what you ultimately want to do - that is, it takes a string and returns the hash for the string. See Get-StringHash in the Technet Powershell Script Gallery.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.