Sphinx JS-An Ultralight Open Source Library Encoding String into png Pictures

  base64, canvas, es6, html5, javascript

logo

Experience address:https://jrainlau.github.io/sp …
Project address:https://github.com/jrainlau/s …

SphinxJS

An ultra-light open source library that can encode strings into png pictures or decode strings from png pictures, with the total number of codes not exceeding 100 lines!

Use method

vianpmInstallation

npm install sphinx.js

via<script></script>Label introduction

<script src="sphinx.js"></script>

It is worth noting that becauseSphinxJSUsing ES2015PromiseAs well as other great features, which means your browser is required to provide support. Otherwise, you may need toBabelOr some other tool to build your code.

SphinxJSAt the same time support toAMD,CommonJSas well asES6 moduleTo be used as a reference.

Coding

Define a stringHello Sphinx!, we will encode it

let base64URL = new Sphinx().encode('Hello Sphinx!'  )

Then you will get a stringbase64Coding

data:image/png;  base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAAJklEQVQYV2P0SM35r8K1heE5owcDY2Zexf8dc1Yw/BdjYGBkQAIA+r4JjQKvLx4AAAAASUVORK5CYII=

At this step, the coding has been completed.

As you can see,encode()The method returns the of a picturebase64Code.

Decoding

Url defining a string of pictures:

let url = 'data:image/png;  base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAAJklEQVQYV2P0SM35r8K1heE5owcDY2Zexf8dc1Yw/BdjYGBkQAIA+r4JjQKvLx4AAAAASUVORK5CYII='

Then we decode it!

new Sphinx().decode(url)
 .then((info) => {
 console.log(info) // Hello Sphinx!
 })

decode()The method returns aPromiseObject that contains string information decrypted from the picture.

Configuration

new Sphinx()Can receive a configuration object for defining picture types

  • config {Object}OptionalDefault value:{img: 'png'}

new Sphinx({img: 'bmp'})

Certificate

MIT