-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.php
More file actions
147 lines (132 loc) · 3.97 KB
/
code.php
File metadata and controls
147 lines (132 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
const CHECKSUM_GENERATOR_ID = "234567";
const CHECKSUM_VALIDITY = 6;
function sumAlphanumeric($string)
{
$ascii = NULL;
$ascii = sha1($ascii);
for ($i = 0; $i < strlen($string); $i++)
{
$ascii += ord($string[$i]);
}
$ascii = "$ascii";
$ascii = mask($ascii);
return($ascii);
}
function mask($str)
{
if(strpos($str,'.') !== false)
{
$str = roundString($str);
}
$str = truncate($str);
$str = append($str);
return($str);
}
function append($str)
{
$str = "1".$str."1";
return $str;
}
function maskAmount($str)//mask the amount
{
if(strpos($str,'.') !== false)
{
$str = str_replace(".","1",$str);
}
return($str);
}
function roundString($str)//round around point
{
$index = strpos($str,".");
$str = substr($str,0,$index);
return($str);
}
function truncate($str)
{
if(strlen($str) > 6)
{
$str = substr($str,-6);
}
return($str);
}
function toInt($str)
{
$str = intval(str_replace(" ","",$str));
return($str);
}
function unmask($str)
{
$str = "$str";
return(substr($str,1,strlen($str)-2));
}
function decToHex($str)
{
$str = dechex(toInt($str));
$str = "$str";
return($str);
}
function hexToDec($str)
{
$str = hexdec($str);
return($str);
}
function getChecksum( $cardNumber , $amount , $invoiceNumber )
{
$amount = maskAmount($amount);
$hours = mask(date('H'));
$milliseconds = mask(microtime(true)*1000);
$checkSumGeneratorId = mask(CHECKSUM_GENERATOR_ID);
$clientServerConstant = toInt($checkSumGeneratorId) ^ toInt($hours) ^ toInt($milliseconds);
$checkSum_1 = toInt($clientServerConstant) ^ toInt(sumAlphanumeric($amount)) ^ toInt(sumAlphanumeric($cardNumber)) ^ toInt(sumAlphanumeric($invoiceNumber));
$checkSum_1 = append($checkSum_1);
$checkSum_2 = decToHex($hours)."_".decToHex($milliseconds)."_".decToHex($checkSum_1);
return($checkSum_2);
}
function validateChecksum( $checkSum_2 , $cardNumber , $amount ,$invoiceNumber)
{
$checkSumGeneratorId = mask(CHECKSUM_GENERATOR_ID);
$amount = maskAmount($amount);
$hours = hexToDec(substr( $checkSum_2 , 0 , strpos($checkSum_2,"_")));
$milliseconds = hexToDec(substr( $checkSum_2 , (strpos($checkSum_2,"_"))+1 , (strrpos($checkSum_2,"_"))-((strpos($checkSum_2,"_"))+1)));
$checksum_1 = unmask(hexToDec(substr( $checkSum_2 , (strrpos($checkSum_2,"_"))+1 , (strlen($checkSum_2))-((strrpos($checkSum_2,"_"))+1))));
//region verify hours
$currentTime = date('H');
if(($currentTime > unmask($hours)))
{
if(!(($currentTime - unmask($hours)) <= CHECKSUM_VALIDITY))
{
return(0);
}
}
if(($currentTime < unmask($hours)))
{
if(!(((24 + $currentTime) - unmask($hours)) <= CHECKSUM_VALIDITY))
{
return(0);
}
}
//end region
$clientServerConstant = toInt($checksum_1) ^ toInt(sumAlphanumeric($amount)) ^ toInt(sumAlphanumeric($cardNumber)) ^ toInt(sumAlphanumeric($invoiceNumber));
if($clientServerConstant != (toInt($checkSumGeneratorId) ^ toInt($hours) ^ toInt($milliseconds)))
{
return(0);
}
else
{
if(toInt(sumAlphanumeric($amount)) != (toInt($checksum_1) ^ toInt($clientServerConstant) ^ toInt(sumAlphanumeric($cardNumber)) ^ toInt(sumAlphanumeric($invoiceNumber))))
{
return(0);
}
if(toInt(sumAlphanumeric($amount)) == (toInt($checksum_1) ^ toInt($clientServerConstant) ^ toInt(sumAlphanumeric($cardNumber)) ^ toInt(sumAlphanumeric($invoiceNumber))))
{
return(1);
}
}
}
//$check = getChecksum( "2224442220000004.009abc" , "1500.sd" , "aabbcc" );
//echo $check;echo " ";
$validate = validateChecksum( "3f3_d3375f_a4b0529" , "2224442220000004.009abc" , "1500.sd" , "aabbcc" );
//echo getChecksum( "2224442220000001" , "999.999" , "1234567" );echo " ";
echo $validate; echo " ";
?>