Skip to content

Instantly share code, notes, and snippets.

@nedyalkov
Created September 11, 2014 08:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedyalkov/a563dd4fb04d21cb91dc to your computer and use it in GitHub Desktop.
Save nedyalkov/a563dd4fb04d21cb91dc to your computer and use it in GitHub Desktop.
SignFile with Sha1 algorithm when using Sha256 certificate
function SignFile($filePath, $timeStampUri, $certThumbprint)
{
$x509Store = New-Object -TypeName ([System.Security.Cryptography.X509Certificates.X509Store]) -ArgumentList ([System.Security.Cryptography.X509Certificates.StoreName]::My),([System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser)
try
{
$x509Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
$x509Certificate2Collection = $x509Store.Certificates.Find([System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint, $certThumbprint, $false);
if ($x509Certificate2Collection.Count -eq 1)
{
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]@($x509Certificate2Collection)[0]
# This will force using of SHA1 instead of SHA256
$cert.SignatureAlgorithm.FriendlyName = ""
Add-Type -AssemblyName "Microsoft.Build.Tasks.v4.0"
echo "Calling SignFile with file $filePath"
if(Test-Path $filePath)
{
echo "File to be signed exists"
}
[Microsoft.Build.Tasks.Deployment.ManifestUtilities.SecurityUtilities]::SignFile($cert, $timeStampUri, $filePath)
}
}
finally
{
$x509Store.Close();
}
}
@rodri-oliveira-dev
Copy link

I need help with this script . Could you help me ?

@nedyalkov
Copy link
Author

Hey @RodrigoDotNet, I'm really sorry, but I didn't notice your comment here.

I'd love to help if you still need my help, of course 😄. My email is miroslav.nedialkov@gmail.com, so just drop me a line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment