Skip to content

Instantly share code, notes, and snippets.

@knugie
knugie / pdf2jpg.sh
Created Apr 9, 2022
Split PDF into JPEGs
View pdf2jpg.sh
gs -dNOPAUSE -sDEVICE=jpeg -r300 -dJPEGQ=100 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sOutputFile="document-%02d.jpg" "document.pdf" -dBATCH
@knugie
knugie / decrypt.rb
Created Mar 26, 2022
WIP: Ruby AES Encryption using AES-256-GCM
View decrypt.rb
require 'openssl'
################
# Public Input #
################
hex_message = 'a9a08f273d9e96d567ccc3db8f6a6634c895973a260e2b7cb36c1dde457293102163900cc12ffe4ec51aa02db70a0979b510911fa99d50aeadd67f7ff0c37a8ab82e5e45cb4b7d713e8365b1f0e67e188e2807fa31f674f25318de6de122517a06cfa018e0edf308eeaa87530720ecdcac42'
auth_data = 'something'
################
# Secret Input #
@knugie
knugie / decrypt.rb
Last active Mar 26, 2022 — forked from equivalent/gist:b492f6779e99ee9defb2
WIP: Ruby AES Encryption using AES-256-CBC
View decrypt.rb
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 Mar 10, 2022 — forked from efi/keyboard_controller.rb
Send system keyboard events in JRuby via java.awt.Robot
View keyboard_controller.rb
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 / example_encrypt_decrypt.rb
Created May 28, 2020
Encrypt, Decrypt large files
View example_encrypt_decrypt.rb
# 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 Aug 11, 2019
ASCII art with figlet
View example.sh
# brew install figlet
# showfigfonts
# example:
figlet -D -n -f fender "The quick brown fox jumps over the lazy dog [ \ ] { | } ~"
# |''||''| '|| '||
# || || '' ||
# || ||''|, .|''|, .|''||` '|| ||` || .|'', || //`
# || || || ||..|| || || || || || || ||<<
# .||. .|| || `|... `|..|| `|..'|. .||. `|..' .|| \\.
@knugie
knugie / solution.rb
Created Sep 2, 2018
How many 3 digit numbers have exactly 3 factors?
View solution.rb
[*100..999].count{ |x| (1..x).count { |i| x % i == 0 } == 3 }
# => 7
@knugie
knugie / worker_pool.rb
Last active May 25, 2022
Worker pool in Ruby
View worker_pool.rb
# 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
############################################################################
View mount-tmp.sh
#!/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
}