Giter VIP home page Giter VIP logo

Comments (8)

marklister avatar marklister commented on July 2, 2024

RFC 4648

I might be able to use a similar approach to what I did in in the CSV parser of product-collections to simplify the usage.

from base64.

baturinsky avatar baturinsky commented on July 2, 2024

I'm not sure about use cases. Whether people need to have lib that does both, or have two slightly small libs that can do one each? I think second option is more likely. People either prefer "classic" base64 or base64url, and only need to have one.

from base64.

marklister avatar marklister commented on July 2, 2024

Commit 3e17910 should suffice.

from base64.

baturinsky avatar baturinsky commented on July 2, 2024

I think there is also a need of option to omit padding. Or even make it off by default in base64url.
I'd add padding char as the last one in B64Scheme, and if it is omitted - no padding.

from base64.

marklister avatar marklister commented on July 2, 2024

I re read the rfc and I'm not convinced:

Josefsson                   Standards Track                     [Page 3]

RFC 4648                    Base-N Encodings                October 2006


3.2.  Padding of Encoded Data

   In some circumstances, the use of padding ("=") in base-encoded data
   is not required or used.  In the general case, when assumptions about
   the size of transported data cannot be made, padding is required to
   yield correct decoded data.

   Implementations MUST include appropriate pad characters at the end of
   encoded data unless the specification referring to this document
   explicitly states otherwise.

   The base64 and base32 alphabets use padding, as described below in
   sections 4 and 6, but the base16 alphabet does not need it; see
   section 8.

My current implementation has the decode table recomputed at each decode. I'll fix this shortly by encapsulating B64Scheme in it's own class. It would be possible to put padding into this class but I don't think there's much use for unpadded base64...

from base64.

baturinsky avatar baturinsky commented on July 2, 2024

base64url is supposed to be used in URLs, therefore omission of usual reserved characters.

http://tools.ietf.org/html/rfc3986#section-3.4

URI producing applications often use the reserved characters allowed in a segment to delimit
scheme-specific or dereference-handler-specific subcomponents. For
example, the semicolon (";") and equals ("=") reserved characters are
often used to delimit parameters and parameter values applicable to
that segment. The comma (",") reserved character is often used for
similar purposes. For example, one URI producer might use a segment
such as "name;v=1.1" to indicate a reference to version 1.1 of
"name", whereas another might use a segment such as "name,1.1" to
indicate the same.

from base64.

marklister avatar marklister commented on July 2, 2024

The problem is that if padding is omitted the encoding is non canonical.

The RFC suggests percent encoding the "=" character.

The pad character "=" is typically percent-encoded when used in an
   URI [9], but if the data length is known implicitly, this can be
   avoided by skipping the padding; see section 3.2.

I guess the padding could be stripped:

scala> "abcd".getBytes.toBase64
res1: String = YWJjZA==

scala> res1.reverse.dropWhile(_=='=').reverse
res3: String = YWJjZA

And if wanted to disable it I'd do something like this:

class  B64Scheme (val encodeTable:IndexedSeq[Char], paddding:Boolean=true){
    lazy val decodeTable=collection.immutable.TreeMap(encodeTable.zipWithIndex : _*)
    lazy val pad = if (padding) 3 else 1
  }

Then substitute 'pad' for 3 in the rest of the code.

from base64.

marklister avatar marklister commented on July 2, 2024

I'll push branch "nopadding" shortly:

scala> implicit val scheme = new B64Scheme(base64.encodeTable,false)
scheme: io.github.marklister.base64.Base64.B64Scheme = io.github.marklister.base64.Base64$B64Scheme@14bef04

scala> "abcd".getBytes.toBase64
res1: String = 9hYmNk

scala> res1.toByteArray.map(_.toChar)
res2: Array[Char] = Array(�, a, b, c, d)

You'll see the issue with non-canonical encoding.

from base64.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.