| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package SagePay::XORForm; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 66437 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 267 |  | 
| 4 | 1 |  |  | 1 |  | 25 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 98 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 7 | use Carp; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 137 |  | 
| 7 | 1 |  |  | 1 |  | 1187 | use MIME::Base64; | 
|  | 1 |  |  |  |  | 970 |  | 
|  | 1 |  |  |  |  | 76 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 6 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 461 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | ## * Constructor and methods.... | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub new { | 
| 17 | 0 |  |  | 0 | 1 |  | my $class 	= shift; | 
| 18 | 0 |  |  |  |  |  | my $self 	= {}; | 
| 19 | 0 |  |  |  |  |  | bless $self, $class; | 
| 20 | 0 |  |  |  |  |  | $self->_init(@_); | 
| 21 | 0 |  |  |  |  |  | return $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub _init { | 
| 25 | 0 |  |  | 0 |  |  | my $self   = shift; | 
| 26 | 0 |  |  |  |  |  | my (%part) = @_; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 | 0 |  |  |  |  | croak "Must have load the query string to encrypt" | 
| 29 |  |  |  |  |  |  | unless defined $part{'query_string'}; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 0 | 0 |  |  |  |  | croak "Must have load the key with which to encrypt message" | 
| 32 |  |  |  |  |  |  | unless defined $part{'key'}; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | $self->{'query_string'} = $part{'query_string'}; | 
| 35 | 0 |  |  |  |  |  | $self->{'key'} 			= $part{'key'}; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub sage_xor_string { | 
| 40 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 41 | 0 |  |  |  |  |  | my ($options) = @_; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  |  | my $ret = encode_base64($self->_simpleXor); | 
| 44 | 0 | 0 |  |  |  |  | $ret =~ s/(\r|\n)//g if $options->{'strip_newlines'}; | 
| 45 | 0 |  |  |  |  |  | return $ret; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | ## * ------------------------------------------------- | 
| 52 |  |  |  |  |  |  | ## * do the simple method to return an xor'd str | 
| 53 |  |  |  |  |  |  | ## | 
| 54 |  |  |  |  |  |  | sub _simpleXor { | 
| 55 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | my $InString = $self->{'query_string'}; | 
| 58 | 0 |  |  |  |  |  | my $Key		 = $self->{'key'}; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | my @KeyList = (); | 
| 61 |  |  |  |  |  |  | #Initialise out variable | 
| 62 | 0 |  |  |  |  |  | my $output = ""; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | #Convert $Key into array of ASCII values | 
| 65 | 0 |  |  |  |  |  | for(my $i = 0; $i < length($Key); $i++){ | 
| 66 | 0 |  |  |  |  |  | $KeyList[$i] = ord(substr($Key, $i, 1)); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | # Step through string a character at a time | 
| 70 | 0 |  |  |  |  |  | for(my $i = 0; $i < length($InString); $i++) { | 
| 71 |  |  |  |  |  |  | # Get ASCII code from string, get ASCII code from key (loop through with MOD), XOR the two, get the character from the result | 
| 72 |  |  |  |  |  |  | #% is MOD (modulus), ^ is XOR | 
| 73 | 0 |  |  |  |  |  | $output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % length($Key)])); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | return $output; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | 1; | 
| 82 |  |  |  |  |  |  | __END__ |