// using the program:
// ptg <AssemblyPath>
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;
namespace PublicKeyTokenGetter
{
class Program
{
static void Main(string[] args)
{
string asmFilePath = args[0];
if (File.Exists(asmFilePath))
{
string tempFileName = Guid.NewGuid().ToString();
// start a sn.exe process to get the public key
Process sn = new Process();
sn.StartInfo.FileName = "sn.exe";
sn.StartInfo.Arguments = "-e " + asmFilePath + " " + tempFileName;
sn.StartInfo.RedirectStandardOutput = true;
sn.StartInfo.UseShellExecute = false;
sn.Start();
sn.WaitForExit();
using (FileStream publicKeyFile = File.Open(tempFileName, FileMode.Open))
{
using (BinaryReader reader = new BinaryReader(publicKeyFile))
{
// get the public key bytes
// and compute the 20 bytes long hash using the SHA1 algorithm
SHA1Managed sha = new SHA1Managed();
byte[] hash = sha.ComputeHash(reader.ReadBytes(160));
byte[] pkt = new byte[8];
// copy the last 8 bytes and reverse it
Array.Copy(hash, hash.Length - 8, pkt, 0, 8);
Array.Reverse(pkt);
// show the result in hex
foreach (byte b in pkt)
{
Console.Write(Convert.ToString(b, 16).PadLeft(2, '0'));
}
Console.WriteLine();
}
}
File.Delete(tempFileName);
}
}
}
}