base64: adding urlsafe base64 and specify RFC 3548.

clean-macosx-x86-32
John Benediktsson 2019-04-05 12:03:00 -07:00
parent a8b6d7bd4f
commit ab88710e74
3 changed files with 31 additions and 5 deletions

View File

@ -39,3 +39,9 @@ sequences splitting strings tools.test ;
"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

View File

@ -1,8 +1,8 @@
! 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 ;
@ -10,8 +10,10 @@ ERROR: malformed-base64 ;
<PRIVATE
<<
CONSTANT: alphabet
CONSTANT: alphabet $[
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
>byte-array
]
: alphabet-inverse ( alphabet -- seq )
dup supremum 1 + f <array> [
@ -100,3 +102,21 @@ PRIVATE>
: >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 ;

View File

@ -1 +1 @@
Base64 encoding/decoding
Base64 encoding/decoding (RFC 3548)