Skip to content
Sonny Piers edited this page Nov 30, 2017 · 3 revisions

Internally, SJCL does all computation on bitArrays (analogous to Node.js' Buffer). To make working with data from a variety of sources easier, there are several supported codecs which handle translating between SJCL's bitArrays and other encodings.

All codecs present the exact same API: A fromBits function which takes a bitArray and outputs an encoding (usually a String) and a toBits function which takes an encoded object and outputs a bitArray.

var out = sjcl.hash.sha256.hash("Hello World!") // bitArray

sjcl.codec.hex.fromBits(out)
// 7f83b1657ff1fc53b92dc...

Implemented Codecs

Type Location Example Output
String sjcl.codec.utf8String 'Hello World!'
Base32 sjcl.codec.base32 '91jprv3f41bpywkccggg'
Base64 sjcl.codec.base64 'SGVsbG8gV29ybGQh'
Base64 URL sjcl.codec.base64url 'SGVsbG8gV29ybGQh'
Bytes sjcl.codec.bytes [ 72, 101, 108, 108, ...

Notes:

  1. Base64 URL is the same as Base64, except characters '+' and '/' are replaced with '-' and '_'
  2. Bytes must be included with the --with-codecBytes option.