Nim base64 encode and validated decode
Nim
import std/[base64, strutils]
proc tryDecode(s: string): string =
let compact = s.strip()
if compact.len mod 4 != 0:
return "invalid base64: length not a multiple of 4"
try:
result = decode(compact)
except ValueError as e:
result = "invalid base64: " & e.msg
echo encode("hello, Nim")
echo tryDecode("aGVsbG8=")
echo tryDecode("not valid!@#")