Skip to content

Instantly share code, notes, and snippets.

@tanglebones
tanglebones / HttpListenerExample.cs
Last active February 27, 2024 15:21
HttpListener Example
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Server
{
public static class Program
Setup:
1. Client provides server with Username and P = password
2. Server generates N = large Nonce (128bits+), R = 0
3. Server computes Q = H(H(P,N)) and stores (Username, N, Q, R)
4. Server send N to client
Login:
1. Client sends login request to server with Username
2. Server generates R = HMAC(large Nonce (128bits) + timestamp, ServerSecret)
3. Server sends R and N (looked up from Username) to the client
(function() {
// using `value =>` makes value the 'this' of the functions below
const MaybeMonad = value => ({
value,
flatMap(f) {
// The point of the maybe is to prevent problems.
// Having a "nothing value" indicates a problem, so we short circuit
// the return.
// This is like SQL NULL in that any operation on nothing
CREATE FUNCTION array_xirr(flows double precision[], force_period double precision) RETURNS double precision
LANGUAGE plpgsql
AS
$$
DECLARE
v FLOAT8;
rate FLOAT8;
firstday FLOAT8;
period FLOAT8;
exp FLOAT8;
@tanglebones
tanglebones / gist:2640039
Last active September 22, 2017 00:47
Type safe Cons Car Cdr in C# using only functions and allowing for different types in Cons
// cons :: a -> b -> (a -> b -> t) -> t
// cons a b = \x -> x a b
static Func<Func<TA, TB, dynamic>, dynamic> Cons<TA, TB>(TA a, TB b)
{
return f=>f(a,b);
}
// NOTE: you can not write:
// static Func<Func<TA, TB, TC>, TC> Cons<TA, TB>(TA a, TB b)
// {
var webHeaderCollectionType = typeof(WebHeaderCollection);
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(webHeaderCollectionType.TypeHandle);
try
{
var hInfoFieldInfo = webHeaderCollectionType.GetField("HInfo", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static);
var hInfo = hInfoFieldInfo.GetValue(null);
var hInfoType = hInfo.GetType().GetField("HeaderHashTable", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static);
var hashtableOfHeaderInfos = hInfoType.GetValue(hInfo) as Hashtable;
foreach (var e in hashtableOfHeaderInfos.Values)
{
@tanglebones
tanglebones / needless_if.cs
Created July 10, 2013 20:04
Bad Code: Needless if
// do not write:
if (!expr)
{
return false;
}
return true;
// instead of
// Do not do:
var l = new List<Action>{ f, g, h };
l.ForEach(x=>x());
// Instead of:
f();
g();
h();
@tanglebones
tanglebones / needless_switch.cs
Created July 10, 2013 20:08
Bad Code: obfuscation via switch
// Do not do:
enum What { A, B, C };
void F(string data, What what)
{
switch (what) {
case A: DoA(data); break;
case B: DoB(data); break;
case C: DoC(data); break;
}
@tanglebones
tanglebones / app-client-send-s3.cs
Last active December 17, 2015 04:19
Sending a file to S3
private static byte[] GetMultipartFormData(
IDictionary<string, string> parameters,
string caName,
byte[] fileContents,
string boundary)
{
var formDataStream = new MemoryStream();
byte[] formData;
using(formDataStream)