2015年04月30日 23:31
# HTML
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="3qzVAnYwSX6VjH5NYY57cnokuwVBdfUmHo5zKIGYWvKSHOiJoqTNem3zjmK3+0idTczeYiVHyTJsg6sbXa1+DA==" />
<input type="hidden"
name="authenticity_token"
value="hWu4EQJayinqjvTqxRHVgRM5t6L7AiFDiC0X5rmU5RjJ24Wa1s5OLRLxBMUTZOZuJNHSxZ8wHVf6IM/VZaHB5g==" />
#!/usr/bin/ruby
require "base64"
token0="3qzVAnYwSX6VjH5NYY57cnokuwVBdfUmHo5zKIGYWvKSHOiJoqTNem3zjmK3+0idTczeYiVHyTJsg6sbXa1+DA=="
token1="hWu4EQJayinqjvTqxRHVgRM5t6L7AiFDiC0X5rmU5RjJ24Wa1s5OLRLxBMUTZOZuJNHSxZ8wHVf6IM/VZaHB5g=="
AUTHENTICITY_TOKEN_LENGTH=32
def xor_byte_strings(s1, s2)
s1.bytes.zip(s2.bytes).map { |(c1,c2)| c1 ^ c2 }.pack('c*')
end
def decode_mask(encoded_masked_token)
masked_token = Base64.strict_decode64(encoded_masked_token)
one_time_pad = masked_token[0...AUTHENTICITY_TOKEN_LENGTH]
encrypted_csrf_token = masked_token[AUTHENTICITY_TOKEN_LENGTH..-1]
csrf_token = xor_byte_strings(one_time_pad, encrypted_csrf_token)
Base64.strict_encode64(csrf_token)
end
p decode_mask(token0) # => "TLA9i9SUhAT4f/Av1nUz7zfoZWdkMjwUcg3YM9w1JP4="
p decode_mask(token1) #=> "TLA9i9SUhAT4f/Av1nUz7zfoZWdkMjwUcg3YM9w1JP4="