Skip to content

Instantly share code, notes, and snippets.

@xfthhxk
xfthhxk / transducers.clj
Last active August 27, 2024 19:29
Clojure Transducer Notes
;; Transducer a portmanteau of transform and reducer?
(defn reducing-fn
([]
;; this 0 arity is called when no initial coll is provided to a transducer
;; This should return the starting state
(transient #{}))
([ans]
@xfthhxk
xfthhxk / transients.clj
Last active August 22, 2024 10:50
Clojure transients
;; get, get-in, keyword accessor, destructuring etc all work as expected
(get-in (transient {:a (transient {:b 2})}) [:a :b]) ;; => 2
(get-in (transient {:a {:b 2}}) [:a :b]) ;; => 2
(get-in (transient {:a (transient {:b (transient [10 11 12])})}) [:a :b 1]) ;; => 11
((transient {:a 1}) :a) ;; => 1
(:a (transient {:a 1})) ;; => 1
@xfthhxk
xfthhxk / project_setup.clj
Last active January 8, 2024 14:14
Project setup with babashka & spire
#!/usr/bin/env bb
;; NB. it can be helpful to do `sudo id` on the cmdline before running this script so that the privileged commands work
;; Spire doesn't yet have the ability to ask for a password when sudo is used
;;
;;
;; This script is an example of how to use babashka and spire to automate project setup for local development.
;; Sets up the local development environment so that necessary software is installed and https can be used.
;; You would tailor it to your needs.
@xfthhxk
xfthhxk / digital_signature.py
Last active October 2, 2023 20:52
Digital Signature in Python
#######################################################################
# 1. Generate public and private keys
# ```shell
# openssl genpkey -algorithm ED25519 -out ed25519.pem
# openssl pkey -in ed25519.pem -pubout > ed25519.pem.pub
# ```
# 2. Register the public key with the remote service
# 3. Ensure python requirements
# ```shell
# pip install requests cryptography
@xfthhxk
xfthhxk / digital-signature.js
Created October 2, 2023 19:27
Digital Signatures with Node
/*
# 1. Generate public and private keys
# ```shell
# openssl genpkey -algorithm ED25519 -out ed25519.pem
# openssl pkey -in ed25519.pem -pubout > ed25519.pem.pub
# ```
# 2. Register the public key with the remote service
# 3. Ensure node requirements
# ```shell
# npm install axios
@xfthhxk
xfthhxk / DigitalSignature.java
Last active October 2, 2023 21:02
Digital signatures with Java
/*
# 1. Generate public and private keys
# ```shell
# openssl genpkey -algorithm ED25519 -out ed25519.pem
# openssl pkey -in ed25519.pem -pubout > ed25519.pem.pub
# ```
# 2. Register the public key with the remote service
# 3. Compile
# ```shell
# javac DigitalSignature
@xfthhxk
xfthhxk / digital_signature.clj
Last active September 30, 2023 00:56
Digital Signatures using Clojure without dependencies
(ns digital-signature
(:require [clojure.string :as str])
(:import (java.security.spec PKCS8EncodedKeySpec X509EncodedKeySpec)
(java.security PublicKey PrivateKey Signature KeyFactory KeyPairGenerator)
(java.util Base64)))
(defn gen-key
[pem algo]
(let [priv-header "-----BEGIN PRIVATE KEY-----"
@xfthhxk
xfthhxk / roaring_bitmap.clj
Last active July 9, 2024 12:34
RoaringBitmap Clojure wrapper
(ns roaring-bitmap
(:refer-clojure :exclude [contains? empty? min max seq])
(:import (java.io
DataInput
DataOutput)
(java.nio
ByteBuffer)
(org.roaringbitmap
Container
ContainerPointer
@xfthhxk
xfthhxk / test-db.clj
Last active July 23, 2022 03:39
Clojure + Postgres Test Container + Flyway example
(ns test-db
"Example of using testcontainers and launching a postgres container with flyway migrations.
NB. databases are created but not destroyed which can be handy for debugging tests.
* Connecting to template1 from psql for example will cause create database to fail
* Based on ideas from https://github.com/opentable/otj-pg-embedded"
(:require [clj-test-containers.core :as tc]
[clojure.java.jdbc :as jdbc]
[clojure.string :as str])
(:import (org.flywaydb.core
Flyway)))
@xfthhxk
xfthhxk / aes256gcm.cljs
Created October 19, 2021 19:33
ClojureScript AES 256 GCM encrypt/decrypt compatible with the Buddy library
(ns aes256gcm
(:require ["crypto" :as crypto]
[clojure.string :as str]))
(defn url-safe-base64-encode
[s]
(-> s
(str/replace "+" "-")
(str/replace "/" "_")))