Skip to content

Instantly share code, notes, and snippets.

@nmoinvaz
Last active April 30, 2020 21:28
Show Gist options
  • Save nmoinvaz/80cf2b6b07cadee8af2761b987dde199 to your computer and use it in GitHub Desktop.
Save nmoinvaz/80cf2b6b07cadee8af2761b987dde199 to your computer and use it in GitHub Desktop.
Using PowerShell to generate new code signing certificate with base64 thumbprint

To create the code signing certificate using PowerShell (using Administrator prompt):

$cert = New-SelfSignedCertificate -Subject "My Certificate" -Type CodeSigning -CertStoreLocation Cert:\CurrentUser\My -NotAfter (Get-Date).AddYears(100)

To export the certificate from the certificate store:

$certPassword = ConvertTo-SecureString -String "passwordhere" -Force –AsPlainText
$cert | Export-PfxCertificate -FilePath "mycert.pfx" -Password $certPassword

To get the base64 string of the SHA1 thumbprint of a PFX certificate use the following:

$publicKey = (Get-PfxCertificate -FilePath mycert.pfx).GetPublicKey()
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$hash = $sha1.ComputeHash($publicKey)
[System.Convert]::ToBase64String($hash)

To get the base64 string of the SHA1 thumbprint of a PEM certificate use the following:

$publicKeyBase64 = [String]::Join("", (Get-Content -Path mycert.pem)[1..7])
$publicKey = [Convert]::FromBase64String($publicKeyBase64)
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$hash = $sha1.ComputeHash($publicKey)
[System.Convert]::ToBase64String($hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment