Skip to content

Instantly share code, notes, and snippets.

View Hafthor's full-sized avatar

Hafthor Stefansson Hafthor

View GitHub Profile
@Hafthor
Hafthor / httprelay.js
Created August 17, 2011 20:33
http relay using node.js
var http = require('http');
http.createServer(function (req, resp) {
var h = req.headers;
h.host = "stackoverflow.com";
var req2 = http.request({
host: h.host, port: 80, path: req.url, method: req.method, headers: h
}, function (resp2) {
resp.writeHead(resp2.statusCode, resp2.headers);
resp2.on('data', function (d) { resp.write(d); });
resp2.on('end', function () { resp.end(); });
@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 / 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 / UnsafeCompare.cs
Last active August 7, 2022 05:11
Fast compare for byte arrays in .NET - uses unsafe code to compare by casting as longs
// Copyright (c) 2008-2022 Hafthor Stefansson
// Distributed under the MIT/X11 software license
// Ref: http://www.opensource.org/licenses/mit-license.php.
static unsafe bool UnsafeCompare(byte[] a1, byte[] a2) {
unchecked {
if(a1==null || a2==null || a1.Length!=a2.Length)
return false;
fixed (byte* p1=a1, p2=a2) {
byte* x1=p1, x2=p2;
int l = a1.Length;
@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"]}];
@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";
}