Does SJCL support TypedArray and Blob as input or output?

308 views
Skip to first unread message

tom

unread,
Aug 14, 2012, 10:16:54 PM8/14/12
to sjcl-d...@googlegroups.com

it seems SJCL support only string as input/ouput. In case html5 media stream encryption,  it's important encrypt/decrypt on binary data, instead of string. Does SJSCL support binary typedarray or blob as input/output?

Best regards
  Tom

 

vincenzoml

unread,
Aug 15, 2012, 8:44:20 AM8/15/12
to sjcl-d...@googlegroups.com

Hi,

I just asked the very same question a couple of days ago:


and ended up using the following code, where p is an object of options, and where sjcl.codec.bytes is a codec that needs to be configured in the source (./configure --help, then enable the codec, then make and you get your sjcl.js).

    function encrypt(password,plaintext) {
var prp = new sjcl.cipher[p.cipher](sjcl.codec.bytes.toBits(new Uint8Array(password)));
        var ct = sjcl.mode[p.mode].encrypt(prp, sjcl.codec.bytes.toBits(new Uint8Array(plaintext)), p.iv, p.adata, p.ts);
        return (new Uint8Array(sjcl.codec.bytes.fromBits(ct))).buffer;
    }
    
    function decrypt(password,ciphertext){
var prp = new sjcl.cipher[p.cipher](sjcl.codec.bytes.toBits(new Uint8Array(password)));
        pt = sjcl.mode[p.mode].decrypt(prp, sjcl.codec.bytes.toBits(new Uint8Array(ciphertext)), p.iv, p.adata, p.ts);   
        return(new Uint8Array(sjcl.codec.bytes.fromBits(pt))).buffer;
    }

Vincenzo

David Simon

unread,
Dec 9, 2016, 2:05:26 PM12/9/16
to SJCL discussion
More recent versions of SJCL have a codec for ArrayBuffer:

Reply all
Reply to author
Forward
0 new messages