SphinxJS, A very light JS library which could encode a string to an image, or decode an image to a string.

Recently, I have created a project named SphinxJS, which can encodes a string to an image, or decodes a string from an image, too. It’s very light even less than 100 lines! Here is the project’s address below and you could play the demo either:

Project: https://github.com/jrainlau/sphinx
Live demo: http://jrainlau.github.io/sphinx

Usage

Install SphinxJS from npm

npm install sphinx.js

Use <script></script> tags

<script src="sphinx.js"></script>
Mind that SphinxJS uses ES2015 Promise and other amazing features, which means that it requires your browser’s support. If not, you might use ‘Babel’ or something else to build your code.
SphinxJS also supports AMD, CommonJS and ES6 Modules

Encode

Defined a string as Hello Sphinx!, and we’re going to encode it.

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

Then the base64 url is equal to

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

, the string information has encoded to an image successfully.

As you see, the encode() function returns a base64 url of an image.


Decode

Defined an image url

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

Now decode it!

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

The decode() function returns a Promise, which includes the string information decoded from the image.


Config

The new Sphinx() could recieve a config object to select the type of the image it created.

  • config {Object} optional default: {img: ‘png’}
new Sphinx({img: 'bmp'})

License

MIT