Skip to content

Commit

Permalink
Add isBase64 method to SecurityIO
Browse files Browse the repository at this point in the history
  • Loading branch information
samrocketman committed Mar 28, 2024
1 parent 620a60f commit b1706e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/groovy/net/gleske/jervis/tools/SecurityIO.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -898,4 +898,20 @@ println "Encrypt time: ${timing1} second(s)\nDecrypt time: ${timing2} second(s)"
Integer iterations = (hash_iterations > 0) ? hash_iterations : 1
decryptWithAES256(b_secret, b_iv, b_data, iterations)
}

/*
Check if String is valid base64 according to <a href="https://datatracker.ietf.org/doc/html/rfc4648">RFC 4648</a>.
@param s Check if String is similar to valid base64 encoding.
@return <tt>true</tt> if it looks like valid base64 or <tt>false</tt> if not.
*/
static Boolean isBase64(String s) {
if(s.trim().size() == 0) {
return true
}
if(s.size() < 4) {
return false
}
s.tokenize('\n').join('') =~ /^[=0-9a-zA-Z+\/]+$/
}
}
7 changes: 7 additions & 0 deletions src/test/groovy/net/gleske/jervis/tools/SecurityIOTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,11 @@ class SecurityIOTest extends GroovyTestCase {
assert ciphertext != SecurityIO.encryptWithAES256(passphrase, plaintext, 2)
assert plaintext == SecurityIO.decryptWithAES256(passphrase, ciphertext, 1)
}
@Test public void test_SecurityIO_isBase64() {
assert SecurityIO.isBase64('') == true
assert SecurityIO.isBase64('YQ==') == true
assert SecurityIO.isBase64('YQ=') == false
assert SecurityIO.isBase64('YQ\n==\n') == true
assert SecurityIO.isBase64('hello world') == false
}
}

0 comments on commit b1706e5

Please sign in to comment.