Skip to content

Instantly share code, notes, and snippets.

View fabiolimace's full-sized avatar

Fabio Lima fabiolimace

View GitHub Profile
function join_numbers( i, n, x) {
while (match($0, /[ ][0-9,.()-]+[ ][0-9,.()-]+[ ]?/)) {
i = RSTART;
n = RLENGTH;
x = substr($0, i, n);
gsub(" ", "", x);
$0 = substr($0, 0, i-1) " " x " " substr($0, i+n)
}
}
@fabiolimace
fabiolimace / pure_pgsql_uuid7.sql
Created January 11, 2025 03:32
Pure SQL Function for Generating UUIDv7 on PostgreSQL
CREATE OR REPLACE FUNCTION uuid7_sql() RETURNS uuid AS $$
SELECT (FORMAT('%s-%s-%0s-%s-%s',
lpad(to_hex(trunc(EXTRACT(EPOCH FROM statement_timestamp()) * 1000)::bigint >> 16), 8, '0'),
lpad(to_hex(trunc(EXTRACT(EPOCH FROM statement_timestamp()) * 1000)::bigint & 65535), 4, '0'),
lpad(to_hex((trunc(random() * 2^12) + 28672)::bigint), 4, '0'), -- 28672 = 0x7000
lpad(to_hex((trunc(random() * 2^14) + 32768)::bigint), 4, '0'), -- 32768 = 0x8000
lpad(to_hex(trunc(random() * 2^48)::bigint), 12, '0')))::uuid;
$$ LANGUAGE SQL;
select uuid7_sql() -- 019450fe-7b0f-7ccc-8564-ad3ccc8234e0
@fabiolimace
fabiolimace / secure_random.sql
Last active January 11, 2025 14:57
Secure Random Functions for PostgreSQL using PGCRYPTO
-- install the extension
CREATE EXTENSION pgcrypto;
CREATE OR REPLACE FUNCTION secure_random_bigint() RETURNS bigint AS $$
DECLARE
v_bytes bytea;
v_value bigint := 0;
v_length integer := 8;
i integer := 0;
BEGIN
@fabiolimace
fabiolimace / Dockerfile
Last active April 28, 2025 22:39
Tinyproxy on Docker
FROM ubuntu:24.04
RUN apt update
RUN apt upgrade -y
RUN apt install tinyproxy -y
RUN mkdir --parents /home/tinyproxy
COPY template.conf /home/tinyproxy
COPY entrypoint.sh /home/tinyproxy
WORKDIR /home/tinyproxy
@fabiolimace
fabiolimace / validar-bissexto.awk
Last active January 1, 2025 23:15
Validação de datas com expressões regulares levando em consideração os anos bissextos
#!/usr/bin/mawk -f
/^((([0][48]|[2468][048]|[13579][26])00)|([0-9][0-9]([0][48]|[2468][048]|[13579][26])))$/ { print $0; next; }
{ print $0 " INVALIDO" }
@fabiolimace
fabiolimace / gnucash-caixa-conta-transformar-extrato-txt.awk
Last active May 26, 2025 01:32
Ferramentas para detectar entradas de fatura e expandir parcelas de cartão de crédito da CAIXA para importação no GnuCash
#!/usr/bin/mawk -f
#
# Transforma um extrato exportado da Caixa no formato TXT para uma arquivo mais simples.
#
# O arquivo exportado eh na verdade um CSV com um ponto e vírgula separando colunas.
#
# O arquivo de saida gerado por este script eh um TSV com data e valor reformatados.
#
# Aceita entradas de extrato neste formato (4 colunas):
@fabiolimace
fabiolimace / hash-uniqueness.sh
Last active October 27, 2025 19:48
Hash Function Uniqueness and Collision
#!/bin/bash
# Calculates hash uniqueness.
#
# Usage:
#
# hash-uniqueness.sh PROGRAM LENGTH WINDOW
#
# hash-uniqueness.sh md5sum
#
@fabiolimace
fabiolimace / NonCryptoHash.java
Last active December 21, 2024 06:39
Non-Cryptographic Hash Functions: CRC32, FNV-1A and MIX of both.
package com.example;
import java.nio.charset.StandardCharsets;
// https://www.youtube.com/watch?v=siV5pr44FAI
public final class NonCryptoHash {
// http://www.mrob.com/pub/comp/crc-all.html (crc32-adler)
private static final int[] crc32tab = { //
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, //
@fabiolimace
fabiolimace / SHA-256 UUIDv8 and Tag URI.md
Last active April 3, 2026 04:18
SHA-256 UUIDv8 and Tag URI

SHA-256 UUIDv8 and Tag URI

This document defines two unique identifiers:

  • a custom UUID based on RFC 9562;
  • a custom Tag URI based on RFC 4151.

SHA-256 UUIDv8

@fabiolimace
fabiolimace / TSID.diff
Created November 20, 2024 16:05
TSID.diff
diff --git a/src/main/java/io/hypersistence/tsid/TSID.java b/src/main/java/io/hypersistence/tsid/TSID.java
index 1ca20ca..665f081 100644
--- a/src/main/java/io/hypersistence/tsid/TSID.java
+++ b/src/main/java/io/hypersistence/tsid/TSID.java
@@ -36,6 +36,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntFunction;
import java.util.function.IntSupplier;
+import java.util.function.Supplier;