Created
December 22, 2011 09:38
-
-
Save burinov/1509702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module (base64ex). | |
-export ([urlsafe_decode64/1, urlsafe_encode64/1]). | |
urlsafe_decode64(Str) -> | |
Str2 = re:replace(Str, "-", "+", [global, {return,list}]), | |
Str3 = re:replace(Str2, "_", "/", [global, {return,list}]), | |
base64:decode(Str3). | |
urlsafe_encode64(Bin) -> | |
Bin2 = base64:encode(Bin), | |
Bin3 = re:replace(binary_to_list(Bin2), "\\+", "-", [global, {return,list}]), | |
re:replace(Bin3, "/", "_", [global, {return,list}]). | |
% Decode base64, throwing a more intelligible exception if it fails. | |
decode_base64(Base64) -> | |
try base64:decode(Base64) | |
catch | |
error:_ -> throw({invalidBase64, Base64}) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment