"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"
"." split [ base64> ] map
] unit-test
+
+{ "01a+b/cd" } [ "\xd3V\xbeo\xf7\x1d" >base64 "" like ] unit-test
+{ "\xd3V\xbeo\xf7\x1d" } [ "01a+b/cd" base64> "" like ] unit-test
+
+{ "01a-b_cd" } [ "\xd3V\xbeo\xf7\x1d" >urlsafe-base64 "" like ] unit-test
+{ "\xd3V\xbeo\xf7\x1d" } [ "01a-b_cd" urlsafe-base64> "" like ] unit-test
! Copyright (C) 2008 Doug Coleman, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
-USING: arrays combinators fry io io.binary io.encodings.binary
-io.streams.byte-array kernel literals math namespaces sbufs
-sequences ;
+USING: arrays assocs byte-arrays combinators fry io io.binary
+io.encodings.binary io.streams.byte-array kernel literals math
+namespaces sbufs sequences ;
IN: base64
ERROR: malformed-base64 ;
<PRIVATE
<<
-CONSTANT: alphabet
+CONSTANT: alphabet $[
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+ >byte-array
+]
: alphabet-inverse ( alphabet -- seq )
dup supremum 1 + f <array> [
: >base64-lines ( seq -- base64 )
binary [ binary [ encode-base64-lines ] with-byte-reader ] with-byte-writer ;
+
+: >urlsafe-base64 ( seq -- base64 )
+ >base64 H{
+ { CHAR: + CHAR: - }
+ { CHAR: / CHAR: _ }
+ } substitute ;
+
+: urlsafe-base64> ( base64 -- seq )
+ H{
+ { CHAR: - CHAR: + }
+ { CHAR: _ CHAR: / }
+ } substitute base64> ;
+
+: >urlsafe-base64-lines ( seq -- base64 )
+ >base64-lines H{
+ { CHAR: + CHAR: - }
+ { CHAR: / CHAR: _ }
+ } substitute ;