[ZT]Three ways to tell if a .NET Assembly is Strongly Named (or has Strong Name)

Here are several convenient ways to tell whether a .NET assembly is strongly named.  (English language note: I assume the form “strongly named” is preferred over “strong named” since that’s the form used in the output of the sn.exe tool shown immediately below.)

Towards the end, this post discusses use of Strong Names with Silverlight.

Then in the final section of this post the often confusing – though very important – differences between Strongly Named assemblies and Digitally Signed assemblies are clarified.

But first, here are three approaches for telling whether a .NET Assembly is Strongly Named...

Approach #1: Testing for Strong Name on Command Line or in a Script

You tell whether an Assembly/DLL has been successfully strong-named using the Strong Name Tool (sn.exe) (which can be found somewhere like here: C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\sn.exe) by running the following at the command line:

sn -vf System.Data.dll

Here are the results when running against a strongly named assembly, then one that is not strongly named.

C:\> sn -v C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll
Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30128.1
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembly 'C:\...\System.Data.dll' is valid
C:\> sn -v C:\WINDOWS\ismif32.dll
Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30128.1
Copyright (c) Microsoft Corporation.  All rights reserved.
C:\WINDOWS\ismif32.dll does not represent a strongly named assembly

Since the return value from sn.exe is 0 (zero) when the strong name is in place, and 1 (one) if not correctly strong named, you can test for this in a script by examining ERRORLEVEL, as in the following (put it into a text file called “sn-test.bat” for example and run as “sn-test foo.dll”):

@ echo off
if "%1"=="" goto END sn -q -vf %1 > NUL if ERRORLEVEL 1 goto NOT_STRONG
:STRONG
echo Has strong name: %1
goto END
:NOT_STRONG
echo Not strong named: %1
goto END
:END

Note that this will tell you whether it has SOME strong name, but does not tell you which one. So this technique is not appropriate for all uses, but might help in, say, an automated script that checks your about-to-be-released assemblies to make sure you remembered to add the strong names to them. (See note below – “Strong Names not for Security”.)

If you need finer-grain control and wish to write low-level code to ascertain the strong-naming status of an assembly, you can do that too.

Approach #2: Viewing Strong Name Details with IL DASM

Visual Studio ships with a handy utility – the Microsoft Intermediate Language Disassembler (ILDASM.EXE (tutorial)) – which can be used for disassembling .NET binaries to peruse the contents, perhaps for viewing the method signatures or viewing the .NET Assembly Manifest. It is helpful to load an assembly using IL DASM and examine the manifest to see whether there is a strong name key available. Your first step is to load the desired Assembly using the ildasm.exe utility. On my Windows 7 machine, IL DASM is found at

C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\ildasm.exe

and you can load up the System.Drawing.dll .NET Assembly as in the following example:

C:\> ildasm C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll

Once loaded, you will see a screen like the one below.

Note the MANIFEST section highlighted. Double-click on MANIFEST which load the following screen of manifest-specific data:

