Created
June 7, 2012 13:25
-
-
Save thibautsacreste/2888800 to your computer and use it in GitHub Desktop.
Ruby: encoding/decoding uids generated by the ngx_http_userid_module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx a lot!