1#!/usr/bin/env perl
2#
3# Given a QIF file, randomize cookie headers
4#
5# Usage: randomize-cookies.pl input.qif > output.qif
6
7@chars = ('0' .. '9', 'a' .. 'z', 'A' .. 'Z', split '', '/+*');
8
9sub randomify {
10    $val = shift;
11
12    if (exists($cached_vals{$val})) {
13        return $cached_vals{$val};
14    }
15
16    @val = split '', $val;
17    $seen_eq = 0;
18    for ($i = 0; $i < @val; ++$i)
19    {
20        if ($val[$i] ne '=' or $seen_eq++)
21        {
22            $rand = $rand[$i] //= int(rand(@chars));
23            # Output is a function of both position and value:
24            $val[$i] = $chars[ ($rand + ord($val[$i])) % @chars ];
25        }
26    }
27
28    return $cached_vals{$val} = join '', @val;
29}
30
31while (<>) {
32    chomp;
33    if (m/^cookie\t(.*)/) {
34        print "cookie\t", randomify($1), "\n";
35    } elsif (m/^set-cookie\t(.*)/) {
36        print "set-cookie\t", join('; ', map randomify($_),
37                                                    split /;\s+/, $1), "\n";
38    } else {
39        print $_, "\n";
40    }
41}
42