Skip to content

Instantly share code, notes, and snippets.

@thibautsacreste
Created June 7, 2012 13:25
Show Gist options
  • Save thibautsacreste/2888800 to your computer and use it in GitHub Desktop.
Save thibautsacreste/2888800 to your computer and use it in GitHub Desktop.
Ruby: encoding/decoding uids generated by the ngx_http_userid_module
require 'base64'
// Decoding
cookie_value = 'CjpqE0+NhreIpEqgAyz3Ag=='
uid_hex_string = "uscc=" + Base64.decode64(cookie_value).unpack("VVVV").map{|x|x.to_s(16).rjust(8, '0')}.join.upcase
// Encoding with ruby > 1.9.2
uscc = "136A3A0AB7868D4FA04AA48802F72C03"
cookie_value = Base64.safe_encode64(uscc.scan(/.{8}/).map{|x| x.to_i(16)}.pack("VVVV"))
// Encoding with ruby 1.8 + Rails
cookie_value = Base64.encode64s(uscc.scan(/.{8}/).map{|x| x.to_i(16)}.pack("VVVV"))
@nimdraugsael
Copy link

Thx a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment