Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / sha1_snippet.m
Created April 30, 2012 23:39
Generate SHA1 hash for a string
#import <CommonCrypto/CommonDigest.h>
// ...
const char *unlockCode = "99999999";
size_t unlockCodeSize = strlen(unlockCode);
unsigned char hashBytes[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(unlockCode, unlockCodeSize, hashBytes);
// ...
@kristopherjohnson
kristopherjohnson / NSString+KJSHA1.m
Created May 2, 2012 17:45
Generate SHA1 hash from UTF8 encoding of a string
- (NSData *)SHA1HashOfUTF8String {
const char *UTF8 = [self UTF8String];
size_t UTF8Length = strlen(UTF8);
unsigned char hashedUTF8[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(UTF8, UTF8Length, hashedUTF8);
return [NSData dataWithBytes:hashedUTF8 length:sizeof(hashedUTF8)];
}
@kristopherjohnson
kristopherjohnson / mycontent.html
Created June 1, 2012 13:48
Web page with dynamic loading and conversion of Markdown to HTML
<!DOCTYPE html>
<html>
<head>
<title>My Markdown Content</title>
<meta name="viewport" content="width=device-width" >
<style>
#status {
@kristopherjohnson
kristopherjohnson / xmlpp.py
Created June 5, 2012 15:20
Read XML file from stdin; write pretty-printed XML to stdout
#!/usr/bin/python
from lxml import etree
from sys import stdin
t = etree.parse(stdin)
print etree.tostring(t, pretty_print=True)
@kristopherjohnson
kristopherjohnson / datetimeexamples.cs
Created June 8, 2012 18:12
Format .NET DateTime in various formats
// 2008-09-22T13:57:31
DateTime.UtcNow.ToString("s");
// 2008-09-22T13:57:31.2311892-04:00
DateTime.UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");
//2008-09-22T14:01:54.9571247Z
DateTime.UtcNow.ToString("o");
@kristopherjohnson
kristopherjohnson / hman.sh
Last active May 13, 2022 23:46
Display man page as HTML
#!/bin/bash
# Displays man page as HTML page
#
# Requires the man2html utility, which can be installed on OS X with Homebrew (brew install man2html)
# See http://dcssrv1.oit.uci.edu/indiv/ehood/man2html.html for more info.
if [ -z "$1" ]; then
echo "usage: hman MANPAGE"
exit 1
#!/bin/bash
## Debugging: Show expanded commands before executing them
set -x
## Debugging: Exit with error on attempt to use unset variable
set -u
## Run commands in subshell
( /bin/foo; /bin/bar )
@kristopherjohnson
kristopherjohnson / SHA1Util.cs
Last active October 1, 2020 22:18
.NET/C# Generate SHA1 hex string for a string encoded as UTF8
using System.Security.Cryptography;
using System.Text;
namespace Snippets
{
public static class SHA1Util
{
/// <summary>
/// Compute hash for string encoded as UTF8
/// </summary>
@kristopherjohnson
kristopherjohnson / NSData+ZipFile.m
Created June 29, 2012 22:30
NSData category that determines whether data is ZIP format
@implementation NSData (ZipFile)
- (BOOL)isZipFormatData {
// Zip files start with the bytes { 50, 4b, 03, 04 }
if (self.length < 4)
return NO;
unsigned char signature[4];
[self getBytes:signature length:sizeof(signature)];
@kristopherjohnson
kristopherjohnson / IpcRemotingUtil.cs
Created July 4, 2012 14:10
Utility methods for using .NET IpcChannel
using System;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Serialization.Formatters;
namespace MyLibrary
{
/// <summary>
/// Utility methods to support .NET Remoting with IPC channels