Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
catwell committed Mar 22, 2011
0 parents commit 4630f8e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
pkg/
7 changes: 7 additions & 0 deletions README.md
@@ -0,0 +1,7 @@
# b46url

## Usage

require "b64url"
22507476152874102712.to_b64url # => "Thank-you-4"
"all_the_Fish".from_b64url # => 1961803945217736715041
18 changes: 18 additions & 0 deletions Rakefile
@@ -0,0 +1,18 @@
begin; require 'psych'; rescue Object; nil end
require 'rubygems'
require './lib/b64url.rb'

begin
require 'jeweler'
Jeweler::Tasks.new do |gemspec|
gemspec.name = "b64url"
gemspec.summary = "Integer to/from base64url converter"
gemspec.email = "pierre@moodstocks.com"
gemspec.homepage = "http://moodstocks.com"
gemspec.description = "Encode and decode integers to/from base64url using the alphabet from RFC 4648"
gemspec.authors = ["Pierre Chapuis"]
gemspec.files = FileList['lib/**/*.rb']
end
rescue LoadError
puts "Jeweler is not available, please install it first."
end
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
1.0.0
39 changes: 39 additions & 0 deletions lib/b64url.rb
@@ -0,0 +1,39 @@
module B64URL

ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

class << self

def to_b64url(q)
s = ''
while q != 0
q,r = q.divmod(64)
s << ALPHA[r]
end
s.reverse!
end

def from_b64url(s)
t,m = s.chars.to_a.reverse!.map!{|x| ALPHA.index(x)},1
t.inject{|r,x| r+x*(m*=64)}
end

end

end

Integer.class_eval do

def to_b64url
B64URL.to_b64url(self)
end

end

String.class_eval do

def from_b64url
B64URL.from_b64url(self)
end

end

0 comments on commit 4630f8e

Please sign in to comment.