Skip to content

Instantly share code, notes, and snippets.

@nyg
nyg / AllJCAServices.java
Last active April 2, 2024 11:09
List all JCA security provider services and export them to a CSV file.
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.Provider;
import java.security.Security;
import java.util.Arrays;
import java.util.Collection;
@nyg
nyg / FreeSwap.java
Created January 20, 2024 10:22
Display available free Swap memory using JMX
package edu.self.nyg.example.jmx.app;
import java.lang.management.ManagementFactory;
import java.text.NumberFormat;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import lombok.extern.slf4j.Slf4j;
@nyg
nyg / use-local-storage.js
Created December 29, 2023 20:14
Custom useLocalStorage hook for Next.js
// Based on https://usehooks.com/useLocalStorage/, modified to be used with
// Next.js' server-side rendering components.
import { useState } from "react";
export default function useLocalStorage(key, initialValue) {
// There is no need to pass the inital value here as this will be executed on
// the server side, so window.localStorage is not available.
const [stateValue, setStateValue] = useState()
@nyg
nyg / request.mjs
Created December 29, 2023 19:42
HTTP request with Node.js
import http from 'http'
const options = {
hostname: 'perdu.com',
method: 'GET',
}
const req = http.request(options, res => {
console.log(`Status: ${res.statusCode}`)
@nyg
nyg / pdk.c
Created April 21, 2020 09:43
Create a password-derived key using libsodium.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
@nyg
nyg / auto_extract.sh
Created April 2, 2019 11:21
Script to recursively extract archives
#!/usr/bin/env bash
# Usage: /absolute/path/to/script.sh /absolute/path/to/folder/with/archive
for file in $1/* ; do
if [ -f "$file" ] ; then
echo Found file: $file
info=$(file $file | tr '[:upper:]' '[:lower:]')
@nyg
nyg / div_euc_hex.c
Last active March 2, 2024 21:28
Euclidean division in C.
#include <stdio.h>
int main(int argc, const char** argv) {
unsigned int a = 0xDEADBEEF;
unsigned int b = 0xBABE;
unsigned int q = a / b;
unsigned int r = a % b;
printf("%u = %u * %u + %u\n", a, q, b, r);
@nyg
nyg / EXIFUserComment.swift
Last active September 28, 2024 12:58
Get and set an EXIF UserComment to a JPEG image using the ImageIO framework.
// Note: to add a JPEG COM marker go here:
// https://gist.github.com/nyg/bdeae8190a41b4b56bde8e13dd471ecc
import Foundation
import ImageIO
#if os(iOS)
import MobileCoreServices
#endif
@nyg
nyg / AsynchronousServerSocketChannelTest.java
Created September 15, 2017 18:57
Basic code example for AsynchronousServerSocketChannel.
AsynchronousServerSocketChannel listener = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(ip, port));
while (true) {
Future<AsynchronousSocketChannel> future = listener.accept();
AsynchronousSocketChannel channel = future.get();
ByteBuffer buffer = ByteBuffer.allocate(5000);
Future<Integer> byteCount = channel.read(buffer);
System.out.println("Bytes read: " + byteCount.get());
@nyg
nyg / AddJPEGComment.swift
Last active February 21, 2021 05:17
Add a JPEG comment marker to file in pure Swift.
//
// Inspired by wrjpgcom of libjpeg
// https://github.com/libjpeg-turbo/libjpeg-turbo/blob/master/wrjpgcom.c
//
// https://stackoverflow.com/a/46045524/5536516
//
// Note: To add an EXIF UserComment go here:
// https://gist.github.com/nyg/c90f36abbd30f72c8b6681ef23db886b
import Foundation