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:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5377
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     1
"
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     2
 COPYRIGHT (c) 2018 by eXept Software AG
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     3
              All Rights Reserved
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     4
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     5
 This software is furnished under a license and may be used
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     6
 only in accordance with the terms of that license and with the
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     7
 inclusion of the above copyright notice.  This software may not
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     8
 be provided or otherwise made available to, or used by, any
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
     9
 other person.  No title to or ownership of the software is
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    10
 hereby transferred.
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    11
"
4744
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ NameSpace: Smalltalk }"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
Base64Coder subclass:#Base64UrlCoder
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:''
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:'Base64UrlMapping Base64UrlReverseMapping'
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'System-Storage'
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!Base64UrlCoder class methodsFor:'documentation'!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
5377
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    25
copyright
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    26
"
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    27
 COPYRIGHT (c) 2018 by eXept Software AG
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    28
              All Rights Reserved
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    29
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    30
 This software is furnished under a license and may be used
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    31
 only in accordance with the terms of that license and with the
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    32
 inclusion of the above copyright notice.  This software may not
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    33
 be provided or otherwise made available to, or used by, any
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    34
 other person.  No title to or ownership of the software is
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    35
 hereby transferred.
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    36
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    37
"
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    38
!
21cb5f70fbd5 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4744
diff changeset
    39
4744
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
documentation
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    A variant of base64 encoding which generates url- and filename save encodings.
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    Same as Base64 except that instead of plus and slash, minus and underline are generated.
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
    The main entry point API is
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
        Base64UrlCoder encode:aStringOrBytes
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
    and
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
        Base64UrlCoder decode:aString
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    If the decoder should return a string, use
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
        Base64UrlCoder decodeAsString:aString.
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
    [author:]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
        Claus Gittinger
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
    [see also:]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
        RFC https://tools.ietf.org/html/rfc4648
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
    [instance variables:]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    [class variables:]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
        Base64UrlMapping         String   Mapping from bytes (with 6 valid bits)
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
                                          to Base64Url characters
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
        Base64UrlReverseMapping  Array    Mapping from Base64Url characters to 6-bit-Bytes
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
examples
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
                                                                [exBegin]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
   (Base64Coder encode:#[0 0 16r3F]) = 'AAA/'
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
                                                                [exEnd]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
                                                                [exBegin]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
   (Base64Coder decode:'AAA/') = #[0 0 63]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
                                                                [exEnd]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
                                                                [exBegin]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
   (Base64UrlCoder encode:#[0 0 16r3F]) = 'AAA_'
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
                                                                [exEnd]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
                                                                [exBegin]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
   (Base64UrlCoder decode:'AAA_') = #[0 0 63]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
                                                                [exEnd]
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
! !
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
!Base64UrlCoder class methodsFor:'initialization'!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
initializeMappings
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    "initialize class variables"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    Base64UrlMapping isNil ifTrue:[
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
        "65 characters representing the 6-bit values from 0-63 and one pad character"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
        "/ notice: minus and underline instead of plus and slash
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
        Base64UrlMapping := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_='.
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
        Base64UrlReverseMapping := self reverseMappingFor:Base64UrlMapping.
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
    ].
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
    "
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
     Base64UrlMapping := nil.
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
     self initializeMappings
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
    "
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    "Created: / 30-09-2018 / 15:20:47 / Claus Gittinger"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
mapping
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    ^ Base64UrlMapping
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
    "Created: / 30-09-2018 / 15:31:14 / Claus Gittinger"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
reverseMapping
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    ^ Base64UrlReverseMapping
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    "Created: / 30-09-2018 / 15:31:21 / Claus Gittinger"
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
! !
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
!Base64UrlCoder class methodsFor:'documentation'!
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
version_CVS
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    ^ '$Header$'
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
! !
ed3d184041f4 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123