Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / SymmetricExtensions
Created September 5, 2013 20:12
Symmetric Crypto
public static class SymmetricExtensions
{
private const ushort _iteration = 1000;
private const int _saltSize = 8;
public static byte[] Encrypt(this byte[] plainTextData, string password)
{
byte[] encryptedData = null;
using (var provider = new RijndaelManaged())
{
@feanz
feanz / Hash
Created September 10, 2013 22:13
Guid Hash
public static Guid Hash(params object[] args)
{
var composite = "";
foreach (var a in args)
{
composite += a.ToString();
}
Guid result;
using(var md5 = new MD5CryptoServiceProvider())
@feanz
feanz / ShuffleExtension.cs
Created September 15, 2013 14:12
Shuffle
public static void Shuffle<T>(this T[] array, Random random)
{
int n = array.Length;
while (n > 1)
{
int k = random.Next(n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
@feanz
feanz / Expire.cs
Created September 18, 2013 14:45
Expire Header WCF Service
public static class Expires
{
public static void Set(int seconds)
{
var response = WebOperationContext.Current.OutGoingResponse;
if(response != null)
{
var expires = DateTime.UtcNow.AddSeconds(seconds).ToEpireString();
@feanz
feanz / 0_MicrosoftTranslator.cs
Last active August 12, 2017 06:37
Microsoft Translator
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using Think.Formica.Extensions;
namespace Think.Formica.Translation
PRINT 'Checking the existence of the Version table.'
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Version]') AND type IN (N'U'))
BEGIN
PRINT 'The Version table does not exist.'
PRINT 'Creating the Version table...'
CREATE TABLE [dbo].[Version] ([Current] [nvarchar](100) NOT NULL)
END
PRINT 'Checking the existence of a row in the Version table.'
IF NOT EXISTS (SELECT 1 FROM [dbo].[Version])
@feanz
feanz / DeploymentController.cs
Created October 23, 2013 10:28
Sitecore deployment manageer
public partial class DeploymentController : Controller
{
private DeploymentManager _manager;
protected DeploymentManager Manager
{
get { return _manager ?? (_manager = new DeploymentManager()); }
}
public virtual ActionResult Delete()
@feanz
feanz / SerializationExtensions.cs
Created October 30, 2013 15:39
SerializationExtensions
public static class SerializationExtensions
{
public static string Serialize(this object obj)
{
using (var memoryStream = new MemoryStream())
using (var reader = new StreamReader(memoryStream))
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
@feanz
feanz / RemoveSpecialCharacters.cs
Created October 31, 2013 17:10
Remove Special Characters
protected static string RemoveAllSpecialCharacters(string input)
{
var r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
}
protected static string RemoveSelectSpecialCharacters(string input)
{
var r = new Regex("[~#%&*{}:<>?|\".]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
@feanz
feanz / BetterDefaultModelBinder.cs
Created November 8, 2013 09:33
BetterDefaultModelBinder means all collection properties are instantiated as empty collections not null.
public class BetterDefaultModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var model = base.CreateModel(controllerContext, bindingContext, modelType);
if (model == null || model is IEnumerable)
return model;
foreach (var property in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))