Skip to content

Instantly share code, notes, and snippets.

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;
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)
{
# this can be put in [repo]/.git/config for local settings
# or ~/.gitconfig for global settings
# create a difftool "nodiff" that just returns true
# this path is for Mac. On linux it's /bin/true i guess
[diff "nodiff"]
command = /usr/bin/true
# make git ignore white space differences, many different possibilites here
// 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 / needless_if.cs
Created July 10, 2013 20:04
Bad Code: Needless if
// do not write:
if (!expr)
{
return false;
}
return true;
// instead of
@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)
@tanglebones
tanglebones / app-backend-s3policy.cs
Last active December 17, 2015 04:19
Creating a signed S3 policy for a jpeg image.
var policy = new JObject();
policy["expiration"] = DateTime.UtcNow.AddMinutes(10).ToString("u").Replace(" ", "T");
var cond = new JArray();
policy["conditions"] = cond;
cond.Add(new JObject {{"bucket", Bucket}});
cond.Add(new JObject {{"key", filename}});
cond.Add(new JObject {{"acl", "public-read"}});
cond.Add(new JObject {{"Content-Type", "image/jpeg"}});
cond.Add(new JArray("content-length-range", 0, 1024*1024));