Skip to content

Instantly share code, notes, and snippets.

View kballenegger's full-sized avatar

Kenneth Ballenegger kballenegger

View GitHub Profile
@kballenegger
kballenegger / crypto_box_test.c
Created March 1, 2013 00:59
A working test of using NaCl's crypto_box.
#include <stdio.h>
#include <sodium/crypto_box.h>
#include <sodium/randombytes.h>
int main(int argc, char *argv[]) {
// payload
unsigned char payload[] = "Hello world, this is a test payload to test NaCl's cipher.";
unsigned char payload_padded[crypto_box_ZEROBYTES + sizeof(payload)] = {0};
for (int i = 0; i < sizeof(payload); i++)
@kballenegger
kballenegger / 1-base64.h
Created March 12, 2013 07:28
Base 64 encoding / decoding routines written in C.
// out must be cb_b642bytes_outsize(sizeof(in)-1) aka cb_b642bytes_outsize(strlen(in))
// returns length of out, (which *may* be zero-padded) if return val > cb_b642bytes_outsize
static inline int cb_b642bytes_outsize(int insize) { return ((insize-insize%4)+(insize%4==0?0:4))/4*3; }
int cb_b642bytes(unsigned char *out, const char *in);
// out must be cb_bytes2b64_outsize(sizeof(in))
// returns null-terminated string
static inline int cb_bytes2b64_outsize(int insize) { return ((insize-insize%3)+(insize%3==0?0:3))/3*4+1; }
void cb_bytes2b64(char *out, const unsigned char *in, int in_length);
@kballenegger
kballenegger / alexis.rb
Created March 14, 2013 02:50
Average of load averages...
#!/usr/bin/env ruby
require 'scout_api'
group_name = ARGV[0]
Scout::Account.new('...', '...', '...')
all_servers = Scout::Server.all(group_name: 'API')
@kballenegger
kballenegger / caching-lib-usage.mm
Last active December 15, 2015 21:09
A potential API for a library that abstracts and takes care of offline caching of assets.
typedef void(^CBOfflineCacheContentFetcher)(CBOfflineCacheFetchedContentCallback callback);
typedef void(^CBOfflineCacheFetchedContentCallback)(BOOL success, NSDictionary *content);
typedef void(^CBOfflineCacheCallback)(BOOL success, NSDictionary *content);
[CBOfflineCache fetchContent:@"store-view-boosts"
withFetcher:^(CBOfflineCacheFetchedContentCallback callback){
// this code does the network access
@kballenegger
kballenegger / validations.rb
Last active December 16, 2015 00:49
Validations library.
require 'validations/version'
module Validations
module ClassMethods
# Doubles as setter & getter for @validations
# This method is also the main interface to this lib.
#
def validations(&block)
@kballenegger
kballenegger / all.go
Last active December 16, 2015 11:39
Cryptographic exercise in Go (XOR, base64, hex, AES).
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
)
const (
@kballenegger
kballenegger / gist:5566093
Created May 13, 2013 04:05
Calculating fib(20) recursively is not very efficient...
Do it up to fib # 20...
About to generate # 20
About to generate # 19
About to generate # 18
About to generate # 17
About to generate # 16
About to generate # 15
About to generate # 14
About to generate # 13
About to generate # 12
@kballenegger
kballenegger / fib.c
Created May 13, 2013 04:59
Fibonacci generator in C with Y-Combinator powered memoization.
//
// Fibonacci generator in C with Y-Combinator powered memoization.
//
// Written by Kenneth Ballenegger in 2013
//
#include <stdlib.h>
#include <stdio.h>
#include <Block.h>
@kballenegger
kballenegger / lock.rb
Created June 3, 2013 16:18
Ensure script only runs once through a lock.
unless File.open('/tmp/transactions.lock', File::RDWR|File::CREAT, 0644).flock(File::LOCK_EX|File::LOCK_NB)
puts 'Cannot lock: Another instance of this script is probably running.'
exit
end
@kballenegger
kballenegger / dispatcher.rb
Created June 3, 2013 18:46
Problem with Ruby filesystem locks…
require 'open3'
command = 'ruby test2.rb'
(1..2).map do |n|
Thread.new do
puts "Executing scheduled command: #{command}"
output, status = Open3.capture2e(command)
unless status.success?