deadmanswitch-encryption
Version:
Cross-platform encryption library for React Native and React web applications with password-based encryption
34 lines (33 loc) • 1.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.arrayBufferToBase64 = arrayBufferToBase64;
exports.base64ToArrayBuffer = base64ToArrayBuffer;
exports.generateRandomBytes = generateRandomBytes;
function arrayBufferToBase64(buffer) {
const bytes = new Uint8Array(buffer);
let binary = '';
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function base64ToArrayBuffer(base64) {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes.buffer;
}
function generateRandomBytes(length) {
const array = new Uint8Array(length);
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
crypto.getRandomValues(array);
}
else {
for (let i = 0; i < length; i++) {
array[i] = Math.floor(Math.random() * 256);
}
}
return array;
}
;