Skip to content

Instantly share code, notes, and snippets.

@nmoinvaz
Last active January 22, 2023 18:11
Show Gist options
  • Select an option

  • Save nmoinvaz/92aaae57bba55de863ba3af97a2061e8 to your computer and use it in GitHub Desktop.

Select an option

Save nmoinvaz/92aaae57bba55de863ba3af97a2061e8 to your computer and use it in GitHub Desktop.

Revisions

  1. nmoinvaz revised this gist Jun 26, 2021. No changes.
  2. nmoinvaz revised this gist Apr 30, 2020. No changes.
  3. nmoinvaz revised this gist Apr 30, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions powershell-cert.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ## Self-signed Code Signing Certificate Creation
    ## Self-signed Code Signing Certificate

    To create the code signing certificate using PowerShell (using Administrator prompt):
    ```
    @@ -10,7 +10,7 @@ $certPassword = ConvertTo-SecureString -String "passwordhere" -Force –AsPlainT
    $cert | Export-PfxCertificate -FilePath "mycert.pfx" -Password $certPassword
    ```

    ## PFX Public Key Extraction
    ## Public Key Extraction

    To retrieve the public key from a PFX certificate using Powershell, use the following command:
    ```
  4. nmoinvaz created this gist Apr 30, 2020.
    43 changes: 43 additions & 0 deletions powershell-cert.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    ## Self-signed Code Signing Certificate Creation

    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
    ```

    ## PFX Public Key Extraction

    To retrieve the public key from a PFX certificate using Powershell, use the following command:
    ```
    $publicKey = (Get-PfxCertificate -FilePath mycert.pfx).GetPublicKey()
    ```

    ## Hex Thumbprint

    To convert the public key to a hex string without hyphens you can use this command:
    ```
    [System.BitConverter]::ToString($publicKey).Replace("-", "")
    ```

    ## Base64 Thumbprint

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