소스 검색

Add Base64 encode/decode functions and use them where needed (#793)

Lennart Grahl 6 년 전
부모
커밋
1554e5ca4e
2개의 변경된 파일19개의 추가작업 그리고 11개의 파일을 삭제
  1. 17 9
      src/helpers.ts
  2. 2 2
      src/services/qrcode.ts

+ 17 - 9
src/helpers.ts

@@ -60,6 +60,20 @@ export function hexToU8a(hexstring: string): Uint8Array {
     return array;
 }
 
+/**
+ * Convert an Uint8Array to a base 64 string.
+ */
+export function u8aToBase64(array: Uint8Array): string {
+    return btoa(Array.from(array, (byte: number) => String.fromCharCode(byte)).join(''));
+}
+
+/**
+ * Convert a base 64 string to an Uint8Array.
+ */
+export function base64ToU8a(base64String: string): Uint8Array {
+    return Uint8Array.from(atob(base64String), (char: string) => char.charCodeAt(0));
+}
+
 /**
  * Generate a (non-cryptographically-secure!) random string.
  *
@@ -259,8 +273,8 @@ export function escapeRegExp(str: string) {
  * Generate a link to the msgpack visualizer from an Uint8Array containing
  * msgpack encoded data.
  */
-export function msgpackVisualizer(bytes: Uint8Array): string {
-    return 'https://msgpack.dbrgn.ch#base64=' + encodeURIComponent(btoa(String.fromCharCode.apply(null, bytes)));
+export function msgpackVisualizer(array: Uint8Array): string {
+    return 'https://msgpack.dbrgn.ch#base64=' + encodeURIComponent(u8aToBase64(array));
 }
 
 /**
@@ -290,12 +304,6 @@ export function bufferToUrl(buffer: ArrayBuffer, mimeType: string, logWarning: (
     if (buffer === null || buffer === undefined) {
         throw new Error('Called bufferToUrl on null or undefined');
     }
-    let binary = '';
-    const bytes = new Uint8Array(buffer);
-    const len = bytes.byteLength;
-    for (let i = 0; i < len; i++) {
-        binary += String.fromCharCode(bytes[i]);
-    }
     switch (mimeType) {
         case 'image/jpg':
         case 'image/jpeg':
@@ -313,7 +321,7 @@ export function bufferToUrl(buffer: ArrayBuffer, mimeType: string, logWarning: (
             mimeType = 'image/jpeg';
             break;
     }
-    return 'data:' + mimeType + ';base64,' + btoa(binary);
+    return 'data:' + mimeType + ';base64,' + u8aToBase64(new Uint8Array(buffer));
 }
 
 /**

+ 2 - 2
src/services/qrcode.ts

@@ -15,7 +15,7 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
-import {stringToUtf8a} from '../helpers';
+import {stringToUtf8a, u8aToBase64} from '../helpers';
 
 /**
  * Functionality related to the initial QR code.
@@ -70,7 +70,7 @@ export class QrCodeService {
         u8Array.set(saltyRtcHostBytes, 101);
 
         // Base64 encode
-        return btoa(String.fromCharCode.apply(null, new Uint8Array(buf)));
+        return u8aToBase64(new Uint8Array(buf));
 
         // tslint:enable:no-bitwise
     }