diff --git a/src/util.js b/src/util.js --- a/src/util.js +++ b/src/util.js @@ -96,3 +96,45 @@ export function utf82str(arr){ } return res.map(x=>String.fromCodePoint(x)).join(""); } + +const mapping="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""); +const remapping=new Array(128); +mapping.forEach((c,i)=>{remapping[c.charCodeAt(0)]=i;}); + +export function bytes2base64(byteArr){ + let arr=byteArr.concat(); + let out=[]; + let rem=(3-arr.length%3)%3; + for(let i=0;i>>2&63]); + out.push(mapping[((arr[i]&3)<<4)+(arr[i+1]>>>4&15)]); + out.push(mapping[((arr[i+1]&15)<<2)+(arr[i+2]>>>6&3)]); + out.push(mapping[arr[i+2]&63]); + } + + for(let i=0;i>4&3)); + out.push(((b2&15)<<4)+(b3>>2&15)); + out.push(((b3&3)<<6)+b4); + } + + for(let i=1; i<3&&str[str.length-i]=="="; i++){out.pop();} + + return out; +}