Giter VIP home page Giter VIP logo

base64's Introduction

Base64

Another Base64 encoder / decoder.

This one is

  • really small
  • not very efficient (the latest version is not too bad)
  • liberal license (BSD)*,
  • simple API
  • idiomatic scala, and
  • need not add a dependency to your project. Just copy the one source file to your project.
  • Scala-js compatible
  • no Dom required
  • works on client or server

Getting Started

Using SBT:

libraryDependencies += "com.github.marklister" %% "base64" % "0.3.0"

or for scala-js

libraryDependencies += "com.github.marklister" %%% "base64" % "0.3.0"

API

Simply invoke toBase64 on an Array[Byte] or Seq[Byte] or ArrayBuffer[Byte] or

toByteArray on a String containing a Base64 representation.

Imports

import com.github.marklister.base64.Base64.Encoder 
import com.github.marklister.base64.Base64.Decoder

or use a wildcard:

import com.github.marklister.base64.Base64._

Base64 Url

Two encoding schemes are provided as default: base64 and base64Url. base64 is the default one. To select base64Url

Using implicits -- just make the encode/decode scheme available via an implicit:

implicit val encoding = base64Url

Or you can provide the scheme as an argument to toByteArray or toBase64 eg toBase64(base64Url) or toByteArray(base64)

Padding

Padding is strict for the base64 encode scheme and non-strict for base64Url

To create a non strict scheme:

implicit val encoding = base64.copy(strictPadding=false)

Efficiency

This implementation began life as a toy implementation that chose simplicity over efficiency. The advent of scalaJs meant that the library has a real world target and warranted improving its efficiency. Version 0.2.1 improves efficiency greatly but some legacy inefficiencies remain.

REPL example

[info] Starting scala interpreter...
[info] 
import com.github.marklister.base64.Base64._
Welcome to Scala version 2.10.2 (OpenJDK Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> "ABCDEFG".getBytes.toBase64 //Encodes Array[Byte]=>String
res0: String = QUJDREVGRw==

scala> res0.toByteArray //Decodes base64 String=>Array[Byte]
res1: Array[Byte] = Array(65, 66, 67, 68, 69, 70, 71)

scala> res1.sameElements("ABCDEFG".getBytes)
res2: Boolean = true
  • At one stage this code was released as public domain but there was some discussion about the validity of public domain licences and they don't work on Maven Central. So now it's BSD. If you need some other licence then raise an issue.

base64's People

Contributors

daenyth avatar fedefernandez avatar jonas avatar marklister avatar mdedetrich avatar philippus avatar sethtisue avatar xuwei-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

base64's Issues

Improve performance

Lib began as a concise functional and very inefficient look at Base64. Since scala-js it's probably necessary to improve speed and reduce garbage production.

General approach will be to use some mutable data structures and direct array referencing.

Encoding is not compatible with Browser decoding

Hey, I compiled it to javascript using scala-js and if you do something like :

href = "data:application/octet-stream;base64," + data.stripMargin.getBytes.toBase64

Then the downloaded file ends with some incorrectly decoded characters, otherwise it is alright. So I suspect that the Webkit/Chrome decoding is not compatible with this base64 encoding...

Btw to make this scala-js compatible one just need to comment out the specs2 test suite (that doesn't compile to javascript) or rewrite it using some scala-js compliant testing framework like utest and add plugin :

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.0-M3")

and enable it :

lazy val base64 = (project in file(".")).enablePlugins(org.scalajs.sbtplugin.ScalaJSPlugin)

base64url

Thing to consider. There are more than one standards of base64. base64url, for example, is increasingly popular, because it makes embedding it in urls easier.

You may want to add support for base64url to your lib, unless you consider it too much bloat.

Strict padding unnecessary -- causes JS interop problem

JS WebCrytpoAPI produces Base64URL encoded strings in jws mode.

See the code here to produce those
http://stackoverflow.com/questions/33642310/is-there-a-way-to-get-the-components-of-an-rsa-key-in-the-webcrypto-api

The following JS code does the trick to convert it

function encodeURL(str){
    return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
}

function decodeUrl(str){
    str = (str + '===').slice(0, str.length + (str.length % 4));
    return str.replace(/-/g, '+').replace(/_/g, '/');
}

and then one can use window.atob

I tried the library here, but it gives an error

Release with latest scalajs 1.x?

Hi @marklister

I was wondering whether you could release a new version against the latest scalajs? It's actually luckily humming along if it weren't for those big upgrades :-)

Thanks a lot,
Tanju

Consider structures other than `Array[Byte]`

From #6 (@bblfish)

Perhaps there is an extra improvement you can add. Is it possible to abstract somewhat from an array? For example the JS seems to return a ArrayBuffer which I think can be turned into a java.nio.ByteBuffer using TypedArrayBufferBridge.

So it would be useful to have a method that could work with those elements and not just arrays. I am not yet quite sure what the best abstraction here would be... Certainly given that you don't need to change the source object, an Array is too precise.

There is some example code here using a ByteBuffer https://gist.github.com/mseddon/1cfcb0970272cac40497

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.