1

Using openssl I generated a private key (let's say httpCert.key) and a self signed certificate. Now, I have a string which I want to hash256, digitally sign with my private key i.e. httpCert.key and do a base64 encoding.

I can do this all in bash using openssl as below:

signature=`printf "test" | openssl dgst -sha256 -sign httpCert.key | openssl base64 -A`

But struggling to find a way to do this in Powershell.

Based on a help from other post, I can do a simple hash and encode as below:

$hasher=[System.Security.Cryptography.HashAlgorithm]::Create('sha256')
$signSHA=$hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("test"))
$signature=[Convert]::ToBase64String($signSHA)

But not sure how to sign the hash256 with a private key. I tried using "System.Security.Cryptography.RSACryptoServiceProvider" but couldn't get it working.

aashleo
  • 63
  • 1
  • 5
  • Maybe this is helpful for you: https://stackoverflow.com/questions/55284511/generating-an-rsa-key-pair-in-powershell – f6a4 Nov 15 '19 at 11:59

1 Answers1

1
$Certificate = [Security.Cryptography.X509Certificates.X509Certificate2]::new("g:\Drive\ff.pfx","")
$data=[System.Text.Encoding]::UTF8.GetBytes("test")
$sData=$Certificate.PrivateKey.SignData($Data,'SHA1')
[Convert]::ToBase64String($sData)
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Dec 27 '20 at 16:00