IsBase64: Difference between revisions
m (Created page with 'Test to determine if a string is [http://en.wikipedia.org/wiki/Base64 base64 encoded]. This check only shows if the string conforms to base64. It's possible that the string con…') |
mNo edit summary |
||
Line 16: | Line 16: | ||
Assert.assertFalse(RssFileUpload.isBase64("OMG!")); | 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> | </geshi> | ||
[[Category:Java]] | [[Category:Java]] | ||
[[Category:Regex]] | [[Category:Regex]] |
Revision as of 02:18, 19 October 2009
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>