Last active
March 29, 2016 08:35
-
-
Save roubachof/452bbb983d7d2359019e to your computer and use it in GitHub Desktop.
Signing dll from nuget packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
namespace Autosign | |
{ | |
public static class ProcessUtil | |
{ | |
public static void Execute(string exePath, string arguments) | |
{ | |
// Start the child process. | |
Process p = new Process(); | |
// Redirect the output stream of the child process. | |
p.StartInfo.UseShellExecute = false; | |
p.StartInfo.RedirectStandardOutput = true; | |
p.StartInfo.FileName = exePath; | |
p.StartInfo.Arguments = arguments; | |
p.Start(); | |
// Do not wait for the child process to exit before | |
// reading to the end of its redirected stream. | |
// p.WaitForExit(); | |
// Read the output stream first and then wait. | |
string output = p.StandardOutput.ReadToEnd(); | |
p.WaitForExit(); | |
} | |
} | |
public static class PathUtil | |
{ | |
public static string MakeRelativePath(string fromPath, string toPath) | |
{ | |
if (string.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath"); | |
if (string.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath"); | |
var fromUri = new Uri(fromPath); | |
var toUri = new Uri(toPath); | |
if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative. | |
var relativeUri = fromUri.MakeRelativeUri(toUri); | |
var relativePath = Uri.UnescapeDataString(relativeUri.ToString()); | |
if (toUri.Scheme.ToUpperInvariant() == "FILE") | |
{ | |
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); | |
} | |
return relativePath; | |
} | |
} | |
public class FileToSign | |
{ | |
private static string ILDasmPath = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe"; | |
private static string ILAsmPath = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe"; | |
private readonly string _filepath; | |
private readonly string _rootPath; | |
private readonly string _tempPath; | |
private readonly string _outPath; | |
private readonly string _keyPath; | |
public FileToSign(string filepath, string rootPath, string tempPath, string outPath, string keyPath) | |
{ | |
_filepath = filepath; | |
_rootPath = rootPath; | |
_tempPath = tempPath; | |
_outPath = outPath; | |
_keyPath = keyPath; | |
} | |
public string Filepath => _filepath; | |
public string Filename => Path.GetFileName(Filepath); | |
public string Extension => Path.GetExtension(Filepath); | |
public string RelativePath => PathUtil.MakeRelativePath(_rootPath, Filepath); | |
public string TempFullPath => Path.Combine(_tempPath, RelativePath); | |
public string OutFullPath => Path.Combine(_outPath, RelativePath); | |
private void Disassemble() | |
{ | |
var path = Directory.GetParent(TempFullPath).FullName; | |
if (!Directory.Exists(path)) | |
{ | |
Directory.CreateDirectory(path); | |
} | |
Console.WriteLine($" Disassemble {Filepath} /out:{TempFullPath}"); | |
ProcessUtil.Execute(ILDasmPath, $"{Filepath} /out:{TempFullPath}"); | |
} | |
private void Assemble() | |
{ | |
var path = Directory.GetParent(OutFullPath).FullName; | |
if (!Directory.Exists(path)) | |
{ | |
Directory.CreateDirectory(path); | |
} | |
var parameters = $"{TempFullPath} /dll /key={_keyPath} /output={OutFullPath}"; | |
var resourcesPath = Path.ChangeExtension(TempFullPath, ".res"); | |
if (File.Exists(resourcesPath)) | |
{ | |
parameters += $" /res={resourcesPath}"; | |
} | |
Console.WriteLine($" Reassemble {parameters}"); | |
ProcessUtil.Execute(ILAsmPath, parameters); | |
} | |
public void Transform() | |
{ | |
if (Extension == ".dll") | |
{ | |
Disassemble(); | |
Assemble(); | |
} | |
else | |
{ | |
Console.WriteLine($" Copy {Filepath} to {OutFullPath}"); | |
var path = Directory.GetParent(OutFullPath).FullName; | |
if (!Directory.Exists(path)) | |
{ | |
Directory.CreateDirectory(path); | |
} | |
// just copy the file | |
File.Copy(Filepath, OutFullPath); | |
} | |
} | |
} | |
class Program | |
{ | |
public string KeyPath = ""; | |
static void Main(string[] args) | |
{ | |
var keyPath = args[0]; | |
var dllPath = args[1]; | |
dllPath = Path.GetFullPath(dllPath); | |
var tempPath = Path.Combine(dllPath, "temp"); | |
var outPath = Path.Combine(dllPath, "out"); | |
Console.WriteLine($"Signing all assemblies in {dllPath} with {keyPath} key. OutPath is {outPath}"); | |
var files = Directory.EnumerateFiles(dllPath, "*.*", SearchOption.AllDirectories).Select( | |
p => | |
{ | |
Console.WriteLine($"Processing {p} file dllPath: {dllPath} tempPath: {tempPath}, outPath: {outPath}"); | |
return new FileToSign(p, dllPath, tempPath, outPath, keyPath); | |
}); | |
foreach (var file in files) | |
{ | |
file.Transform(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#To be run from a folder containing the nuget package to be signed | |
#prerequisite: 7z.exe, 7z.dll, AutoSign.exe in Path | |
# ls *.nupkg | ForEach-Object { Rename-Item $_ "$_.zip" } | |
if (-Not (Test-Path "Unzipped")) { mkdir Unzipped } | |
# ls *.nupkg.zip | ForEach-Object { $unzippedFolder = $_.Name + '.unzipped'; Expand-Archive $_ ".\Unzipped\$unzippedFolder" } | |
ls *.nupkg | ForEach-Object { $unzippedFolder = $_.Name + '.unzipped'; 7z.exe x -tzip $_ -o".\Unzipped\$unzippedFolder" } | |
AutoSign.exe .\Key.snk .\Unzipped\ | |
ls Unzipped\out\*.unzipped | ForEach-Object { $zippedFolder = $_.FullName -replace '\.unzipped$',''; 7z.exe a -tzip "$zippedFolder" $_\* } | |
rm -Recurse Unzipped\out\*.unzipped | |
explorer Unzipped\out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment