Click here to Skip to main content
15,886,798 members
Articles / Programming Languages / C#

Parsing strong name signatures generated with sn.exe

Rate me:
Please Sign up or sign in to vote.
4.58/5 (14 votes)
4 May 20042 min read 114.9K   1.6K   25  
This article shows how to parse Assembly "strong name keyfiles" generated with sn.exe
//------------------------------------------------------------------------
// (c) 2002-2004 by Per Anderson.  All rights reserved.
//------------------------------------------------------------------------

using System;
using System.IO;
using System.Text;
using System.Xml; 
using System.Reflection;
using System.Security.Cryptography; 
using System.Collections;



namespace Bullfrog.Compress.RSA
{
	public class Signer : Validator 
	{

		public Signer() : base()
		{
		}


		public Signer(string xmlkey) : base(xmlkey)
		{			
		}

		public Signer(byte[] snkbuf) : base(snkbuf)
		{
		}


		
		protected RSAPKCS1SignatureFormatter _fmt = null; 
		protected RSAPKCS1SignatureFormatter Fmt 
		{
			get
			{
				if (_fmt == null)
				{
					_fmt = new RSAPKCS1SignatureFormatter(RSA); 
					_fmt.SetHashAlgorithm("SHA1"); 
				}
				return _fmt; 
			}
		}


		public override void SetKey(AsymmetricAlgorithm key)
		{
			base.SetKey(key); 
			Fmt.SetKey(key); 
		}


		public byte[] SignToBytes(string str_orig)
		{
			byte[] orig = PlainTextToByte(str_orig); 			
			return SignToBytes(orig); 			
		}

		public byte[] SignToBytes(byte[] orig)
		{
			if (_rsa == null) return null; 							
			if ((orig == null) || (orig.Length < 1)) 
				return null;
			
			byte[] hash = SHA.ComputeHash(orig, 0, orig.Length); 			
			return Fmt.CreateSignature(hash); 
		}

		public string SignToString(string str_orig)
		{
			byte[] orig = PlainTextToByte(str_orig); 			
			return SignToString(orig); 
		}

		public string SignToString(byte[] orig)
		{
			byte[] signed = SignToBytes(orig); 
			string ret = ByteToBase64String(signed); 
			return ret; 
		}
		

	}


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Per Anderson is the founder of Sunfrog Technologies LLC, http://sunfrog-tech.com .

Comments and Discussions