-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormat.php
73 lines (66 loc) · 1.58 KB
/
Format.php
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
<?php
namespace CryptTor;
/**
* Class Format
*
* @author Daniel Toader <developer@danieltoader.com>
* @package Crypt
*/
class Format
{
/**
* Process input/output string in raw format
*/
const FORMAT_RAW = 0;
/**
* Process input/output string in base64 format
*/
const FORMAT_B64 = 1;
/**
* Process input/output string in hexadecimal format
*/
const FORMAT_HEX = 2;
/**
* Format the output string
*
* @param string $string
* @param int $format
* @return string
*/
public static function output($string, $format)
{
if ($format == self::FORMAT_B64) {
$string = base64_encode($string);
} elseif ($format == self::FORMAT_HEX) {
$string = unpack('H*', $string)[1];
}
return $string;
}
/**
* Format the input string
*
* @param string $string
* @param int $format
* @return string
*/
public static function input($string, $format)
{
if ($format == self::FORMAT_B64) {
$string = base64_decode($string);
} elseif ($format == self::FORMAT_HEX) {
$string = pack('H*', $string);
}
return $string;
}
/**
* Validate that format is one of the following FORMAT_RAW, FORMAT_B64 or FORMAT_HEX
*
* @param int $format
*/
public static function validate($format)
{
if(!in_array($format, [self::FORMAT_RAW, self::FORMAT_B64, self::FORMAT_HEX])){
throw new \InvalidArgumentException('Format not valid');
}
}
}