UNPKG

sjcl-aws

Version:

Stanford Javascript Crypto Library

43 lines (40 loc) 955 B
/** @fileOverview Bit array codec implementations. * * @author Emily Stark * @author Mike Hamburg * @author Dan Boneh */ /** * UTF-8 strings * @namespace */ sjcl.codec.utf8String = { /** Convert from a bitArray to a UTF-8 string. */ fromBits: function (arr) { var out = "", bl = sjcl.bitArray.bitLength(arr), i, tmp; for (i=0; i<bl/8; i++) { if ((i&3) === 0) { tmp = arr[i/4]; } out += String.fromCharCode(tmp >>> 8 >>> 8 >>> 8); tmp <<= 8; } return decodeURIComponent(escape(out)); }, /** Convert from a UTF-8 string to a bitArray. */ toBits: function (str) { str = unescape(encodeURIComponent(str)); var out = [], i, tmp=0; for (i=0; i<str.length; i++) { tmp = tmp << 8 | str.charCodeAt(i); if ((i&3) === 3) { out.push(tmp); tmp = 0; } } if (i&3) { out.push(sjcl.bitArray.partial(8*(i&3), tmp)); } return out; } };