Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a public key token and how is it calculated in assembly strong names?

What is a 'public key token' and how is it calculated in assembly strong names?

like image 664
Praveen Sharma Avatar asked Feb 21 '09 14:02

Praveen Sharma


People also ask

How do I get a public key for a strong-named assembly?

First, right click on the Assembly DLL -> Properties -> Details. Here you can find the name, version and Culture of your Assembly. It will give you the public key.

How public key cryptography is used for strong naming and signing the assembly?

The developer uses a pair of cryptographic keys: a public key, which everyone can see, and a private key, which the developer must keep secret. To create a strong-named assembly, the developer signs the assembly with his private key when building the assembly.

How do I find the public key of an assembly?

In Visual Studio, click External Tools on the Tools menu. In the External Tools dialog box, click Add and enter Get Assembly Public Key in the Title box. Fill the Command box by browsing to sn.exe. It is typically installed at the following location: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.

What is the role of a public key in a shared assembly?

Signing the assembly involves taking a hash of important parts of the assembly and then encrypting the hash with the private key. The signed hash is stored in the assembly along with the public key. The public key will decrypt the signed hash.


3 Answers

Regarding your question, "How is it calculated", it's an SHA1 hash.

From dot net blog:

Microsoft solves the "public key bloat" problem by using a hash of the strongly-named assembly's public key. These hashes are referred to as public key tokens, and are the low 8 bytes of the SHA1 hash of the strongly-named assembly's public key. SHA1 hashes are 160 bit (20 byte) hashes, and the top 12 bytes of the hash are simply discarded in this algorithm.

like image 86
Cerebrus Avatar answered Oct 06 '22 14:10

Cerebrus


You can get the PublicKeyToken from the VS Command Line by typing:

sn –T DLLName.dll
like image 32
Saurabh Kumar Avatar answered Oct 06 '22 14:10

Saurabh Kumar


If you need to generate a public key token based on a full public key, this little static method works:

   private static byte[] GetKeyTokenFromFullKey(byte[] fullKey)
    {
        SHA1CryptoServiceProvider csp = new SHA1CryptoServiceProvider();
        byte[] hash = csp.ComputeHash(fullKey);
        byte[] token = new byte[8];
        for (int i = 0; i < 8; i++ )
            token[i] = hash[hash.Length - (i+1)];

        return token;
    }
like image 7
Todd Kobus Avatar answered Oct 06 '22 12:10

Todd Kobus