2020-09-11 14:31:38 +00:00
|
|
|
import ByteBuffer from 'bytebuffer';
|
|
|
|
import SimpleCrypto from 'react-native-simple-crypto';
|
|
|
|
|
|
|
|
import random from '../../utils/random';
|
|
|
|
import { fromByteArray, toByteArray } from '../../utils/base64-js';
|
|
|
|
|
|
|
|
const BASE64URI = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
|
|
|
2022-02-16 21:14:28 +00:00
|
|
|
// @ts-ignore
|
|
|
|
export const b64ToBuffer = (base64: string): ArrayBuffer => toByteArray(base64).buffer;
|
2020-09-11 14:31:38 +00:00
|
|
|
export const utf8ToBuffer = SimpleCrypto.utils.convertUtf8ToArrayBuffer;
|
2022-02-16 21:14:28 +00:00
|
|
|
export const bufferToB64 = (arrayBuffer: ArrayBuffer): string => fromByteArray(new Uint8Array(arrayBuffer));
|
2020-09-11 14:31:38 +00:00
|
|
|
// ArrayBuffer -> Base64 URI Safe
|
|
|
|
// https://github.com/herrjemand/Base64URL-ArrayBuffer/blob/master/lib/base64url-arraybuffer.js
|
2022-02-16 21:14:28 +00:00
|
|
|
export const bufferToB64URI = (buffer: ArrayBuffer): string => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const uintArray = new Uint8Array(buffer);
|
|
|
|
const len = uintArray.length;
|
|
|
|
let base64 = '';
|
|
|
|
|
|
|
|
for (let i = 0; i < len; i += 3) {
|
|
|
|
base64 += BASE64URI[uintArray[i] >> 2];
|
|
|
|
base64 += BASE64URI[((uintArray[i] & 3) << 4) | (uintArray[i + 1] >> 4)];
|
|
|
|
base64 += BASE64URI[((uintArray[i + 1] & 15) << 2) | (uintArray[i + 2] >> 6)];
|
|
|
|
base64 += BASE64URI[uintArray[i + 2] & 63];
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (len % 3 === 2) {
|
2020-09-11 14:31:38 +00:00
|
|
|
base64 = base64.substring(0, base64.length - 1);
|
|
|
|
} else if (len % 3 === 1) {
|
|
|
|
base64 = base64.substring(0, base64.length - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64;
|
|
|
|
};
|
|
|
|
// SimpleCrypto.utils.convertArrayBufferToUtf8 is not working with unicode emoji
|
2022-02-16 21:14:28 +00:00
|
|
|
export const bufferToUtf8 = (buffer: ArrayBuffer): string => {
|
|
|
|
const uintArray = new Uint8Array(buffer) as number[] & Uint8Array;
|
2020-09-11 14:31:38 +00:00
|
|
|
const encodedString = String.fromCharCode.apply(null, uintArray);
|
2022-02-16 21:14:28 +00:00
|
|
|
return decodeURIComponent(escape(encodedString));
|
2020-09-11 14:31:38 +00:00
|
|
|
};
|
2022-02-16 21:14:28 +00:00
|
|
|
export const splitVectorData = (text: ArrayBuffer): ArrayBuffer[] => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const vector = text.slice(0, 16);
|
|
|
|
const data = text.slice(16);
|
|
|
|
return [vector, data];
|
|
|
|
};
|
2022-02-16 21:14:28 +00:00
|
|
|
|
|
|
|
export const joinVectorData = (vector: ArrayBuffer, data: ArrayBuffer): ArrayBufferLike => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const output = new Uint8Array(vector.byteLength + data.byteLength);
|
|
|
|
output.set(new Uint8Array(vector), 0);
|
|
|
|
output.set(new Uint8Array(data), vector.byteLength);
|
|
|
|
return output.buffer;
|
|
|
|
};
|
2022-02-16 21:14:28 +00:00
|
|
|
export const toString = (thing: string | ByteBuffer | Buffer | ArrayBuffer | Uint8Array): string | ByteBuffer => {
|
2020-09-11 14:31:38 +00:00
|
|
|
if (typeof thing === 'string') {
|
|
|
|
return thing;
|
|
|
|
}
|
2022-02-16 21:14:28 +00:00
|
|
|
// @ts-ignore
|
2020-09-11 14:31:38 +00:00
|
|
|
return new ByteBuffer.wrap(thing).toString('binary');
|
|
|
|
};
|
2022-02-16 21:14:28 +00:00
|
|
|
export const randomPassword = (): string => `${random(3)}-${random(3)}-${random(3)}`.toLowerCase();
|