Skip to content

Instantly share code, notes, and snippets.

@oprypin
Created January 19, 2015 10:34
Show Gist options
  • Save oprypin/4c84e0ee0064e17d5f52 to your computer and use it in GitHub Desktop.
Save oprypin/4c84e0ee0064e17d5f52 to your computer and use it in GitHub Desktop.
urlsafe base64
import strutils
from base64 import nil
proc encodeUrlSafe(s: string): string =
result = base64.encode(s, newLine="")
while result.endsWith("="):
result = result.substr(0, result.high-1)
result = result.replace('+', '-').replace('/', '_')
proc decodeUrlSafe(s: string): string =
var s = s
while s.len mod 4 > 0:
s &= "="
base64.decode(s).replace('+', '-').replace('/', '_')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment