Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 4630f8e5ffa4a36a0360b83e000daf146b9de051 @catwell committed Mar 22, 2011
Showing with 66 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 README.md
  3. +18 −0 Rakefile
  4. +1 −0 VERSION
  5. +39 −0 lib/b64url.rb
@@ -0,0 +1 @@
+pkg/
@@ -0,0 +1,7 @@
+# b46url
+
+## Usage
+
+ require "b64url"
+ 22507476152874102712.to_b64url # => "Thank-you-4"
+ "all_the_Fish".from_b64url # => 1961803945217736715041
@@ -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
@@ -0,0 +1 @@
+1.0.0
@@ -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.