IsBase64
Test to determine if a string is base64 encoded. This check only shows if the string conforms to base64. It's possible that the string conforms, but is not in fact encoded.
<geshi lang="java5"> static boolean isBase64(String s) { return (s.length() % 4 == 0) && s.matches("^[A-Za-z0-9+/]+[=]{0,2}$"); } </geshi>
<geshi lang="java5"> @Test public void testIsBase64() throws Exception { Assert.assertTrue(RssFileUpload.isBase64("bGVhc3VyZS4=")); Assert.assertTrue(RssFileUpload.isBase64("ZWFzdXJlLg==")); Assert.assertTrue(RssFileUpload.isBase64("YXN1cmUu")); Assert.assertTrue(RssFileUpload.isBase64("c3VyZS4=")); Assert.assertFalse(RssFileUpload.isBase64("OMG!")); } </geshi>
Python
The same function in Python: <geshi lang="python"> def isBase64(s):
return (len(s) % 4 == 0) and re.match('^[A-Za-z0-9+/]+[=]{0,2}$', s)
</geshi>