Base64UrlCoder.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5377 21cb5f70fbd5
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:

"
 COPYRIGHT (c) 2018 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.  This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

Base64Coder subclass:#Base64UrlCoder
	instanceVariableNames:''
	classVariableNames:'Base64UrlMapping Base64UrlReverseMapping'
	poolDictionaries:''
	category:'System-Storage'
!

!Base64UrlCoder class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2018 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.  This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

"
!

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$'
! !