ma11hew28
ma11hew28

Reputation: 126397

What's the opposite of `unpack("m")`?

I'm trying to figure out how to encode a session cookie in a similar fashion to that of Facebook's signed_request.

The runwithfriends Facebook sample app implements base64_url_decode & base64_url_decode in Python.

I've found a simple implementation of base64_url_decode:

def base64_url_decode(string)
  "#{string}==".tr("-_", "+/").unpack("m")[0]
end

How do I implement base64_url_encode w/o require 'base64'. (I figure it's better to do it w/o require 'base64', right?)

Basically, what's the opposite of unpack("m")?

ruby-1.9.2-p0 > "aGVsbG8sIG1ycyB0ZWFs\012".unpack("m")
 => ["hello, mrs teal"] 

Upvotes: 0

Views: 1163

Answers (2)

zetetic
zetetic

Reputation: 47548

what's the opposite of unpack("m")?

Surely it's pack("m")? Or is this a trick question?

["hello, mrs teal"].pack("m") # => "aGVsbG8sIG1ycyB0ZWFs\n"

Upvotes: 1

Phrogz
Phrogz

Reputation: 303261

Viewing the source of Base64.encode64 from the documentation yields:

# File base64.rb, line 37
def encode64(bin)
  [bin].pack("m")
end

Yes, the opposite of unpack is pack.

Upvotes: 3

Related Questions