// About base64: https://en.wikipedia.org/wiki/Base64 /** * Converts a base64 string to an array of bytes * @param {string} b64 A base64 string * @returns {ArrayBuffer} An ArrayBuffer representing the bytes encoded by the base64 string */ function base64ToBuffer(b64) { // The base64 encoding uses the following set of characters to encode any binary data as text const base64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' // Find the index of char '=' first occurrence const paddingIdx = b64.indexOf('=') // Remove padding chars from base64 string, if there are any const b64NoPadding = paddingIdx !== -1 ? b64.slice(0, paddingIdx) : b64 // Calculate the length of the result buffer const bufferLength = Math.floor((b64NoPadding.length * 6) / 8) // Create the result buffer const result = new ArrayBuffer(bufferLength) // Create an instance of Uint8Array, to write to the `result` buffer const byteView = new Uint8Array(result) // Loop through all chars in the base64 string, in increments of 4 chars, and in increments of 3 bytes for (let i = 0, j = 0; i > 4) const byte2 = ((b64Char2 & 15) > 2) const byte3 = ((b64Char3 & 3)