Skip to content

Instantly share code, notes, and snippets.

View raypendergraph's full-sized avatar

Raymond Pendergraph raypendergraph

  • Chattanooga, TN
View GitHub Profile
@raypendergraph
raypendergraph / GenericCollectionFetchResultsController.swift
Last active May 25, 2017 14:14
A generic reusable NSFetchedResultsControllerDelegate.
import UIKit
import CoreData
class GenericCollectionFetchResultsController: NSObject {
fileprivate var blockOperation = BlockOperation()
//This flag is a workaround to a bug in iOS: http://openradar.appspot.com/12954582
fileprivate var shouldReloadCollectionView = false
fileprivate var collectionView: UICollectionView!
@raypendergraph
raypendergraph / CLLocationCoordinate2D+BoundingBox.swift
Last active November 15, 2019 05:47
A CLLocationCoordinate2D extension for calculating bounding boxes.
import CoreLocation
extension CLLocationCoordinate2D {
/**
Calculates a bbox around this CLLocationCoordinat2D by describing a distance that is roughly analagous to
the radius of a circle with a center at this coordinate and the radius is the distance and inscribes the circle
to the bbox created.
This tangential point method of calculating the box is described in Handbook of Mathematics By I.N. Bronshtein,
K.A. Semendyayev, Gerhard Musiol, Heiner Mühlig
@raypendergraph
raypendergraph / GenericTableFetchResultsControllerDelegate.swift
Last active May 25, 2017 14:15
A sort of generic FetchedResultsController delegate.
import UIKit
import CoreData
class GenericTableFetchResultsController: NSObject {
fileprivate var tableView: UITableView!
fileprivate var rowAnimation: UITableViewRowAnimation!
init(tableView: UITableView, rowAnimation: UITableViewRowAnimation) {
self.tableView = tableView
@raypendergraph
raypendergraph / TestCoreMotion.swift
Created September 27, 2017 18:13
Snippet to test motion activity data
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let startDate = df.date(from: "2017-09-27 14:50:19 +0000")!
let endDate = df.date(from: "2017-09-27 15:14:28 +0000")!
let cmm = CMMotionActivityManager()
let lowestConfidence = CMMotionActivityConfidence.low
cmm.queryActivityStarting(from: startDate, to: endDate, to: OperationQueue.main) {
(activities, error) in
guard let activities = activities else {
@raypendergraph
raypendergraph / SwiftAppReceiptVerification.swift
Created November 10, 2017 13:42
Swift print Apple app receipt verification
import UIKit
import CloudKit
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
func main() {
let filePath = Bundle.main.path(forResource: "sandboxReceipt", ofType: "")
// get the contentData
let base64 = FileManager.default.contents(atPath: filePath!)!.base64EncodedString()
@raypendergraph
raypendergraph / aho-corasick.py
Created November 29, 2017 23:21
Algorithm Snippets
#!/usr/bin/python
# -*- coding: ascii -*-
# A pythonic implementation of the Aho-Corasick string searching algorithm which (I hope
# you will find) needs no documentation if you are reading the white paper along-side.
# https://en.wikipedia.org/wiki/Aho?Corasick_algorithm
from collections import deque, namedtuple
from typing import Iterable, Dict, List, Callable, Optional, Set, Generator
FAILURE = ()
#!/usr/bin/env python
from __future__ import print_function
import json
import subprocess
import multiprocessing
import sys
import os
import signal
import threading
@raypendergraph
raypendergraph / toyRsa.go
Created November 2, 2021 14:27
Toy RSA implementation in Go
package main
import (
"fmt"
"math/big"
)
func encrypt(plainText string, e, n int) (cipherText []int) {
for _, p := range plainText {
//Exp sets c = p**e mod |n|
c := new(big.Int).
Exp(big.NewInt(int64(p)), big.NewInt(int64(e)), big.NewInt(int64(n))).
@raypendergraph
raypendergraph / jwt.go
Created November 2, 2021 14:32
Simple JWT demo in Go
package main
import (
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"math/big"
"math/rand"
"strings"