Find the section for the Assembly you’ve loaded – in this case, System.Drawing and following the section (which is marked with the “.assembly System.Drawing” directive highlighted above, and the content begins with the opening brace (“{“) shown above, and ends with its matching brace later in the manifest, and shown below.

The highlighted part of the manifest is the public key for this assembly. This public key can also be seen using the sn.exe tool, as follows:

C:\> sn -Tp C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll echo Not strong named: %1
Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.
Public key is 002400000480000094000000060200000024000052534131000400000100010007d1fa57c4aed9 f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79ad9d5dcc1dd9ad2361321 02900b723cf980957fc4e177108fc607774f29e8320e92ea05ece4e821c0a5efe8f1645c4c0c93 c1ab99285d622caa652c1dfad63d745d6f2de5f17e5eaf0fc4963d261c8a12436518206dc09334 4d5ad293
Public key token is b03f5f7f11d50a3a

Note that the Public key in the output from sn.exe matches the highlighted public key in the image immediately above it (of course you should ignore the spaces between pairs of digits in the screen shot).

If an assembly is not strongly named, the Public key will be missing from the manifest and will not be displayed by sn -Tp command.

Since IL DASM comes with both Visual Studio and with the .NET SDK, it is already on the desktop for most .NET Developers, and is therefore sometimes the handiest tool. The third option, .NET Reflector, is a third-party tool, though one adopted by many .NET Developers due to its awesomeness. Reflector conveniently shows more details about the strong name.

Approach #3: Viewing Strong Name Details with Reflector

You can load an assembly in the free version RedGate’s .NET Reflector and quickly see the strong name details – or lack thereof for non-strong named assemblies. In the image below, see at the bottom where the strong name string is highlighted. Note that the strong name has five parts (though the Culture is optional):

  1. Simple Name or Assembly name without the “.dll” extension (“System.Data” in case of assembly “System.Data.dll”)
  2. Assembly version (“2.0.0.0″ in case of “System.Data.dll”)
  3. Culture (“neutral” in case of “System.Data.dll”, but might be “en-us” for US English, or one of many others)
  4. Public Key or PublicKeyToken (public part of the cryptographic public/private key pair used to sign the assembly, “b77a5c561934e089″ in case of “System.Data.dll”)
  5. Processor Architecture – Defines the assembly’s format, such as MSIL (intermediate language) or x86 (binary for Intel x86 processors)

Using Reflector to show strong name

In the next image, see at the bottom where the LACK OF complete name string is highlighted; this assembly does not have a strong name to display, so “Name” field includes a null value for PublicKeyToken. (Note that in the real world, Spring.Core.dll is in fact released as strongly named by the good folks on the Spring.NET project; the screen shot below was done on a non-production version of that DLL.)

Reflector shows missing strong name

While you are at it… make Reflector the default program for “launching” assemblies (actually would need to be for all files ending in the .DLL extension, but Reflector is smart enough to not choke on non-.NET assemblies).

Approach #4: (Bonus!) Viewing Strong Name with Windows Explorer

This post promised three ways to tell if a .NET Assembly has a strong name - but here is a bonus 4th way. Windows Explorer will not show you the strong name characteristics of an assembly, with one exception – for assemblies in the Global Assembly Cache (GAC), strong name data is included in the Properties dialog. If  you are examining the GAC, this can be handy.

Of course, if an assembly is in the GAC at all, it is strongly named by definition; assemblies are required by .NET to be strongly named to be allowed in the GAC.

Strong Naming for Silverlight

Silverlight also has support for strongly named assemblies, which is needed for the Cached Assembly Feature introduced in Silverlight 3.0.

(Silverlight 4 also introduces supports for digital signatures on XAP files, created by signtool.exe, which are validated by the Silverlight runtime for out-of-browser (OOB) applications running with elevated trust.)

Strongly Name Assembly != Digitally Signed Assembly

Strong Names and Digital Signatures are Orthogonal Concerns - Almost

Strongly Naming and Digitally Signing are largely orthogonal concerns. They have different purposes, different tools, and the digital certificates may come from different sources (for publicly distributed binaries, the certs for Digital Signing usually will come from a PKI source, though that is not essential for the Strong Naming certs).

The only dependency among them is that if the Assembly is to be Strongly Named, then the Strong Naming step has to happen before the Digital Signing step.3

How do I check whether an assembly is Digitally Signed? You can run the following command to determine whether assembly “foo.dll” is digitally signed:

signtool verify /pa foo.dll

If you want to see the hash – for example, to compare with another assembly’s hash – then you can view it using the following command sequence:

signtool verify /v /pa /ph foo.dll | find "Hash"

Of course, you can use sn.exe and signtool.exe together (one after another) to examine an assembly to ascertain both whether it is strongly named and whether it has been digitally signed.

Strong Names are NOT for Security!

Finally, a word of caution… Strong names are about versioning, not about security. Strong names are more about avoiding DLL Hell (which is largely an accidental concern) than about avoiding hackers (which is deliberate). While a strong name may help alert you to tampering, realize that strong names can be hacked, and Microsoft emphasizes that  strong-named assemblies do not give the same level of trust as digitally signing:

Strong names provide a strong integrity check. Passing the .NET Framework security checks guarantees that the contents of the assembly have not been changed since it was built. Note, however, that strong names in and of themselves do not imply a level of trust like that provided, for example, by a digital signature and supporting certificate.

Consider digitally signing your .NET assemblies if it is important to you or your customers that the origin of the assemblies be traceable and verifiable. One source of digital certificates that can be used for Digitally Signing assemblies is Verisign which has Authenticode Certificates.

转载于:https://www.cnblogs.com/samcn/archive/2010/08/11/1797284.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379070.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

rstudio 修改代码间距_第一章 R和RStudio

R与RStudioR是一种统计学编程语言,在科学计算领域非常流行。它是由Ross Ihaka和Robert Gentleman开发的,是 "S "编程语言的开源实现。R也是使用这种语言进行统计计算的软件的名字。它有一个庞大的在线支持社区和专门的软件包,可以为…

ubuntu下最稳定的QQ

一、安装好 Wine 1.2(1.2 版安装好就支持中文界面的了) 当然得有WINE 了 当然我的有 如果没有可以如下方法得到: 第一种方法:如果你已经安装过 Wine 的老版本,那么只要添加 Wine 1.2 的软件源,然后去新立得…

字体Times New Roman

Windows系统中的字体是Monotype公司为微软公司制作的Times New Roman PS(TrueType字体),视窗系统从3.1版本开始就一直附带这个字体。而在苹果电脑公司的麦金塔系统中使用的是Linotype公司的 Times Roman (在Macintosh系统中直接简称为‘Times…

磁盘调度算法

1,假设磁头当前位于第105道,正在向磁道序号增加的方向移动,现有一个磁道访问请求序列为:35,45,12,68,100,180,170,195,试用先来先服务…

C++11 std::shared_ptr的std::move()移动语义底层分析

std::shared_ptr的std::move()移动语义底层分析 执行std::move()之前: 执行std::move()之后: 结论:一个浅拷贝 sizeof(std::shared_ptr) 8字节 pss1 : 0x0028fea8 pss2 : 0x0028fea0 (栈是逆增长的) 观察执行std::m…

python去掉字符串最外侧的引号_疯狂Python讲义第二章读书笔记

本章讲解变量和简单类型2.1 从注释讲起单行注释使用#,#后面的代码被注释掉不会运行,如:# print(123) 注释掉后123不会输出。多行注释使用""" """,三个双引号,双引号中的内容注释掉&…

【转】深入分析 ASP.NET Mvc 1.0 – 1. 深入MvcHandler

MvcHandler是一个mvc程序真正开始的地方,因为你可以直接看到并调试它的源码。 MvcHandler的主要代码如下:protected internal virtual void ProcessRequest(HttpContextBase httpContext) {AddVersionHeader(httpContext);// Get the controller typestring control…

C++11 右值引用与常量左值引用保存临时变量(函数返回值)的底层分析

右值引用保存临时变量(函数返回值)的问题 :临时变量是右值 1、普通变量接收函数返回值: 2、右值引用变量接收函数返回值: 3、用const int& 和右值引用是一样的效果,只是const int& 就不可以修改…

axure源文件_Axure教程:实现网易云音乐有声播放效果

为了方便讲解,我们首先在桌面新建一个文件夹,命名为音乐。1、将自己想要演示播放的MP3音乐文件放在这个文件夹里面。2、给播放页添加一个中继器,随便命名,我给它命名为【音乐地址链接器】,用来链接播放本地音乐文件。并…

ffplay分析(从启动到读取数据线程插入到字幕、音频、视频解码前的队列操作)

《ffplay的数据结构分析》 《ffplay分析(视频解码线程的操作)》 《ffplay分析(音频解码线程的操作)》 《ffplay 分析(音频从Frame(解码后)队列取数据到SDL输出)》 《ffplay分析 (视频从Frame(解…

并发进程同步

P是荷兰语Proberen(测试)的首字母。为阻塞原语,负责把当前进程由运行状态转换为阻塞状态,直到另外一个进程唤醒它。也就是不好的一方面。 V是荷兰语Verhogen(增加)的首字母。为唤醒原语,负责把一…

寄存器和pin_16x2 LCD的PIN图和寄存器

寄存器和pinIn these years the LCD is finding widespread use. It has replaced the LEDs or other multi-segment LEDs.This is due to the following reasons: 近年来, LCD正在广泛使用。 它已替换LED或其他多段LED,原因如下: The decli…

ffplay分析(视频解码线程的操作)

《ffplay的数据结构分析》 《ffplay分析(从启动到读取线程的操作)》 《ffplay分析(音频解码线程的操作)》 《ffplay 分析(音频从Frame(解码后)队列取数据到SDL输出)》 《ffplay分析 (视频从Fram…

【转】.NET深入学习笔记(4):深拷贝与浅拷贝(Deep Copy and Shallow Copy)

今天继续利用准备WSE安全开发文章的空闲时间,完善《.NET深入学习笔记》系列(基本都是.Net重要的知识点,我都做了详细的总结,是什么、为什么、和怎么实现)。想必很多人也接触过这两个概念。做过C的人对深浅拷贝的概念一…

abap 添加alv上的工具栏的按钮_Excel里的置顶功能——快速访问工具栏

100万职场人都在看后台回复礼包领199元职场干货大家好,我是小可~今天跟大家分享一个提高Excel操作效率的小技巧自定义你的快速访问工具栏设置后的效果▼▼▼也就是把你最经常用到的两三个功能放到快速访问工具栏可以一眼就找到这些功能不需要靠快捷键或者功能选项卡…

用递归法求1²+2²+...+n²的值

思路分析: 谈到递归,我个人会联想到数学里面的通式。因为递归调用的函数的对应法则是相同的。例如这道题:f(x)=x。这个就是函数通式,只不过把每个求得的结果进行累加求和即可。用户输入5的时候,会出现f(5)=5,之后再进行x减一操作,执行f(4)=4,最后将每个进行累加即可。…

编写一个函数,计算下式当n=10和n=100的值。

思路分析: 首先,我个人看法:当我拿到这道题的时候,我会把它当成一道数学题对待。分子是动的,恒为一,分母是进行依次增加的。且奇数项为正,偶数项为负。因为设计运算出现的是分数,故,设计选取存储类型为double。 找出问题: ①正负号问题、②分母问题、③累计求和问题…

POJ 1001 大数的乘法

对这道题的理解 大数的乘法 关键是 实型的 那么首先就是数出来小数点有几位这个相信很简单 从后面往前数刚开始0 不算接着就是遇到小数点结束如果没有小数点 那么置为0 接着就是输出地时候首先算出小数点的位置然后输出 你想怎么样都行 从后往前数这个时候输出 那么就是你也…

铃木uy125摩托车机油_济南铃木安徽发布国四新车—6480元瑞梦125、9380元UY125

安徽合肥,这个具有两千多年历史的古城,以“三国故地、包拯家乡”而闻名海内外,2019年4月22日济南铃木为这座城市带来一份惊喜,今年正值国四执行,济南铃木旗下两款国四新车瑞梦125与UY125正式在合肥与大家相见。济南铃木…