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 / 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]

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 / 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'
@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 / 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 / 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 / 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 / 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 / parse_jks.py
Last active March 25, 2025 12:40
How to parse a JKS in Python
from pprint import pprint
import dataclasses
import struct
from datetime import datetime
import cryptography.x509
from cryptography.x509 import Certificate
def next_long(f):
return struct.unpack(">Q", f.read(8))[0]
@markscottwright
markscottwright / sample.py
Created July 5, 2023 11:46
My PyCharm console startup script
import sys
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])
from pprint import pprint as pp
try:
import dotenv
dotenv.load_dotenv()
except ModuleNotFoundError:
pass
print('Python %s on %s' % (sys.version, sys.platform))