//-----------------------------------------------------------------------
// <copyright file="CryptoUtil.cs" company="Pyramid Consulting">
// Copyright (c) Pyramid Consulting. All rights reserved.
// khoa.tran - 15-Dec-2007
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace Bamboo.Core.Common.Crypto
{
public class CryptoUtil
{
public static string EncryptMD5(string value)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] byteValue = System.Text.Encoding.UTF8.GetBytes(value);
byte[] byteHash = md5.ComputeHash(byteValue);
return Convert.ToBase64String(byteHash);
}
}
}
Month: February 2011
Create ASCII MD5 Hash
///////////////////////////////////////////////////////////////////////////////////////////////
//
// This File is Part of the CallButler Open Source PBX (http://www.codeplex.com/callbutler
//
// Copyright (c) 2005-2008, Jim Heising
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// * Neither the name of Jim Heising nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Security.Cryptography;
using System.Text;
using System.Globalization;
using System.IO;
namespace WOSI.Utilities
{
///
///
public class CryptoUtils
{
public static string CreateASCIIMD5Hash(string stringToHash)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashValue = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(stringToHash));
StringBuilder hexString = new StringBuilder(32);
for (int index = 0; index < hashValue.Length; index++) { hexString.AppendFormat(CultureInfo.InvariantCulture, "{0:x2}", hashValue[index]); } return hexString.ToString(); } } } [/csharp]
Create MD5 Hash
///////////////////////////////////////////////////////////////////////////////////////////////
//
// This File is Part of the CallButler Open Source PBX (http://www.codeplex.com/callbutler
//
// Copyright (c) 2005-2008, Jim Heising
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// * Neither the name of Jim Heising nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Security.Cryptography;
using System.Text;
using System.Globalization;
using System.IO;
namespace WOSI.Utilities
{
///
///
public class CryptoUtils
{
public static string CreateMD5Hash(string stringToHash)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashValue = md5.ComputeHash(System.Text.Encoding.Unicode.GetBytes(stringToHash));
StringBuilder hexString = new StringBuilder(32);
for(int index = 0; index < hashValue.Length; index++) { hexString.AppendFormat(CultureInfo.InvariantCulture, "{0:x2}", hashValue[index]); } return hexString.ToString(); } } } [/csharp]
MD5 Encrypt
//http://activedeveloperdk.codeplex.com/
//The MIT License (MIT)
using System;
using System.Security.Cryptography;
using System.Security;
using System.IO;
using System.Text;
namespace ActiveDeveloper.Core.Utilities
{
public class Encryption
{
public static string MD5Encrypt ( string phrase )
{
UTF8Encoding encoder = new UTF8Encoding();
MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes = md5hasher.ComputeHash( encoder.GetBytes( phrase ) );
return byteArrayToString( hashedDataBytes );
}
private static string byteArrayToString ( byte[] inputArray )
{
StringBuilder output = new StringBuilder( “” );
for ( int i = 0; i < inputArray.Length; i++ ) { output.Append( inputArray[i].ToString( "X2" ) ); } return output.ToString(); } } } [/csharp]
MD5 encode
using System;
using System.Collections.Generic;
using System.Text;
namespace Utils
{
public class MD5Agent
{
public string Encode(string pass)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(pass);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs )
{
s.Append(b.ToString("x2").ToLower());
}
pass = s.ToString();
return pass ;
}
}
}
KeyedHashAlgorithm: Key
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
class MainClass {
public static void Main(string[] args) {
byte[] key = Encoding.Unicode.GetBytes(args[2]);
using (KeyedHashAlgorithm hashAlg = KeyedHashAlgorithm.Create(args[1])) {
hashAlg.Key = key;
using (Stream file = new FileStream(args[0], FileMode.Open, FileAccess.Read)) {
byte[] hash = hashAlg.ComputeHash(file);
Console.WriteLine(BitConverter.ToString(hash));
}
}
}
}
HashAlgorithm: Create()
using System;
using System.IO;
using System.Security.Cryptography;
class MainClass {
public static void Main(string[] args) {
using (HashAlgorithm hashAlg = HashAlgorithm.Create(args[0])) {
using (Stream file = new FileStream(args[1], FileMode.Open, FileAccess.Read)) {
byte[] hash = hashAlg.ComputeHash(file);
Console.WriteLine(BitConverter.ToString(hash));
}
}
}
}

