Attached is a fully managed implementation of the skeleton classes and methods 
from openssl.so that I posted last week.

I'm currently working on implementing digest.so, which is very similar.

Cheers, Wayne.
using Ruby;
using Ruby.Runtime;
using Ruby.Builtins;
using System.Security.Cryptography;

namespace OpenSSL
{
    namespace Digest
    {
        [RubyClass("OpenSSL::Digest::Digest")]
        public class Digest
        {
            public System.Security.Cryptography.HMAC algorithm;

            [RubyMethod("initialize")]
            public static Digest initialize(Digest self, MutableString 
algorithm_name)
            {
                self.algorithm = 
System.Security.Cryptography.HMAC.Create("HMAC" + algorithm_name.ToString());
                if (self.algorithm == null)
                    throw new 
Ruby.Builtins.RuntimeError(string.Format("Unsupported digest algorithm ({0}).", 
algorithm_name));
                return self;
            }
        }
    }

    [RubyClass("OpenSSL::HMAC")]
    public class HMAC
    {
        [RubyMethod("hexdigest", RubyMethodAttributes.PublicSingleton)]
        public static MutableString hexdigest(RubyClass self, Digest.Digest 
digest, MutableString key, MutableString data)
        {
            digest.algorithm.Key = 
System.Text.Encoding.UTF8.GetBytes(key.ToString());
            return new 
MutableString(System.BitConverter.ToString(digest.algorithm.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data.ToString()))).Replace("-","").ToLower());
        }
    }
}

Attachment: OpenSSL_Test.rb
Description: OpenSSL_Test.rb

using System;
using System.Collections.Generic;
using System.Text;
using Ruby.Builtins;

namespace TestLibraries
{
    public class OpenSSL_Test
    {
        public static void Test()
        {
            MutableString expected_result = new 
MutableString(@"aeea11e714db6129aad0ea048b9251b301d86c50");
            MutableString key = new 
MutableString(@"5f8ac9bf2c112fdfe1da559d5492c80d589c25b0fed3b4c688d1fecef0ceb2c2f806ed70d93a14270c214a95a8aeaf0ca975e54ccc6f187ee0dfa7e7fd432e08");
            MutableString data = new 
MutableString(@"BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo\nSGFzaHsABjoKQHVzZWR7AA==");

            OpenSSL.Digest.Digest digest = new OpenSSL.Digest.Digest();
            OpenSSL.Digest.Digest.initialize(digest, new MutableString("SHA1"));

            MutableString r1 = OpenSSL.HMAC.hexdigest(null, digest, key, data);

            if (r1.Equals(expected_result))
                Console.WriteLine("OK");
            else
                Console.WriteLine("Bad!");

            MutableString r2 = OpenSSL.HMAC.hexdigest(null, digest, new 
MutableString("Wilma"), new MutableString("Fred Flintstone"));

            if (r2.Equals(new 
MutableString("9ab13cc2cee2d512661fb903e27ce7c82c24407a")))
                Console.WriteLine("OK");
            else
                Console.WriteLine("Bad!");
        }
    }
}
_______________________________________________
Ironruby-core mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to