This repository was archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathBase64Codec.php
More file actions
executable file
·130 lines (118 loc) · 3.63 KB
/
Base64Codec.php
File metadata and controls
executable file
·130 lines (118 loc) · 3.63 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
<?php
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project.
*
* LICENSE: This source file is subject to the New BSD license. You should read
* and accept the LICENSE before you use, modify, and/or redistribute this
* software.
*
* PHP version 5.2
*
* @category OWASP
* @package ESAPI_Codecs
* @author Martin Reiche <martin.reiche.ka@gmail.com>
* @author jah <jah@jahboite.co.uk>
* @author Mike Boberski <boberski_michael@bah.com>
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version SVN: $Id$
* @link http://www.owasp.org/index.php/ESAPI
*/
require_once dirname(__FILE__) . '/Codec.php';
require_once dirname(__FILE__) . '/../ESAPI.php';
/**
* Reference implementation of the base 64 codec.
*
* @category OWASP
* @package ESAPI_Codecs
* @author Martin Reiche <martin.reiche.ka@gmail.com>
* @author jah <jah@jahboite.co.uk>
* @author Mike Boberski <boberski_michael@bah.com>
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version Release: @package_version@
* @link http://www.owasp.org/index.php/ESAPI
*/
class Base64Codec extends Codec
{
/**
* Public Constructor
*/
function __construct()
{
$logger = ESAPI::getAuditor("Base64");
}
/**
* Encodes the input string to Base64.
*
* The output is wrapped at 76 characters by default, but this behaviour may
* be overridden by supplying a value of boolean false for the $wrap
* parameter.
*
* @param string $input the input string to be encoded
* @param bool $wrap if should wrap output
*
* @return string the encoded string
*/
public function encode($input, $wrap = true)
{
$encoded = base64_encode($input);
if ($wrap === false) {
return $encoded;
}
// wrap encoded string into lines of not more than 76 characters
$detectedCharacterEncoding = Codec::detectEncoding($encoded);
$wrapped = '';
$limit = mb_strlen($encoded, $detectedCharacterEncoding);
$index = 0;
while ($index < $limit) {
if ($wrapped != '') {
$wrapped .= "\r\n";
}
$wrapped .= mb_substr($encoded, $index, 76);
$index += 76;
}
return $wrapped;
}
/**
* Encodes a single character to Base64.
*
* @param string $input the character to encode
*
* @return string the base64 encoded character
*/
public function encodeCharacter($input, $c = '')
{
$detectedCharacterEncoding = Codec::detectEncoding($input);
$c = mb_substr(
$input, 0, 1,
$detectedCharacterEncoding
);
return $this->encode($c, false);
}
/**
* Decodes the given input string from Base64 to plain text.
*
* @param string $input the base64 encoded input string
*
* @return string the decoded string
*/
public function decode($input)
{
return base64_decode($input);
}
/**
* Decodes a character from Base64 to plain text
*
* @param string $input the character to decode
*
* @return string the decoded character
*/
public function decodeCharacter($input)
{
return $this->decode($input);
}
}