Base64 encode and decode strings in Groovy
Groovy
import java.util.Base64
String encode(String text) {
Base64.getEncoder().encodeToString(text.getBytes('UTF-8'))
}
String decode(String encoded) {
try {
new String(Base64.getDecoder().decode(encoded), 'UTF-8')
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("invalid base64 input: ${e.message}", e)
}
}
def encoded = encode('hello groovy')
println encoded
println decode(encoded)