[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.pswp.cn/news/379070.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379070.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379070.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

最佳頁面置換算法

在一個請求分頁系統中,采用最佳頁面置換算法時,假如一個作業的頁面走向為4、3、2、1、4、3、5、4、3、2、1、5,當分配給該作業的物理塊數M分別為3和4時,試計算在訪問過程中所發生的缺頁次數和缺頁率。請給出分析過程。 解析&…

網絡名稱 轉換 網絡地址_網絡地址轉換| 計算機網絡

網絡名稱 轉換 網絡地址At the time of classful addressing, the number of household users and small businesses that want to use the Internet kept increasing. In the beginning, a user was connected to the Internet with a dial-up line, for a specific period of…

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…

最近最久未使用頁面置換算法

在一個請求分頁系統中,采用最近最久未使用頁面置換算法時,假如一個作業的頁面走向為4、3、2、1、4、3、5、4、3、2、1、5,當分配給該作業的物理塊數M分別為3和4時,試計算在訪問過程中所發生的缺頁次數和缺頁率。請給出分析過程。 …

ffplay的數據結構分析

《ffplay分析(從啟動到讀取線程的操作)》 《ffplay分析(視頻解碼線程的操作)》 《ffplay分析(音頻解碼線程的操作)》 《ffplay 分析(音頻從Frame(解碼后)隊列取數據到SDL輸出)》 《f…

tolowercase_Java String toLowerCase()方法與示例

tolowercase字符串toLowerCase()方法 (String toLowerCase() Method) toLowerCase() method is a String class method, it is used to convert given string into the lowercase. toLowerCase()方法是String類方法,用于將給定的字符串轉換為小寫。 Syntax: 句法&a…

python web 服務器實時監控 websocket_python websocket網頁實時顯示遠程服務器日志信息...

功能:用websocket技術,在運維工具的瀏覽器上實時顯示遠程服務器上的日志信息一般我們在運維工具部署環境的時候,需要實時展現部署過程中的信息,或者在瀏覽器中實時顯示程序日志給開發人員看。你還在用ajax每隔段時間去獲取服務器日…

磁盤調度算法

1,假設磁頭當前位于第105道,正在向磁道序號增加的方向移動,現有一個磁道訪問請求序列為:35,45,12,68,100,180,170,195,試用先來先服務…

C# Using用法三則

(1)引用命名空間 using作為引入命名空間指令的用法準則為: using Namespace; 在.NET程序中,最多見的代碼莫過于在程序文件的開頭引入System命名空間,其原由在于System命名空間中封裝了許多最基本最常用的操作&#xff…

iOS開發 工程

一直沒正兒八經的寫過技術文章。今日開個小窗,準備寫點東西。。。完了 1、傳統的MVC結構需要至少M、V、C三個模塊,在實際開發中往往需要添加額外的模塊,添加的模塊當然也大體上屬于這三個模塊之內。以下為較為常用的子模塊。 (1&a…

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…

一個使用numpy.ones()的矩陣| 使用Python的線性代數

Ones Matrix - When all the entries of a matrix are one, then it is called as ones matrix. It may be of any dimension (MxN). 一個矩陣 -當矩陣的所有條目均為1時,則稱為一個矩陣。 它可以是任何尺寸( MxN )。 Properties: 特性: The determina…

python去掉字符串最外側的引號_瘋狂Python講義第二章讀書筆記

本章講解變量和簡單類型2.1 從注釋講起單行注釋使用#,#后面的代碼被注釋掉不會運行,如:# print(123) 注釋掉后123不會輸出。多行注釋使用""" """,三個雙引號,雙引號中的內容注釋掉&…

Xpath[轉]

Xpath[轉] XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。 XPath 是 W3C XSLT 標準的主要元素,并且 XQuery 和 XPointer 同時被構建于 XPath 表達之上。 因此,對 XPath 的理解是很多高級 XML 應用的基礎。 …

【轉】深入分析 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& 就不可以修改…

JavaScript中的位置協議屬性

JavaScript | 位置協議屬性 (JavaScript | Location protocol property) A protocol by definition simply implies a set or working rules that must be adhered to. A network protocol thus defines rules for communication between network devices. You must be familia…