Skip to content

Instantly share code, notes, and snippets.

View markscottwright's full-sized avatar

Mark Wright markscottwright

  • Washington, DC
View GitHub Profile
@markscottwright
markscottwright / pretty.java
Created December 8, 2022 23:45
How to pretty print json using java and Jackson
HashMap<String, Object> map = new ObjectMapper().readValue(jsonString, new TypeReference<HashMap<String, Object>>() {});
String prettyJson = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(map);
@markscottwright
markscottwright / template.sh
Created November 25, 2022 12:57
Best practices shell template
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
@markscottwright
markscottwright / Financial.java
Last active November 15, 2022 00:19
Financial functions, taken from Apache POI.
public class Financial {
/**
* Emulates Excel/Calc's PMT(interest_rate, number_payments, PV, FV, Type)
* function, which calculates the payments for a loan or the future value of an
* investment
*
* @param r
* - periodic interest rate represented as a decimal.
* @param nper
* - number of total payments / periods.
@markscottwright
markscottwright / SshKeyReader.java
Last active September 30, 2022 14:13
How to parse a ssh public key file. Only supports RSA keys right now.
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
@markscottwright
markscottwright / githubopen.vim
Created September 21, 2022 15:21
How to open the current file/line in github from Vim
fun! GithubOpen()
let olddir = getcwd()
call chdir(expand('%:p:h'))
silent! execute "!gh browse " . shellescape(expand('%')) . ":" . line('.')
redraw!
call chdir(olddir)
endfun
@markscottwright
markscottwright / certutil.py
Last active July 21, 2022 12:45
How to get all server certificates from a TLS site
import random
import socket
import struct
import time
from cryptography.x509 import load_der_x509_certificate
# from: http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml
TLS_NULL_WITH_NULL_NULL = b'\x00\x00'
TLS_RSA_WITH_NULL_MD5 = b'\x00\x01'

java.util.logging tips

The format can be set on the command line using -Djava.util.logging.SimpleFormatter.format="XXX". Or it can be set like this programmatically:

static
{
    System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT.%1$tL %4$s %2$s %5$s%6$s%n");
}
@markscottwright
markscottwright / get_value.py
Last active May 26, 2022 12:23
Python function to dig into a nested obect without errors
def get_value(o, *args, default=None):
for argIndex, arg in enumerate(args):
if isinstance(arg, int):
if isinstance(o, list) and 0 <= arg < len(o):
o = o[arg]
else:
return default
elif isinstance(arg, str):
if isinstance(o, dict) and arg in o:
o = o[arg]
@markscottwright
markscottwright / ImageOcr.java
Last active May 9, 2022 09:49
A single-file (although depending on Tesseract) tool for extracting text from images.
package scratch;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
@markscottwright
markscottwright / Main.java
Last active February 9, 2022 00:11
How to use an SSH2 key in Java.
import static org.bouncycastle.crypto.util.OpenSSHPrivateKeyUtil.parsePrivateKeyBlob;
import java.io.IOException;
import java.io.StringReader;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;