Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
gigamonkey / oauth-notes.md
Created June 25, 2024 19:13
Trying to wrap my brain around OAuth (again)

Trying to understand OAuth

Here’s my basic understanding of how to do client-side OAuth, meaning we want JavaScript running in the browser to be able to authenticate to a service (e.g., GitHub) and then access that service directly from the browser. The key security requirement is that the client-side JavaScript can’t know the client secret because the whole point of secrets as that they be secret. Assume in what follows that our web server is running on example.com. Also note that all these requests should be over HTTPS.

@gigamonkey
gigamonkey / pugsql.js
Created June 18, 2024 21:31
PugSQL inspired JS library.
import Database from 'better-sqlite3';
import { argv } from 'process';
import fs from 'fs';
/*
* The kind of queries we support.
*/
const kinds = {
// Execute SQL, returns { changes, lastInsertRowId } object.
run: () => (stmt) => (...args) => stmt.run(...args),
@gigamonkey
gigamonkey / find-times.py
Last active June 9, 2024 18:48
Find multiple times to hold a meeting to maximize number of possible attendees.
#!/usr/bin/env python
"""
Find times to hold the CSA Zoom meeting based on when people said they could do it.
Given a number N, find the N times that maximize the number of people who can come.
"""
from datetime import datetime
from functools import reduce
import json
@gigamonkey
gigamonkey / bouncing.js
Last active March 19, 2024 23:28
Bouncing ball utility functions
// Some utility functions
const distance = (a, b) => Math.abs(a - b);
const distance2d = (p1, p2) => Math.hypot(p1.x - p2.x, p1.y - p2.y);
const clamp = (n, min, max) => (n < min ? min : n > max ? max : n);
const nextPos = (b) => ({ x: b.x + b.dx, y: b.y + b.dy });
/*
* A number is prime if it is not divisible by any number other than 1 and
* itself. 1 is not prime. Thus you can test whether a number greater than 1 is
* prime by checking whether it is divisible by any smaller number greater than
* one.
*/
public class Primes {
public boolean isPrime(int n) {
@gigamonkey
gigamonkey / TicTacToe.java
Last active November 2, 2023 21:19
Code golfed tic tac toe. Doesn't allow moving in occupied squares and detects wins and ties. Didn't exactly stick to what we've covered so for. Don't try this at home.
import java.util.Scanner;
public class TicTacToe {
public static void printBoard(int board) {
System.out.print("\033[2J\033[0;0H");
int pieces = board >> 4;
for (int i = 0; i < 9; i++) {
System.out.print(" " + (i + "XO").charAt((pieces >> i & 1 | pieces >> i + 8 & 2) & 3) + " ");
if (i % 3 < 2) {
@gigamonkey
gigamonkey / Chatbot.java
Last active October 19, 2023 17:27
Starter code for a chatbot.
import java.util.Scanner;
/**
* Make a chatbot. See [1] for some background on chatbots. But write your code
* here in a Codespace, not in Replit.
*
* [1] https://runestone.academy/ns/books/published/BHSawesome/Unit3-If-Statements/magpieindex.html
*/
public class Chatbot {
import java.util.Scanner;
public class Change0 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Item 1 price: ");
double price1 = input.nextDouble();
import java.nio.charset.StandardCharsets;
public class Xor {
// This is the cipher text, encoded as a hex string. If you translate every two
// characters of this string into a byte you will have an array of bytes which
// can be decoded by xor'ing the bytes with bits from the key, taking first the 8
// least significant bits of the key for the 0th byte, the the next 8, and so on,
// looping back around to the least significant bits every four bytes.
private static final String CIPHERTEXT =
@gigamonkey
gigamonkey / gpt-4.md
Created April 6, 2023 15:42
Translating CTE standards

CTE Standards, summarize by GPT-4

C1.0 Systems Development Process

  • C1.1 Life Cycle Phases

  • C1.2 Development Models

  • C1.3 Specifications, Requirements