News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
Signing a Hash  
by Jim Hunt [jimh at netwasp dot com]
posted on 2004/06/12

I want to get my certificate from the SPC system store, Hash a data string and then create a signature of that Hashed string using MD5 as the hashing algorithm. In C++ I doing the following:

CertOpenStore(...)
CertFindCertificateInStore(...)
CryptAcquireCertificatePrivateKey(...)
CryptCreateHash(...)
CryptHashData(...)
CryptSignHash(...)

In the Mentais library I can do the first bit fine:
cs = new CertificateStore("SPC");
cert = cs.FindCertificateBySubjectString("PhoneThemes");

How do I do the next bit? There seems to be a really good function called "CreateSignature" but this is in an internal class ("MD5SHA1CryptoServiceProvider") and doesn't just do MD5 hashing. What's the best way of going about this?

(Great resource BTW)

Thanks

Jim

by Pieter Philippaerts [Pieter at mentalis dot org]
posted on 2004/06/14

The hashing- and signature classes are part of the normal .NET framework. Typically you'd use the MD5CryptoServiceProvider or SHA1CryptoServiceProvider class [from the System.Security.Cryptography namespace] to hash data and then one of the AsymmetricSignatureFormatters to sign it.

Here's some code that may help you on your way:

using System;
using System.Security.Cryptography;
using Org.Mentalis.Security.Certificates;

string text = ...;
byte[] text_bytes = Encoding.UTF8.GetBytes(text);
Certificate spc = ...;

MD5 md5 = MD5.Create();
md5.TransformFinalBlock(text_bytes, 0, text_bytes.Length);

AsymmetricSignatureFormatter asf = new RSAPKCS1SignatureFormatter(spc.PrivateKey);

byte[] signature = asf.CreateSignature(md5);

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.