Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / VisaCheckoutDecryptor
Last active November 30, 2017 18:06
VisaCheckoutDecryptor
public class VisaCheckoutDecryptor
{
private const int HMAC_LENGTH = 32, IV_LENGTH = 16;
public static string DecryptPayload(string key, string dynamicKey, string payload)
{
var payloadData = Convert.FromBase64String(payload);
var dynamicKeyData = Convert.FromBase64String(dynamicKey);
var keyData = Encoding.UTF8.GetBytes(key);
@feanz
feanz / ExampleRepo.cs
Last active November 30, 2017 18:06
A wrapper around Riak time series client so you can work with documents and not lists of columns, rows and cells. The abstract repo just requires you to define the table name, columns and a map method.
public class TransactionRepository : RiakTSRepository<TestTransaction>
{
protected override string TableName => "Transaction";
protected override IEnumerable<Column> Columns
{
get
{
var columns = new[]
{
@feanz
feanz / Microsoft.Powershell_Profile.ps1
Last active July 13, 2023 12:50
Powershell Profile
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
if ($host.Name -eq 'ConsoleHost')
{
Import-Module PSReadLine
}
oh-my-posh --init --shell pwsh --config C:/Github/prompt/oh-my-posh.json | Invoke-Expression
@feanz
feanz / IEventPublisher.cs
Last active November 30, 2017 18:06
Example event model
public interface IEventPublisher
{
void Publish<T>(T item) where T : IEvent;
}
public interface IHandler<in T> where T : IEvent
{
void Handle(T item);
}
@feanz
feanz / program.cs
Last active November 30, 2017 18:00
Example issue with mongo client version 2 beta
class Program
{
static void Main(string[] args)
{
Run().Wait();
}
private static async Task Run()
{
var client = new MongoClient("mongodb://localhost:27016");
@feanz
feanz / program.cs
Created February 6, 2015 16:24
Example issue with mongo client version 1.
public class program
{
static void Main(string[] args)
{
var client = new MongoClient("mongodb://localhost:27016");
try
{
var server = client.GetServer();
var database = server.GetDatabase("testv1");
@feanz
feanz / MongoUnitOfWork.cs
Last active November 30, 2017 18:07
MongoUnitOfWork
public static class MongoUnitOfWork
{
public static async Task Transaction(this IMongoDatabase database, Func<Task> commands)
{
if (database == null) throw new ArgumentNullException("database");
var commited = false;
var beginTransaction = new BsonDocument("beginTransaction", "true");
await database.RunCommandAsync<BsonDocument>(beginTransaction);
@feanz
feanz / RouteTestingExtensions.cs
Created January 9, 2015 16:53
RouteTestingExtensions WebAPI Only
/// <summary>
/// Used to simplify testing routes and restful testing routes
/// <example>
/// This tests that incoming PUT on resource is handled by the update method of the Banner controller
/// "~/banner/1"
/// .WithMethod(HttpVerbs.Put)
/// .ShouldMapTo<BannerController>(action => action.Update(1));
/// </example>
/// </summary>
public static class RouteTestingExtensions
@feanz
feanz / StreamExtensions.cs
Created January 9, 2015 16:03
Stream extension methods
public static Stream ToStream(this string str)
{
byte[] byteArray = Encoding.UTF8.GetBytes(str);
//byte[] byteArray = Encoding.ASCII.GetBytes(str);
return new MemoryStream(byteArray);
}
public static string ToString(this Stream stream)
{
var reader = new StreamReader(stream);
return reader.ReadToEnd();
@feanz
feanz / DateTimeExtensions.cs
Created January 9, 2015 16:03
DateTime extension methods
public static bool Between(this DateTime dt, DateTime rangeBeg, DateTime rangeEnd)
{
return dt.Ticks >= rangeBeg.Ticks && dt.Ticks <= rangeEnd.Ticks;
}
public static int CalculateAge(this DateTime dateTime)
{
var age = DateTime.Now.Year - dateTime.Year;
if (DateTime.Now < dateTime.AddYears(age))
age--;