Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created April 25, 2010 17:59
Show Gist options
  • Save apeiros/378578 to your computer and use it in GitHub Desktop.
Save apeiros/378578 to your computer and use it in GitHub Desktop.
class String
Base32Alphabet ||= ('A'..'Z').to_a+%w[2 3 4 5 6 7]
def base32
binary = unpack("B*").first
binary << "0"*(5-(binary.length % 5))
decoded = binary.scan(/.{1,5}/).map { |r| Base32Alphabet[r.to_i(2)] }.join('')
decoded << "="*(8-(binary.length.div(5) % 8))
decoded
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment