Skip to content

Instantly share code, notes, and snippets.

View patrickfav's full-sized avatar
🐿️
Nam nam nam

Patrick Favre patrickfav

🐿️
Nam nam nam
View GitHub Profile
@patrickfav
patrickfav / example_jekyll.md
Last active February 24, 2025 01:06
A Liquid Filter for obfuscating an Email Address (can be used with Jekyll aswell).

In Jekyll set a variable for the mail, e.g. in the _config.yml

email: name@mail.com

then use it in your page

Reach me under:	{{ site.email | mailObfuscate }}

which will generate the following HTML

@patrickfav
patrickfav / convert_png.ps1
Created September 21, 2024 22:24
A Powershell Script that converts all png files recusivly in given folder (and subfolders) when the resulting jpeg is smaller than the png and the png does not contain transparency. Requires Image Magick to be available in PATH.
$sourceFolder = "C:\path\to\pngs"
$sumSavedKb = 0
# Get all PNG files recursively
Get-ChildItem -Path $sourceFolder -Recurse -Filter *.png | ForEach-Object {
$file = $_.FullName
$jpgFile = [System.IO.Path]::ChangeExtension($file, "jpg")
# Get the old file size (in kB)
$oldfilesize = [Math]::Round((Get-Item -LiteralPath $file).Length / 1KB, 2)
@patrickfav
patrickfav / AesCbcExample.java
Last active May 14, 2024 05:25
Companion code to my article about AES+CBC with Encrypt-then-MAC.
package at.favre.lib.armadillo;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;
@patrickfav
patrickfav / AesGcmTest.java
Last active February 27, 2024 11:32
Java Authenticated Encryption with AES and GCM.
package at.favre.lib.bytes.otherPackage;
import org.junit.Test;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
@patrickfav
patrickfav / plugin-so-turndown.ts
Last active May 27, 2023 16:07
A Turndown Plugin parsing Stack Overflow HTML answers; converts it to GitHub-Flavored-Markdown (GFM) with correct language. Turndown is a JS Markdown Parser
import TurndownService from "turndown";
export const stackOverflowHighlightedCodeBlock = function (service: TurndownService): void {
const highlightRegExp = /lang-([a-z0-9]+)/
service.addRule('stackOverflowHighlightedCodeBlock', {
filter: function (node: HTMLElement, options: Options): boolean | null {
const firstChild = node.firstChild
return (
node.nodeName === 'PRE' &&
/*
* Copyright 2017 Patrick Favre-Bulle
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@patrickfav
patrickfav / encrypt_img_ecb.sh
Created November 17, 2018 14:24
Encrypt .ppm file with AES-ECB to show ECB will reveal patterns
#!/bin/sh
# This is part of my blog about AES: https://medium.com/p/7616beaaade9
# Inspired by https://blog.filippo.io/the-ecb-penguin/
# Convert your image to .ppm with Gimp or Photoshop
#
# Usage: ./ecb_img <image file as ppm> <password>
# extract header and body
@patrickfav
patrickfav / plugin-so-turndown.ts
Last active March 30, 2023 16:58
A Turndown Plugin parsing figures with captions; creates the format '![imgText](https://example.com/img.png "caption")' Turndown is a JS Markdown Parser see https://www.npmjs.com/package/turndown
export const figureCaption = function (service: TurndownService): void {
service.addRule('stackOverflowHighlightedCodeBlock', {
filter: function (node: HTMLElement, options: Options): boolean | null {
const firstChild = node.firstChild
const lastChild = node.lastChild
return (
node.nodeName === 'FIGURE' &&
firstChild && firstChild.nodeName === 'IMG' &&
lastChild && lastChild.nodeName === 'FIGCAPTION'
)
@patrickfav
patrickfav / head.html
Last active March 18, 2023 15:29
A hugo shortcode that obfuscates email addresses; makes it more difficult for bots to crawl it from your website. Add the html file to your "layouts/shortcodes/" folder then you can use it in your content.
{{/* If you are using a CSP header this is how you can whitelist the inline-script */}}
{{/* its not optimal since it only changes every day and only if you regenerate the site */}}
{{ $currentDayNonce := now | time.Format "2006-01-02" | md5 }}
<meta http-equiv="Content-Security-Policy"
content="default-src 'self';
script-src 'self' 'nonce-{{ $currentDayNonce }}';
img-src 'self';"
/>
@patrickfav
patrickfav / mac-rnd-sizes.md
Last active February 26, 2023 18:53
Research of ID-Mask SIV Engine MAC & Randomization Byte Lengths.

Proposed Lengths for ID Encryption Schema

32-bit Integer

Integer Value MAC Version Randomization Sum Base64/Base32
deterministic 4 byte 12 byte 1 byte - 17 byte (136 bit) 22/27
randomized 4 byte 12 byte 1 byte 10 byte 27 byte (216 bit) 36/43