Skip to content

Instantly share code, notes, and snippets.

@oluies
Created September 10, 2011 08:37
Show Gist options
  • Save oluies/1208120 to your computer and use it in GitHub Desktop.
Save oluies/1208120 to your computer and use it in GitHub Desktop.
validate base64string
static final Pattern pattern = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
public static boolean isBase642(String base64) {
final Matcher m = pattern.matcher(base64);
return m.matches();
}
// with guava
// this is not really correct as = should only be allowed in the end
static final CharMatcher base64Chars = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).or(CharMatcher.WHITESPACE).or(CharMatcher.DIGIT).or(CharMatcher.is('=') );
public static boolean isBase64(String base64) {
return base64Chars.matchesAllOf(base64);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment