keybase / triplesec

Triple Security for the browser and Node.js
https://keybase.io/triplesec
MIT License
399 stars 48 forks source link

Support for ArrayBuffer #18

Closed xcambar closed 10 years ago

xcambar commented 10 years ago

Hi, it's all in the title: though I supposed it is planned, would you add support for ArrayBuffer in triplesec?

Or could you provide some help with a gist on how to cypher from and decypher to an ArrayBuffer?

It is blocking for me at the moment, and I have to use the (very good though) CryptoJS as it has this feature... Further more, it would provide an awesome performance boost!

Thanks!

maxtaco commented 10 years ago

Hi something like this should work:

function triplesecBufferToArrayBuffer (b) {
   var ret = new ArrayBuffer(b.length);
   for (var i = 0; i < b.length; i++) {
     ret[i] = b.readUInt8(i)
   }
   return ret;
}

function arrayBufferToTriplesecBuffer (b) {
   var ret = new triplesec.Buffer(b.byteLength);
   for (var i = 0; i < b.byteLength; i++) {
     ret.writeUInt8(b[i], i);
   }
   return ret;
}
xcambar commented 10 years ago

Thanks for the answer, it helped a lot. Unfortunately, it didn't work out of the box. After further googling, I've found this to work:

function arrayBufferToTriplesecBuffer (b) {
  return new triplesec.Buffer( new Uint8Array(b)  );
}

function triplesecBufferToArrayBuffer (b) {
  var ret = new ArrayBuffer(b.length);
  var view = new Uint8Array(ret);
  for (var i = 0; i < b.length; i++) {
    view[i] = b.readUInt8(i);
  }
  return ret;
}
maxtaco commented 10 years ago

Ok, thanks for letting me know. Out of curiosity which browser were you on? I tested my snippet to work in Chrome I believe.

xcambar commented 10 years ago

Chrome also. I use triplesec with binary data, if that's of any kind of importance in that matter.

ColdHeat commented 6 years ago

Not sure if triplesec-3.0.14-min.js is the right file to be using but that is what I used and my code would not work without @xcambar 's function.

function arrayBufferToTriplesecBuffer (b) {
  return new triplesec.Buffer( new Uint8Array(b)  );
}

Without the conversion, binary data will be mostly correct but have some oddly incorrect bytes in output.