File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,9 @@ <h1>Base64 Encoding/Decoding Tests</h1>
74
74
75
75
_should : {
76
76
error : {
77
- testInvalidChar : new Error ( "Not a base64-encoded string." )
77
+ testInvalidChar : new Error ( "Not a base64-encoded string." ) ,
78
+ testInvalidString : new Error ( "Not a base64-encoded string." ) ,
79
+ testMissingPaddingIndicator : new Error ( "Not a base64-encoded string." )
78
80
}
79
81
} ,
80
82
@@ -117,9 +119,14 @@ <h1>Base64 Encoding/Decoding Tests</h1>
117
119
} ,
118
120
119
121
testInvalidChar : function ( ) {
120
- base64Decode ( "TWF,JFU" ) ;
122
+ base64Decode ( "aGF,0Y2g" ) ;
123
+ } ,
124
+
125
+ testInvalidString : function ( ) {
126
+ base64Decode ( "aGF0Y2g==" ) ;
121
127
}
122
128
129
+
123
130
} ) ) ;
124
131
125
132
Original file line number Diff line number Diff line change @@ -79,8 +79,11 @@ function base64Encode(text){
79
79
*/
80
80
function base64Decode ( text ) {
81
81
82
+ //ignore white space
83
+ text = text . replace ( / \s / g, "" ) ;
84
+
82
85
//first check for any unexpected input
83
- if ( ! ( / ^ [ a - z 0 - 9 \+ \/ \s ] + \= { 0 , 2 } $ / i. test ( text ) ) ) {
86
+ if ( ! ( / ^ [ a - z 0 - 9 \+ \/ \s ] + \= { 0 , 2 } $ / i. test ( text ) ) | text . length % 4 > 0 ) {
84
87
throw new Error ( "Not a base64-encoded string." ) ;
85
88
}
86
89
@@ -91,7 +94,7 @@ function base64Decode(text){
91
94
result = [ ] ;
92
95
93
96
//remove any whitespace and equals signs
94
- text = text . replace ( / [ \s = ] / g, "" ) ;
97
+ text = text . replace ( / = / g, "" ) ;
95
98
96
99
//loop over each character
97
100
while ( i < text . length ) {
You can’t perform that action at this time.
0 commit comments