Skip to content

Instantly share code, notes, and snippets.

@jooeycheng
jooeycheng / ruby_generate_pubkey.rb
Last active May 5, 2023 01:03
Ruby OpenSSL::PKey::RSA generate RSA Public Key from Modulus and Exponent
# [A] OpenSSL::PKey::RSA has undocumented `e=' and `n=' methods
exponent = "10001"
modulus = "9201EBD5DC974FDE613A85AFF2728627FD2C227F18CF1C864FBBA3781908BB7BD72C818FC37D0B70EF8708705C623DF4A9427A051B3C8205631716AAAC3FCB76114D91036E0CAEFA454254D135A1A197C1706A55171D26A2CC3E9371B86A725458E82AB82C848AB03F4F0AF3127E7B2857C3B131D52B02F9A408F4635DA7121B5B4A53CEDE687D213F696D3116EB682A4CEFE6EDFC54D25B7C57D345F990BB5D8D0C92033639FAC27AD232D9D474896668572F494065BC7747FF4B809FE3084A5E947F72E59309EDEAA5F2D81027429BF4827FB62006F763AFB2153C4A959E579390679FFD7ADE1DFE627955628DC6F2669A321626D699A094FFF98243A7C105"
rsa = OpenSSL::PKey::RSA.new
e = exponent.to_i(16)
n = modulus.to_i(16)
rsa.e = OpenSSL::BN.new(e)
rsa.n = OpenSSL::BN.new(n)
@jooeycheng
jooeycheng / multiplexer.js
Last active January 18, 2017 03:03
ExpressJS Multiplexer
var express = require('express')
var app = express()
var request = require('request')
app.use(require('body-parser').json())
var endpoints = require('./endpoints.json')
var response = require('./response.json')
app.all("*", function(req, res) {
endpoints.forEach(function(ept) {
full_url = ept + req.url
@jooeycheng
jooeycheng / Open in iTerm
Created December 10, 2016 07:48
Open in iTerm AppleScript
-- Adapted from:
-- https://gist.github.com/eric-hu/5846890
--
-- Modified to work with files as well, cd-ing to their container folder
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
-- Treats OS X applications as files. To treat them as folders, integrate this SO answer:
-- http://stackoverflow.com/a/6881524/640517
@jooeycheng
jooeycheng / localtunnel.rb
Last active October 26, 2017 08:48
Ruby auto-restart script for localtunnel
# https://github.com/localtunnel/localtunnel/issues/81#issuecomment-218320442
# usage: ruby localtunnel.rb --port 3000 --subdomain yourdomainhere
require 'optparse'
options = { subdomain: 'defaultdomain', port: 3000 }
parser = OptionParser.new do |opts|
opts.banner = 'Usage: localtunnel.rb [options]'
opts.on('-s', '--subdomain subdomain', 'Subdomain') do |subdomain|
@jooeycheng
jooeycheng / ruby_dollar_signs.txt
Last active March 17, 2023 19:05 — forked from dvliman/gist:10402435
ruby dollar sign $ global variable
$: (Dollar Colon) is basically a shorthand version of $LOAD_PATH.
contains an array of paths that your script will search through when using require.
$0 (Dollar Zero) contains the name of the ruby program being run. This is typically the script name.
$* (Dollar Splat) is basically shorthand for ARGV. $* contains the command line arguments that were passed to the script.
$? (Dollar Question Mark) returns the exit status of the last child process to finish.
$$ (Dollar Dollar) returns the process number of the program currently being ran.
$~ (Dollar Tilde) contains the MatchData from the previous successful pattern match.
$1, $2, $3, $4 etc represent the content of the previous successful pattern match.
$& (Dollar Ampersand) contains the matched string from the previous successful pattern match.
$+ (Dollar Plus) contains the last match from the previous successful pattern match.
@jooeycheng
jooeycheng / git_notes.md
Last active September 1, 2016 10:59
jooeycheng git notes / reference / cheatsheet
git status
git diff                        // diff working_dir vs last_commit

git log
git log --graph                 // visualize graph topology in terminal
gitk                            // visualize graph topology in gitk (GUI)

git add .                       // stage new & modified files, without deleted
git add -u                      // stage modified & deleted files, without new
@jooeycheng
jooeycheng / .bashrc
Created August 26, 2016 04:45
custom ubuntu bash prompt
# custom bash, copied from Vagabond box
bind 'set completion-ignore-case on'
GIT_PS1_SHOWDIRTYSTATE=1 # displays * or +
GIT_PS1_SHOWSTASHSTATE=1 # displays $
GIT_PS1_SHOWUNTRACKEDFILES=1 # displays %
GIT_PS1_SHOWUPSTREAM='auto' # displays <, >, <> or =
GIT_PS1_SHOWCOLORHINTS=1
GIT_PS1_DESCRIBE_STYLE='contains'
RESET="\[\033[0m\]"
GRAY="\[\033[38;5;248m\]"
@jooeycheng
jooeycheng / codility_lessons.rb
Last active July 5, 2016 09:27
Solutions to some of Codility Lessons
# === [Lesson 2: Arrays] - CyclicRotation
# === Program v1 (recursive)
def solution(a, k)
return a if k == 0 || a.length == 0
a = a.unshift(a.delete_at(a.length - 1))
k -= 1
return solution(a, k)
end
@jooeycheng
jooeycheng / equilibrium_index.rb
Last active July 4, 2016 06:36
Function that finds the equilibrium index of an array
# === Program v1 [time complexity: O(n^2)]
class Main
def equilibrium_index(arr)
result = []
arr.each_with_index do |el, i|
j = 0
sum_prefix = 0
@jooeycheng
jooeycheng / special_sort.rb
Last active July 3, 2016 19:40
Function that rearranges an array of positive integers to produce largest possible integer when concatenated
# === Program
class Main
def compare(n1, n2)
(n1 + n2).to_i > (n2 + n1).to_i
end
def special_sort(arr)
result = []
temp = []