diff options
author | Maximilian Krambach <[email protected]> | 2018-08-22 14:32:31 +0000 |
---|---|---|
committer | Maximilian Krambach <[email protected]> | 2018-08-22 14:32:31 +0000 |
commit | 129fa919b935d97d995bc6b457c7f6984c06e825 (patch) | |
tree | d25245b9ae7634acd99d713e12832fd97c988af0 /lang/js/src/Helpers.js | |
parent | js: Return error if signature has no fingerprint (diff) | |
download | gpgme-129fa919b935d97d995bc6b457c7f6984c06e825.tar.gz gpgme-129fa919b935d97d995bc6b457c7f6984c06e825.zip |
js: improve decryption performance
--
* src/Connection.js, src/Helpers.js: performance of decoding incoming
base64 data was improved to about 4 times the speed by introducing
two more efficient functions (thanks to [email protected] for
finding and testing them)
* src/gpgmejs.js: Decrypted data will now return as Uint8Array, if the
caller does not wish for a decoding. Decoding binary data will return
invalid data, and a Uint8Array may be desired. This can be indicated
by using the (new) 'binary' option in decrypt.
* src/Errors.js A new error in case this decoding fails
* src/Message.js, src/Connection.js: expected is change from base64
to binary, to avoid confusion later on.
Diffstat (limited to 'lang/js/src/Helpers.js')
-rw-r--r-- | lang/js/src/Helpers.js | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index ba4277ab..9fa5775b 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -134,4 +134,73 @@ export function decode (property){ return property; } return property; -}
\ No newline at end of file +} + +/** + * Turns a base64 encoded string into an uint8 array + * @param {String} base64 encoded String + * @returns {Uint8Array} + * adapted from https://gist.github.com/borismus/1032746 + */ +export function atobArray (base64) { + if (typeof (base64) !== 'string'){ + throw gpgme_error('DECODE_FAIL'); + } + const raw = window.atob(base64); + const rawLength = raw.length; + let array = new Uint8Array(new ArrayBuffer(rawLength)); + for (let i = 0; i < rawLength; i++) { + array[i] = raw.charCodeAt(i); + } + return array; +} + +/** + * Turns a Uint8Array into an utf8-String + * @param {*} array Uint8Array + * @returns {String} + * Taken and slightly adapted from + * http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt + * (original header: + * utf.js - UTF-8 <=> UTF-16 convertion + * + * Copyright (C) 1999 Masanao Izumo <[email protected]> + * Version: 1.0 + * LastModified: Dec 25 1999 + * This library is free. You can redistribute it and/or modify it. + * ) + */ +export function Utf8ArrayToStr (array) { + let out, i, len, c, char2, char3; + out = ''; + len = array.length; + i = 0; + if (array instanceof Uint8Array === false){ + throw gpgme_error('DECODE_FAIL'); + } + while (i < len) { + c = array[i++]; + switch (c >> 4) { + case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: + // 0xxxxxxx + out += String.fromCharCode(c); + break; + case 12: case 13: + // 110x xxxx 10xx xxxx + char2 = array[i++]; + out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); + break; + case 14: + // 1110 xxxx 10xx xxxx 10xx xxxx + char2 = array[i++]; + char3 = array[i++]; + out += String.fromCharCode(((c & 0x0F) << 12) | + ((char2 & 0x3F) << 6) | + ((char3 & 0x3F) << 0)); + break; + default: + break; + } + } + return out; +} |