فهرست منبع

Fixed the number of complements in Base64 encoding (#10564)

* fixed the number of complements

* revert changes to non-PHP library

* add tests for Base64
saharan 3 سال پیش
والد
کامیت
3d940a97e9
2فایلهای تغییر یافته به همراه51 افزوده شده و 6 حذف شده
  1. 6 6
      std/php/_std/haxe/crypto/Base64.hx
  2. 45 0
      tests/unit/src/unitstd/haxe/crypto/Base64.unit.hx

+ 6 - 6
std/php/_std/haxe/crypto/Base64.hx

@@ -44,10 +44,10 @@ class Base64 {
 
 
 	public static inline function decode(str:String, complement = true):Bytes {
 	public static inline function decode(str:String, complement = true):Bytes {
 		if (!complement) {
 		if (!complement) {
-			switch (strlen(str) % 3) {
-				case 1:
-					str += "==";
+			switch (strlen(str) % 4) {
 				case 2:
 				case 2:
+					str += "==";
+				case 3:
 					str += "=";
 					str += "=";
 				default:
 				default:
 			}
 			}
@@ -65,10 +65,10 @@ class Base64 {
 
 
 	public static inline function urlDecode(str:String, complement = false):Bytes {
 	public static inline function urlDecode(str:String, complement = false):Bytes {
 		if (complement) {
 		if (complement) {
-			switch (strlen(str) % 3) {
-				case 1:
-					str += "==";
+			switch (strlen(str) % 4) {
 				case 2:
 				case 2:
+					str += "==";
+				case 3:
 					str += "=";
 					str += "=";
 				default:
 				default:
 			}
 			}

+ 45 - 0
tests/unit/src/unitstd/haxe/crypto/Base64.unit.hx

@@ -0,0 +1,45 @@
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("The quick brown fox jumps over the lazy dog"), true) == "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("The quick brown fox jumps over the lazy dog"), false) == "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("The quick brown fox jumps over the lazy dog"), true) == "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("The quick brown fox jumps over the lazy dog"), false) == "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";
+
+haxe.crypto.Base64.decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==", true).toString() == "The quick brown fox jumps over the lazy dog";
+haxe.crypto.Base64.decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw", false).toString() == "The quick brown fox jumps over the lazy dog";
+haxe.crypto.Base64.urlDecode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==", true).toString() == "The quick brown fox jumps over the lazy dog";
+haxe.crypto.Base64.urlDecode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw", false).toString() == "The quick brown fox jumps over the lazy dog";
+
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("a"), true) == "YQ==";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("ab"), true) == "YWI=";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("ab?"), true) == "YWI/";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("ab~c"), true) == "YWJ+Yw==";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("a"), false) == "YQ";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("ab"), false) == "YWI";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("ab?"), false) == "YWI/";
+haxe.crypto.Base64.encode(haxe.io.Bytes.ofString("ab~c"), false) == "YWJ+Yw";
+
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("a"), true) == "YQ==";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("ab"), true) == "YWI=";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("ab?"), true) == "YWI_";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("ab~c"), true) == "YWJ-Yw==";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("a"), false) == "YQ";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("ab"), false) == "YWI";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("ab?"), false) == "YWI_";
+haxe.crypto.Base64.urlEncode(haxe.io.Bytes.ofString("ab~c"), false) == "YWJ-Yw";
+
+haxe.crypto.Base64.decode("YQ==", true).toString() == "a";
+haxe.crypto.Base64.decode("YWI=", true).toString() == "ab";
+haxe.crypto.Base64.decode("YWI/", true).toString() == "ab?";
+haxe.crypto.Base64.decode("YWJ+Yw==", true).toString() == "ab~c";
+haxe.crypto.Base64.decode("YQ", false).toString() == "a";
+haxe.crypto.Base64.decode("YWI", false).toString() == "ab";
+haxe.crypto.Base64.decode("YWI/", false).toString() == "ab?";
+haxe.crypto.Base64.decode("YWJ+Yw", false).toString() == "ab~c";
+
+haxe.crypto.Base64.urlDecode("YQ==", true).toString() == "a";
+haxe.crypto.Base64.urlDecode("YWI=", true).toString() == "ab";
+haxe.crypto.Base64.urlDecode("YWI_", true).toString() == "ab?";
+haxe.crypto.Base64.urlDecode("YWJ-Yw==", true).toString() == "ab~c";
+haxe.crypto.Base64.urlDecode("YQ", false).toString() == "a";
+haxe.crypto.Base64.urlDecode("YWI", false).toString() == "ab";
+haxe.crypto.Base64.urlDecode("YWI_", false).toString() == "ab?";
+haxe.crypto.Base64.urlDecode("YWJ-Yw", false).toString() == "ab~c";