如何从snk文件中获取PublicKeyToken?使用命令行工具.我想过使用sn.exe但是找不到合适的参数.
xan*_*tos 24
如果要使用该sn.exe工具:
sn -p yourkey.snk publickey.snk
Run Code Online (Sandbox Code Playgroud)
现在只有公钥的publickey.snk
sn -tp publickey.snk
Run Code Online (Sandbox Code Playgroud)
现在你有了公钥和公钥令牌.
鉴于一个byte[]共同的snk文件,就像
byte[] snk = File.ReadAllBytes("YourSnkFile.snk");
Run Code Online (Sandbox Code Playgroud)
使用
byte[] publicKey = GetPublicKey(snk);
byte[] publicKeyToken = GetPublicKeyToken(publicKey);
Run Code Online (Sandbox Code Playgroud)
用这些实用方法
public static byte[] GetPublicKey(byte[] snk)
{
var snkp = new StrongNameKeyPair(snk);
byte[] publicKey = snkp.PublicKey;
return publicKey;
}
public static byte[] GetPublicKeyToken(byte[] publicKey)
{
using (var csp = new SHA1CryptoServiceProvider())
{
byte[] hash = csp.ComputeHash(publicKey);
byte[] token = new byte[8];
for (int i = 0; i < 8; i++)
{
token[i] = hash[hash.Length - i - 1];
}
return token;
}
}
Run Code Online (Sandbox Code Playgroud)
除了 @xanatos 答案之外,如果有人需要 PowerShell 版本:
function Get-SnkPublicKey([string]$FilePath)
{
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
$pair = New-Object System.Reflection.StrongNameKeyPair -ArgumentList @(,$bytes)
$publicKey = $pair.PublicKey
#[System.Convert]::ToBase64String($publicKey)
$hexes = [System.BitConverter]::ToString($publicKey);
$hexes.Replace("-", "")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8024 次 |
| 最近记录: |