initial checkin
authorClaus Gittinger <cg@exept.de>
Sun, 30 Sep 2018 15:55:46 +0200
changeset 4744 ed3d184041f4
parent 4743 6bdaef8ec48a
child 4745 8e46bdaeb066
initial checkin
Base64UrlCoder.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Base64UrlCoder.st	Sun Sep 30 15:55:46 2018 +0200
@@ -0,0 +1,97 @@
+"{ Package: 'stx:libbasic2' }"
+
+"{ NameSpace: Smalltalk }"
+
+Base64Coder subclass:#Base64UrlCoder
+	instanceVariableNames:''
+	classVariableNames:'Base64UrlMapping Base64UrlReverseMapping'
+	poolDictionaries:''
+	category:'System-Storage'
+!
+
+!Base64UrlCoder class methodsFor:'documentation'!
+
+documentation
+"
+    A variant of base64 encoding which generates url- and filename save encodings.
+    Same as Base64 except that instead of plus and slash, minus and underline are generated.
+
+    The main entry point API is
+        Base64UrlCoder encode:aStringOrBytes
+    and
+        Base64UrlCoder decode:aString
+
+    If the decoder should return a string, use
+        Base64UrlCoder decodeAsString:aString.
+
+    [author:]
+        Claus Gittinger
+
+    [see also:]
+        RFC https://tools.ietf.org/html/rfc4648
+
+    [instance variables:]
+
+    [class variables:]
+        Base64UrlMapping         String   Mapping from bytes (with 6 valid bits)
+                                          to Base64Url characters
+        Base64UrlReverseMapping  Array    Mapping from Base64Url characters to 6-bit-Bytes
+"
+!
+
+examples
+"
+                                                                [exBegin]
+   (Base64Coder encode:#[0 0 16r3F]) = 'AAA/'
+                                                                [exEnd]
+                                                                [exBegin]
+   (Base64Coder decode:'AAA/') = #[0 0 63]
+                                                                [exEnd]
+
+                                                                [exBegin]
+   (Base64UrlCoder encode:#[0 0 16r3F]) = 'AAA_'
+                                                                [exEnd]
+                                                                [exBegin]
+   (Base64UrlCoder decode:'AAA_') = #[0 0 63]
+                                                                [exEnd]
+"
+! !
+
+!Base64UrlCoder class methodsFor:'initialization'!
+
+initializeMappings
+    "initialize class variables"
+
+    Base64UrlMapping isNil ifTrue:[
+        "65 characters representing the 6-bit values from 0-63 and one pad character"
+        "/ notice: minus and underline instead of plus and slash
+        Base64UrlMapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_='.
+        Base64UrlReverseMapping := self reverseMappingFor:Base64UrlMapping.
+    ].
+
+    "
+     Base64UrlMapping := nil.
+     self initializeMappings
+    "
+
+    "Created: / 30-09-2018 / 15:20:47 / Claus Gittinger"
+!
+
+mapping
+    ^ Base64UrlMapping
+
+    "Created: / 30-09-2018 / 15:31:14 / Claus Gittinger"
+!
+
+reverseMapping
+    ^ Base64UrlReverseMapping
+
+    "Created: / 30-09-2018 / 15:31:21 / Claus Gittinger"
+! !
+
+!Base64UrlCoder class methodsFor:'documentation'!
+
+version_CVS
+    ^ '$Header$'
+! !
+