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>
7474
7575 _should : {
7676 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." )
7880 }
7981 } ,
8082
@@ -117,9 +119,14 @@ <h1>Base64 Encoding/Decoding Tests</h1>
117119 } ,
118120
119121 testInvalidChar : function ( ) {
120- base64Decode ( "TWF,JFU" ) ;
122+ base64Decode ( "aGF,0Y2g" ) ;
123+ } ,
124+
125+ testInvalidString : function ( ) {
126+ base64Decode ( "aGF0Y2g==" ) ;
121127 }
122128
129+
123130 } ) ) ;
124131
125132
Original file line number Diff line number Diff line change @@ -79,8 +79,11 @@ function base64Encode(text){
7979 */
8080function base64Decode ( text ) {
8181
82+ //ignore white space
83+ text = text . replace ( / \s / g, "" ) ;
84+
8285 //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 ) {
8487 throw new Error ( "Not a base64-encoded string." ) ;
8588 }
8689
@@ -91,7 +94,7 @@ function base64Decode(text){
9194 result = [ ] ;
9295
9396 //remove any whitespace and equals signs
94- text = text . replace ( / [ \s = ] / g, "" ) ;
97+ text = text . replace ( / = / g, "" ) ;
9598
9699 //loop over each character
97100 while ( i < text . length ) {
You can’t perform that action at this time.
0 commit comments