Skip to content

Instantly share code, notes, and snippets.

@Hafthor
Hafthor / keybase.md
Created January 12, 2015 20:31
keybase.md

Keybase proof

I hereby claim:

  • I am hafthor on github.
  • I am hafthor (https://keybase.io/hafthor) on keybase.
  • I have a public key whose fingerprint is 02E0 BCF1 DB0D 866C 348D 9414 BA7B 4402 329E 8B25

To claim this, I am signing this object:

@Hafthor
Hafthor / luhnCheck
Last active August 29, 2015 14:15
calculates Luhn check digit or returns true/false based on if number passes Luhn check (for credit cards, IMEAs, etc)
var luhnMap = ['0246813579','0123456789'];
function luhnCheck(value) {
value = value.replace(/\D/g, '');
return luhnDigit(value.slice(0, -1)) === value.slice(-1);
}
function luhnDigit(value) {
return ('' + value.replace(/\D/g, '').split('').reverse().map(function(d, i) {
return -luhnMap[i % 2][+d];
@Hafthor
Hafthor / gist:11298ce2480baecf03ad
Created April 9, 2015 23:19
parse query string into handy map
function searchmap(separator) {
var map = {};
(location.search || '')
.split("&")
.forEach(function(x) {
var kv = x.split('='), k = kv[0], v = decodeURIComponent(x.substr(k + 1));
if (separator === undefined)
if (map[k])
map[k] = [v];
else
@Hafthor
Hafthor / gist:c6ed4223d6c1b515a999
Last active September 14, 2015 21:48
Java StringConcat vs StringBuilder
// StringConcat.java
public class StringConcat {
public static void main(String[] args) {
String abc = getA() + getB() + getC();
System.out.println(abc);
}
private static String getA() {
return "ABC";
}
@Hafthor
Hafthor / jqmchk.aspx
Created October 18, 2011 18:40
tapping actual checkbox on checkboxes with multiline labels on Android submits unchecked
<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head><title></title>
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="http://jquerymobile.com//test/css/themes/default/" />
<script src="http://jquerymobile.com//test/js/jquery.js"></script>
<script src="http://jquerymobile.com//test/js/"></script>
@Hafthor
Hafthor / gist:2670635
Created May 13, 2012 02:36
CalculatePasswordHash
Public Function CalculatePasswordHash(password As String, usernameOrId As String) As Byte()
Static saltBytes = New Byte() {&HDE, &HAD, &HC0, &HDE}
Static h As New SHA512CryptoServiceProvider
Dim o(0 To h.HashSize \ 8 - 1) As Byte
Dim usrBytes = UTF8Encoding.UTF8.GetBytes(usernameOrId)
Dim pwdBytes = UTF8Encoding.UTF8.GetBytes(password)
SyncLock h
h.Initialize()
h.TransformBlock(usrBytes, 0, usrBytes.Length, o, 0)
h.TransformBlock(saltBytes, 0, saltBytes.Length, o, 0)
@Hafthor
Hafthor / project.json
Created October 23, 2015 16:07
asp.net 5 project.json
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta8"
},
"commands": {
@Hafthor
Hafthor / GuardedCtors
Created January 13, 2013 02:11
Comparing guarded ctor for IDisposables
public RethrowingCatch() {
this.dependency = new Dependency();
try {
this.dependency.SomeProperty = "Blah";
} catch (Exception) {
dependency.Dispose();
throw;
}
}
@Hafthor
Hafthor / UIWebView cookies and useragent
Created October 19, 2013 00:23
Allow cookies and set user agent on UIWebViews
[NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
NSString *userAgent = [[[UIWebView alloc] initWithFrame:CGRectZero] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": [userAgent stringByAppendingString:@" Version/7.0 Safari/9537.53"]}];
console.log(expr('12+34*56'));
function expr(s) {
"use strict";
const ops = {
'+': function(a,b) { return a+b; },
'-': function(a,b) { return a-b; },
'*': function(a,b) { return a*b; },
'/': function(a,b) { return a/b; }