Skip to content

Instantly share code, notes, and snippets.

@benders
benders / convert_base.rb
Created May 13, 2009 18:19
Example methods for converting Integers to and from bases over 36
#!/usr/bin/env ruby -wKU
B64_ALPHABET = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a + ['+', '/']
class Integer
def to_s64( base = 64 )
input = self
out = ""
while input > 0
digit = input % base
@benders
benders / rsa-test.rb
Created May 13, 2009 21:44
Ruby RSA using OpenSSL example
require 'openssl'
require 'base64'
require 'stringio'
plaintext = StringIO.new <<-EOF
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Also, because I said so.
EOF
target_key = OpenSSL::PKey::RSA.new(640)
@benders
benders / prh-ssl.rb
Created May 15, 2009 18:59
Example symmetric encryption (Snipped from phedders / prh-rlib)
require 'openssl'
def simple_decrypt(iv,crypted,password)
crypt = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
crypt.decrypt
crypt.key = Digest::SHA256.digest(password)
crypt.iv = iv
data = crypt.update(crypted)
data << crypt.final rescue nil
end
@benders
benders / fix-post-content-type.conf
Created May 19, 2009 17:13
Apache config to fixup the Content-Type for POST requests
SetEnv !needs_ct
SetEnvIf Request_Method POST needs_ct
SetEnvIf Content-Type "\S+" !needs_ct
RequestHeader set Content-Type "application/x-www-form-urlencoded" env=needs_ct
@benders
benders / birthday.rb
Created May 19, 2009 17:25
Estimate the collision rate of k keys in space n
# Approximates the chance of at least one collision in a random
# distribution of k items across n possible IDs. Adapted from
# Karsten's approximate calculator at http://tinyurl.com/5zt6ol
def collision_probability( k, n )
1 - Math.exp((-k ** 2) / ( 2.0 * n).to_f)
end
# The odds of two people having the same birthday are slightly
# greater than 50% if you have 23 or more people.
puts collision_probability( 23, 365 ) # => 0.515509538061517
@benders
benders / index-usage.sql
Created May 19, 2009 18:31 — forked from gnarg/gist:114268
Finds unused indexes in a mysql database
// Finds unused indexes in a mysql database
SELECT
t.TABLE_SCHEMA,
t.TABLE_NAME,
s.INDEX_NAME,
s.COLUMN_NAME,
s.SEQ_IN_INDEX,
( SELECT MAX(SEQ_IN_INDEX)
FROM INFORMATION_SCHEMA.STATISTICS s2
# HACK !!! This forces a post-hoc "preload"
Product.send(:preload_associations, p, 'product_type')
@benders
benders / archive-data
Created June 15, 2009 21:25
Example script for archiving old data
#!/usr/bin/env ruby
ENV["RAILS_ENV"] = (ENV['RAILS_ENV'] || "development").dup
require File.dirname(__FILE__) + '/../config/boot'
require RAILS_ROOT + '/config/environment'
MAX_AGE = 4.weeks
def archive(tablename, finder_sql)
raise ArgumentError if tablename.blank? or finder_sql.blank?
start_time = Time.now.to_f
#!/bin/sh
#
# Settings from Evan Weaver
# http://blog.evanweaver.com/articles/2009/04/09/ruby-gc-tuning/
#
RUBY_HEAP_MIN_SLOTS=500000
RUBY_HEAP_SLOTS_INCREMENT=250000
RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
RUBY_GC_MALLOC_LIMIT=50000000
@benders
benders / gist:134278
Created June 23, 2009 00:15
God setup for Apache httpd (Red Hat)
We couldn’t find that file to show.