• nodejs 不支持 atob、btoa解决方案(Base64加解密、base64与uint8array转换)


    浏览器等含有window对象处理base64的转换

    // uint8array转为base64字符串
    const uint8arrayToBase64 = function(u8Arr) {
         try{
              let CHUNK_SIZE = 0x8000; //arbitrary number
              let index = 0;
              let length = u8Arr.length;
              let result = '';
              let slice;
              while (index < length) {
                  slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length));
                  result += String.fromCharCode.apply(null, slice);
                  index += CHUNK_SIZE;
              }
              // web image base64图片格式: "data:image/png;base64," + b64encoded;
              // return  "data:image/png;base64," + btoa(result);
              return btoa(result);
          }
          catch(e) {
              throw e;
          }
      }
    // base64字符串转为uint8array数组
    const base64ToUint8Array = function(base64String) {
         try{
             let padding = '='.repeat((4 - base64String.length % 4) % 4);
             let base64 = (base64String + padding)
                 .replace(/\-/g, '+')
                 .replace(/_/g, '/');
             let rawData = atob(base64);
             let outputArray = new Uint8Array(rawData.length);
             for (let i = 0; i < rawData.length; ++i) {
                 outputArray[i] = rawData.charCodeAt(i);
             }
             return outputArray;
         }
         catch(e) {
             throw e;
         }
     }
    // 判断是否json
    const isJson = function(_str){
        if (typeof _str === 'string') {
            try {
                JSON.parse(_str);
                return true;
            } catch(e) {}
        }
        return false;
    }
     // base64加密
    const base64Encode = function(rawStr){
        if (typeof rawStr === 'undefined') {
            return null;
        }
        let result;
        try{
            result = btoa(unescape(encodeURIComponent(JSON.stringify(rawStr))));
        }
        catch(e) {
            throw e;
        }
        return result;
    }
    // base64解密
    const base64Decode = function(base64Str){
        if (base64Str) {
            // 解密可能存在错误,所以需要try
            try{
                let parsedStr = decodeURIComponent(escape(atob(base64Str)));
                if (isJson(parsedStr)) {
                    // 有的字符串不可以json恢复
                    parsedStr = JSON.parse(parsedStr);
                }
                return parsedStr;
            } catch (e) {
    
            }
        }
        return null;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    nodejs处理base64的转换

    // uint8array转base64
    const uint8arrayToBase64 = (value) => {
      // 必须定义 binary 二进制
      return Buffer.from(value, 'binary').toString('base64');
    }
    
    const base64ToUint8array = (value) => {
      value = Buffer.from(value, 'base64').toString('binary');
      let len = value.length;
      let outputArray = new Uint8Array(len);
      for (let i = 0; i < len; ++i) {
        outputArray[i] = value.charCodeAt(i);
      }
      return outputArray;
    }
    
    // 判断是否json
    const isJson = function(_str){
        if (typeof _str === 'string') {
            try {
                JSON.parse(_str);
                return true;
            } catch(e) {}
        }
        return false;
    }
    
    // base64加密
    const base64Encode = (value) => {
      if (typeof value === 'undefined') {
        return null;
      }
      return new Buffer(JSON.stringify(value), 'binary').toString('base64');
    }
    
    // base64解密
    const base64Decode = (value) => {
      value = new Buffer(value, 'base64').toString('binary');
      if (isJson(value)) {
        value = JSON.parse(value);
      }
      return value;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    第十四届蓝桥杯省赛真题 Java A 组【原卷】
    数据结构---串(整个部分)
    CUDA 从入门到放弃(一)
    Codeforces-1696 D: Permutation Graph【构造、分治、数据结构】
    git仓库迁移
    Qt中音频的使用
    复杂逻辑的开发利器—Mendix快速实现AQL质量抽检
    【Proteus仿真】【STM32单片机】基于单片机的智能晾衣架控制系统
    Unity3D教程:实现房产项目中的材质动态切换
    高防IP:构建网络安全的重要防线
  • 原文地址:https://blog.csdn.net/qq_35606400/article/details/125629074