-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package is | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/goloop/g" | ||
) | ||
|
||
var ( | ||
// The base64StrictModeRegex is a regex pattern used to validate | ||
// standard Base64 encoded strings. | ||
// It matches strings that: | ||
// - consist of sequences of 4 valid Base64 characters (A-Za-z0-9+/), | ||
// repeated 0 or more times. | ||
// - optionally, end with a sequence of 2 valid Base64 characters | ||
// followed by '==', or 3 valid Base64 characters followed by '=', | ||
// or 4 valid Base64 characters. | ||
base64StrictModeRegex = regexp.MustCompile( | ||
`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|` + | ||
`[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`, | ||
) | ||
|
||
// The base64SafeModeRegex is similar to base64Regex, but for URL-safe | ||
// Base64 encoding. The '+' and '/' characters are replaced with '-' | ||
// and '_' respectively. And can be missing paddings (== or =). | ||
base64SafeModeRegex = regexp.MustCompile( | ||
`^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}|` + | ||
`[A-Za-z0-9-_]{3}|[A-Za-z0-9-_]{4})$`, | ||
) | ||
) | ||
|
||
// Base64 validates whether a given string is encoded in Base64. | ||
// By default, the function checks for strict compliance with | ||
// the base64 format. The second argument to the function can be | ||
// as false to check that the base64 URL is safe. | ||
// | ||
// Example usage: | ||
// | ||
// // Strict mode. | ||
// is.Base64("SGVsbG8sIHdvcmxkIQ==") // Returns: true | ||
// is.Base64("SGVsbG8sIHdvcmxkIQ==", true) // Returns: true | ||
// is.Base64("SGVsbG8sIHdvcmxkIQ") // Returns: false | ||
// is.Base64("SGVsbG8sIHdvcmxkIQ", true) // Returns: false | ||
// | ||
// // Safe mode. | ||
// is.Base64("SGVsbG8sIHdvcmxkIQ", false) // Returns: true | ||
// is.Base64("SGVsbG8sIHdvcmxkIQ==", false) // Returns: true | ||
func Base64(v string, strict ...bool) bool { | ||
// Default mode is a strict. | ||
if l := len(strict); l == 0 || l != 0 && g.All(strict...) { | ||
return base64StrictModeRegex.MatchString(v) | ||
} | ||
|
||
fmt.Println("Safe") | ||
return base64SafeModeRegex.MatchString(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package is | ||
|
||
import "testing" | ||
|
||
// TestBase64 tests Base64 function. | ||
func TestBase64(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
strict []bool | ||
want bool | ||
}{ | ||
/*{ | ||
name: "Valid standard base64", | ||
input: "SGVsbG8sIHdvcmxkIQ==", | ||
strict: []bool{}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Valid standard base64 with strict mode", | ||
input: "SGVsbG8sIHdvcmxkIQ==", | ||
strict: []bool{true}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Valid standard base64 with strict mode as many params", | ||
input: "SGVsbG8sIHdvcmxkIQ==", | ||
strict: []bool{true, true}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Valid standard base64 with safe mode", | ||
input: "SGVsbG8sIHdvcmxkIQ==", | ||
strict: []bool{false}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Valid standard base64 with safe mode as many params", | ||
input: "SGVsbG8sIHdvcmxkIQ==", | ||
strict: []bool{true, true, false}, // any false is false exp. | ||
want: true, | ||
}, | ||
{ | ||
name: "Not valid standard base64 (missing padding)", | ||
input: "SGVsbG8sIHdvcmxkIQ", | ||
strict: []bool{}, | ||
want: false, | ||
},*/ | ||
{ | ||
name: "Valid base64 with missing padding for safe mode", | ||
input: "SGVsbG8sIHdvcmxkIQ", | ||
strict: []bool{false}, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := Base64(test.input, test.strict...) | ||
if got != test.want { | ||
t.Errorf("Base64(%q, %v) = %v; want %v", | ||
test.input, test.strict, got, test.want) | ||
} | ||
}) | ||
} | ||
} |