Skip to content

Instantly share code, notes, and snippets.

@knugie
knugie / pdf2jpg.sh
Created April 9, 2022 05:49
Split PDF into JPEGs
gs -dNOPAUSE -sDEVICE=jpeg -r300 -dJPEGQ=100 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sOutputFile="document-%02d.jpg" "document.pdf" -dBATCH
@knugie
knugie / decrypt.rb
Created March 26, 2022 07:56
WIP: Ruby AES Encryption using AES-256-GCM
require 'openssl'
################
# Public Input #
################
hex_message = 'a9a08f273d9e96d567ccc3db8f6a6634c895973a260e2b7cb36c1dde457293102163900cc12ffe4ec51aa02db70a0979b510911fa99d50aeadd67f7ff0c37a8ab82e5e45cb4b7d713e8365b1f0e67e188e2807fa31f674f25318de6de122517a06cfa018e0edf308eeaa87530720ecdcac42'
auth_data = 'something'
################
# Secret Input #
@knugie
knugie / decrypt.rb
Last active March 26, 2022 07:56 — forked from equivalent/gist:b492f6779e99ee9defb2
WIP: Ruby AES Encryption using AES-256-CBC
require 'openssl'
# We use the AES 256 bit cipher-block chaining symetric encryption.
# AES 256 is virtually impenetrable using brute-force methods.
# However, CBC introduces a data integrity vulnerability (stream cipher attacks).
# We should use HMAC or GCM to mitigate the issue.
alg = 'aes-256-cbc'
cipher = OpenSSL::Cipher::Cipher.new(alg)
cipher.decrypt
@knugie
knugie / keyboard_controller.rb
Last active March 10, 2022 07:57 — forked from efi/keyboard_controller.rb
Send system keyboard events in JRuby via java.awt.Robot
class KeyboardController
def initialize
@robot = java.awt.Robot.new
end
def type *args
[args].flatten.map(&:to_s).map{|s|s.split(/\s+/)}.flatten.map(&:upcase).each do |n|
press, name = (n[0]=="-") ? [false,n[1..-1]] : [true,n]
press ? @robot.key_press(@code) : @robot.key_release(@code) if @code = java.awt.event.KeyEvent.const_get("VK_#{name}")
end
self
@knugie
knugie / macOS.md
Created September 30, 2021 07:43
Open ssh tunnel macOS

image

@knugie
knugie / example_encrypt_decrypt.rb
Created May 28, 2020 19:32
Encrypt, Decrypt large files
# From https://tjay.dev/howto-working-efficiently-with-large-files-in-ruby/
# Encrypt
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
file = nil
enc_file = nil
@knugie
knugie / example.sh
Last active August 11, 2019 19:18
ASCII art with figlet
# brew install figlet
# showfigfonts
# example:
figlet -D -n -f fender "The quick brown fox jumps over the lazy dog [ \ ] { | } ~"
# |''||''| '|| '||
# || || '' ||
# || ||''|, .|''|, .|''||` '|| ||` || .|'', || //`
# || || || ||..|| || || || || || || ||<<
# .||. .|| || `|... `|..|| `|..'|. .||. `|..' .|| \\.
@knugie
knugie / solution.rb
Created September 2, 2018 20:47
How many 3 digit numbers have exactly 3 factors?
[*100..999].count{ |x| (1..x).count { |i| x % i == 0 } == 3 }
# => 7
@knugie
knugie / worker_pool.rb
Last active May 25, 2022 19:34
Worker pool in Ruby
# from https://hspazio.github.io/2017/worker-pool/
SIZED_QUEUE_SIZE = 10
############################################################################
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
############################################################################
#!/usr/bin/env bash
size=1024 # MB
mount_point=$HOME/tmp
name=$(basename "$mount_point")
usage() {
echo "usage: $(basename "$0") [mount | umount | remount | check | orphan]" \
"(default: mount)" >&2
}