Skip to content

Instantly share code, notes, and snippets.

View James-E-A's full-sized avatar

James E. A. James-E-A

View GitHub Profile
@James-E-A
James-E-A / rsa.py
Created July 21, 2022 04:01
stdlib RSA walkthrough
### RSA textbook-ish walkthrough ###
# This is WORK IN PROGRESS.
# Intend to cover:
# -- "RSA uses numbers, not words?" (DONE)
# -- RSA ACTUAL MATH FORMULA (DONE)
# -- Hybrid encryption (TODO)
# -- Asymmetric padding (TODO)
# -- Forward secrecy, part 1 (TODO)
# -- Encapsulation protocols (TODO)
@James-E-A
James-E-A / qrnganu_uint8array.js
Last active December 7, 2022 22:25
fetch random uint8array
await (n =>
fetch('https://qrng.anu.edu.au/API/jsonI.php?' + new URLSearchParams({length: n, type: 'uint8'}))
.then(response => {if (!response.ok) {throw response}; return response.json();})
.then(result => new Uint8Array(result['data']))
)
(32);
// https://quantumnumbers.anu.edu.au/pricing
await (key =>
(n =>
@James-E-A
James-E-A / getElementByXPath.mjs
Last active June 26, 2025 19:56
Get element(s) by xpath
/**
* Get an element by XPath.
* Includes workaround for browser bug not allowing SVG elements to be selected; use the virtual "_" prefix to reference the context node's namespace.
* @param {string} expression - A string representing the XPath to be evaluated.
* @param {Node|Document} [contextNode] - The context node.
* @param {XPathEvaluator|Document} [document] - The document via which evaluate() will be called. Defaults to the context node's document, if specified; otherwise globalThis.document. Required if context node and globalThis.document are both absent.
* @param {boolean} [ordered=true] - Whether to return the "first" node selected by the expression. If false, allows the XPath engine to return an arbitrary node selected by the expression.
* @returns {?Node}
*/
export function getElementByXPath(expression, contextNode, document, ordered=true) {
@James-E-A
James-E-A / bn_js_maths.js
Last active December 5, 2022 19:11
javascript bignum modular exponentiation
function bn_powMod(a, e, m) {
// h/t https://umaranis.com/2018/07/12/calculate-modular-exponentiation-powermod-in-javascript-ap-n/
if (m === 1n)
return 0n;
if (e < 0n)
return bn_powMod(bn_modInv(a, m), -e, m);
for (var b = 1n; e; e >>= 1n) {
if (e % 2n === 1n)
b = (b * a) % m;
a = (a * a) % m;
@James-E-A
James-E-A / ed25519.py3
Last active October 19, 2022 14:33
X25519 demo implementation (Python 3 port)
#!/usr/bin/env python3
import hashlib
b = 256
q = 2**255 - 19
l = 2**252 + 27742317777372353535851937790883648493
def H(m):
return hashlib.sha512(m).digest()
@James-E-A
James-E-A / SSH to localhost.sh
Created October 24, 2022 20:13
Efficient version of ssh -Y for localhost
#!/bin/sh
otheruser="$(id -nu 1000)"
xhost "+SI:localuser:${otheruser}"
if pulseaudio --check; then
pax11publish -S "/run/user/$(id -u)/pulse/native" -e
setfacl -m "u:${otheruser}:x" "/run/user/$(id -u)" "/run/user/$(id -u)/pulse"
setfacl -m "u:${otheruser}:rw" "/run/user/$(id -u)/pulse/native"
setfacl -m "m::rwx" "/run/user/$(id -u)/pulse"
@James-E-A
James-E-A / parse-asound-cards.sh
Last active May 3, 2023 17:39
parse /proc/asound/cards in shell script
perl -0pe 's/^\s*(\S+)\s\[(.*?)\s*\]:\s+(.*)\n\s*(.*)/\1\t\2\t\3\t\4/gm' /proc/asound/cards # | cut -f"${fieldIndex-1}"
# fieldIndex: 1=ID; 2=Type; 3=Name; 4=Description
@James-E-A
James-E-A / clmul.mjs
Last active September 8, 2024 15:35
Carry-less product in Python
// James Edington <[email protected]>
// ht https://wunkolo.github.io/post/2020/05/pclmulqdq-tricks/
// h/t https://en.wikipedia.org/wiki/Carry-less_product
function clmul(a, b) {
const ONE = Object.getPrototypeOf(a).constructor(1)
const a_bit_length = a.toString(2).length;
let result = Object.getPrototypeOf(ONE).constructor(0);
for ( let i = 0 ; i < a_bit_length ; i++ ) {
result ^= b*(a & ONE<<i);
@James-E-A
James-E-A / istartswith.py
Last active January 13, 2023 21:47
Python: iterable startswith()
from operator import eq as _eq
from itertools import starmap as _starmap, chain as _chain, repeat as _repeat
def _istartswith(a, b):
"""
Returns True if every element of b is equal to the zip-corresponding element of a;
returns False otherwise, including in the event that a has fewer elements than b.
"""
return all(_starmap(_eq, zip(_chain(a, _starmap(object, _repeat(()))), b)))
@James-E-A
James-E-A / normalized_lru.py
Last active March 24, 2023 14:39
Normalize function inputs before LRU cache
import functools
from .. import NORMALIZE_FUNC, EXPENSIVE_API_CALL
@(lambda t: lambda f: functools.wraps(f)(lambda x: f(t(x))))(NORMALIZE_FUNC)
@functools.lru_cache
def f(x):
return EXPENSIVE_API_CALL(x)