github.com/lestrrat-go/jwx/v2@v2.0.21/internal/base64/asmbase64.go (about) 1 //go:build jwx_asmbase64 2 3 package base64 4 5 import ( 6 "fmt" 7 8 asmbase64 "github.com/segmentio/asm/base64" 9 ) 10 11 func init() { 12 SetEncoder(asmbase64.RawURLEncoding) 13 SetDecoder(asmDecoder{}) 14 } 15 16 type asmDecoder struct{} 17 18 func (d asmDecoder) Decode(src []byte) ([]byte, error) { 19 var enc *asmbase64.Encoding 20 switch Guess(src) { 21 case Std: 22 enc = asmbase64.StdEncoding 23 case RawStd: 24 enc = asmbase64.RawStdEncoding 25 case URL: 26 enc = asmbase64.URLEncoding 27 case RawURL: 28 enc = asmbase64.RawURLEncoding 29 default: 30 return nil, fmt.Errorf(`invalid encoding`) 31 } 32 33 dst := make([]byte, enc.DecodedLen(len(src))) 34 n, err := enc.Decode(dst, src) 35 if err != nil { 36 return nil, fmt.Errorf(`failed to decode source: %w`, err) 37 } 38 return dst[:n], nil 39 }