Click here to Skip to main content
15,886,788 members
Articles / Web Development / HTML

Signum Framework Tutorials Part 2 – Southwind Logic

Rate me:
Please Sign up or sign in to vote.
4.45/5 (6 votes)
15 Nov 2012LGPL325 min read 31.3K   1K   22  
In this part, we will focus on writing business logic, LINQ queries and explain inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Signum.Services
{
    public static class Security
    {
        static MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();

        public static string EncodePassword(string originalPassword)
        {
            byte[] originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            byte[] encodedBytes = provider.ComputeHash(originalBytes);
            return BitConverter.ToString(encodedBytes);
        }
    }

    public class CryptorEngine
    {
        private string key;

        public CryptorEngine(string key)
        {
            this.key = key;
        }

        byte[] GetMD5KeyHash()
        {
            using (MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider())
            {
                return hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            }
        }

        TripleDESCryptoServiceProvider TripleDesCryptoService()
        {
            return new TripleDESCryptoServiceProvider()
            {
                Key = GetMD5KeyHash(),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
            };
        }

        public string Encrypt(string text)
        {
            using (TripleDESCryptoServiceProvider tdes = TripleDesCryptoService())
            {
                byte[] toEncrypt = UTF8Encoding.UTF8.GetBytes(text);

                byte[] result = tdes.CreateEncryptor().TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);

                return Convert.ToBase64String(result, 0, result.Length);
            }
        }

        public string Decrypt(string encrypted)
        {
            using (TripleDESCryptoServiceProvider tdes = TripleDesCryptoService())
            {
                byte[] toDecrypt = Convert.FromBase64String(encrypted);

                byte[] resultArray = tdes.CreateDecryptor().TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);

                return UTF8Encoding.UTF8.GetString(resultArray);
            }
        }


        public static string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input

            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            return CalculateMD5Hash(inputBytes);
        }

        public static string CalculateMD5Hash(byte[] inputBytes)
        {
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

    }
}

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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Signum Software
Spain Spain
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.

www.signumframework.com

I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...

Comments and Discussions