base64.ss
#lang scheme/base

(require (prefix-in net: net/base64))

(define (encode input)
  (regexp-replace*
   #rx"[/+]"
   (regexp-replace* #rx"\r\n" (bytes->string/utf-8 (net:base64-encode input)) "")
   (λ (match)
     (if (equal? match "/") "-" "_"))))

(define (decode input)
  (net:base64-decode 
   (string->bytes/utf-8 
    (regexp-replace* 
     #rx"[-_]"
     input
     (λ (match)
       (if (equal? match "-") "/" "+"))))))

(provide encode decode)