Skip to content

Instantly share code, notes, and snippets.

View kamranayub's full-sized avatar

Kamran Ayub kamranayub

View GitHub Profile
@kamranayub
kamranayub / SymmetricEncryption.cs
Created April 3, 2012 15:01
Validating key size for a SymmetricAlgorithm
// in a method
// alorithm = SymmetricAlgorithm
// keySize = int (in bits)
if (algorithm.ValidKeySize(keySize))
{
algorithm.KeySize = keySize;
}
else
{
@kamranayub
kamranayub / jquery.formatXml.js
Created April 9, 2012 19:36
jQuery formatXml plugin
// XML Formatter
// =============
// A small jQuery plugin that works in conjunction with [jquery.xml](https://github.com/kamranayub/jQuery-XML-Helper) to format
// an XML DOM structure.
//
//
// Usage:
// ------
//
// $(function () {
@kamranayub
kamranayub / ko-winjs.js
Created December 1, 2012 16:59
Knockout View Binding Support for WinJS
//
// Via Kamranicus: http://kamranicus.com/Blog/Posts/59/using-knockout-bindings-in-your-winjs-application
//
// Include this before the rest of your app code (before default.js)
//
// <!-- WinJS references -->
// ...
//
// <script src="/scripts/knockout-2.2.js">
// <script src="/scripts/ko-winjs.js">
@kamranayub
kamranayub / App.config
Created November 5, 2015 05:06
Example self-hosting a Web API endpoint in a Windows Service using TopShelf (https://topshelf.readthedocs.org) and TopShelf.Owin package.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Port" value="1337"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@kamranayub
kamranayub / knockout.extendedCheckedBinding.js
Last active December 10, 2015 10:08 — forked from anonymous/knockout.extendedCheckedBinding.js
A smarter `checked` and `value` binding that play nice together.
// Now you can bind your checkbox selection to an observable array of model
// properties, instead of having to bind it to a property on the item
// itself (e.g. `isSelected`).
// For more information, see:
// http://kamranicus.com/Blog/Posts/61/a-smarter-checked-binding-for-knockoutjs
// For a demo, see:
// http://jsfiddle.net/kamranayub/G8YZU
var oldValueBinding = ko.bindingHandlers['value'];
@kamranayub
kamranayub / RouteExtensions.cs
Last active December 10, 2015 13:08
RenderRoutes renders a JSON object that contains all your named routes, as long as you're using AttributeRouting
// RenderRoutes extension
//
// Use with AttributeRouting and JSON.NET to
// render out a JSON object that contains all your
// named routes for use in your client scripts.
public static class RouteExtensions {
/// <summary>
/// Renders a JSON object of routes
@kamranayub
kamranayub / CryptoExtensions.cs
Last active December 11, 2015 09:19
Two useful hashing extension methods (MD5 and SHA1).
using System.Security.Cryptography;
namespace Extensions {
public static class CryptoExtensions {
private static readonly SHA1 Sha1 = SHA1.Create();
private static readonly MD5 Md5 = MD5.Create();
/// <summary>
/// Performs an MD5 hash on the string and returns a 32 character hex format.
/// </summary>
@kamranayub
kamranayub / TaskExtensions.cs
Created January 27, 2013 16:51
A Promise/A-like implementation of the `Then()` function. Accepts optional error callback, otherwise errors bubble up to next chained method. See: http://blogs.msdn.com/b/pfxteam/archive/2010/11/21/10094564.aspx
public static class TaskExtensions {
/// <summary>
/// A CommonJS Promise-like method that will execute a success and error callback, or just bubble up errors if not handled
/// </summary>
/// <typeparam name="TFirst"></typeparam>
/// <typeparam name="TNext"></typeparam>
/// <param name="first"></param>
/// <param name="success"></param>
/// <param name="error"></param>
@kamranayub
kamranayub / SlideLeftRightTransitions.xaml
Created February 4, 2013 23:39
Slide Left / Right Transitions for `TransitioningContentControl`
<VisualState x:Name="NextTransition">
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="CurrentContentPresentationSite" Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1">
<EasingDoubleKeyFrame.EasingFunction>
<ExponentialEase EasingMode="EaseOut" Exponent="6"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
@kamranayub
kamranayub / CssClassBinding.js
Last active December 15, 2015 07:38
CSS Class binding for Knockout.js
//
// Observable CSS Class Binding
//
// See Fiddle for usage: http://jsfiddle.net/kamranayub/3ahUA/
//
ko.bindingHandlers['class'] = {
update: function (element, valueAccessor) {
var currentValue = ko.utils.unwrapObservable(valueAccessor()),
prevValue = element['__ko__previousClassValue__'],