1 USING: base64 byte-arrays io.encodings.ascii io.encodings.string
2 kernel sequences splitting strings tools.test ;
4 { t } [ 256 <iota> >byte-array dup >base64 base64> = ] unit-test
6 { "abcdefghijklmnopqrstuvwxyz" } [ "abcdefghijklmnopqrstuvwxyz" ascii encode >base64 base64> ascii decode
8 { "" } [ "" ascii encode >base64 base64> ascii decode ] unit-test
9 { "a" } [ "a" ascii encode >base64 base64> ascii decode ] unit-test
10 { "ab" } [ "ab" ascii encode >base64 base64> ascii decode ] unit-test
11 { "abc" } [ "abc" ascii encode >base64 base64> ascii decode ] unit-test
12 { "abcde" } [ "abcde" ascii encode >base64 3 cut "\r\n" swap 3append base64> ascii decode ] unit-test
14 ! From http://en.wikipedia.org/wiki/Base64
15 { "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" }
17 "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
18 ascii encode >base64 >string
21 { "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" }
23 "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
24 ascii encode >base64-lines >string
27 [ { 33 52 17 40 12 51 33 43 18 33 23 } base64> ]
28 [ malformed-base64? ] must-fail-with
32 B{ 123 34 97 108 103 34 58 34 72 83 50 53 54 34 125 }
33 B{ 123 34 115 117 98 34 58 34 74 111 101 34 125 }
35 138 151 175 68 219 145 63 161 223 148 111 28 20 169 230
36 80 251 114 166 187 145 11 135 219 212 53 173 160 178 250
41 "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"
42 "." split [ base64> ] map
45 { "01a+b/cd" } [ "\xd3V\xbeo\xf7\x1d" >base64 "" like ] unit-test
46 { "\xd3V\xbeo\xf7\x1d" } [ "01a+b/cd" base64> "" like ] unit-test
48 { "01a-b_cd" } [ "\xd3V\xbeo\xf7\x1d" >urlsafe-base64 "" like ] unit-test
49 { "\xd3V\xbeo\xf7\x1d" } [ "01a-b_cd" urlsafe-base64> "" like ] unit-test
52 [ "{\"a\":\"bcd\"}" >urlsafe-base64-jwt >string ] unit-test
55 [ "{\"a\":\"bcd\"}" >urlsafe-base64-jwt urlsafe-base64> >string ] unit-test
57 { "" } [ "" >base64 >string ] unit-test
58 { "Zg==" } [ "f" >base64 >string ] unit-test
59 { "Zm8=" } [ "fo" >base64 >string ] unit-test
60 { "Zm9v" } [ "foo" >base64 >string ] unit-test
61 { "Zm9vYg==" } [ "foob" >base64 >string ] unit-test
62 { "Zm9vYmE=" } [ "fooba" >base64 >string ] unit-test
63 { "Zm9vYmFy" } [ "foobar" >base64 >string ] unit-test
65 { "" } [ "" base64> >string ] unit-test
66 { "f" } [ "Zg==" base64> >string ] unit-test
67 { "fo" } [ "Zm8=" base64> >string ] unit-test
68 { "foo" } [ "Zm9v" base64> >string ] unit-test
69 { "foob" } [ "Zm9vYg==" base64> >string ] unit-test
70 { "fooba" } [ "Zm9vYmE=" base64> >string ] unit-test
71 { "foobar" } [ "Zm9vYmFy" base64> >string ] unit-test