首页 > 其他分享 >TypeScript编码解码Base64

TypeScript编码解码Base64

时间:2023-09-17 16:56:52浏览次数:37  
标签:TypeScript keyStr string 解码 Base64 utftext let ._ fromCharCode

const Base64 = {     _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',     _utf8_encode: function(string:string) {         string = string.replace(/\r\n/g, '\n')         let utftext = ''         for (let n = 0; n < string.length; n++) {             const c = string.charCodeAt(n)             if (c < 128) {                 utftext += String.fromCharCode(c)             } else if ((c > 127) && (c < 2048)) {                 utftext += String.fromCharCode((c >> 6) | 192)                 utftext += String.fromCharCode((c & 63) | 128)             } else {                 utftext += String.fromCharCode((c >> 12) | 224)                 utftext += String.fromCharCode(((c >> 6) & 63) | 128)                 utftext += String.fromCharCode((c & 63) | 128)             }         }         return utftext     },     _utf8_decode: function(utftext:string) {         let string = ''         let i = 0         let c = 0         let c1 = 0         let c2 = 0         while (i < utftext.length) {             c = utftext.charCodeAt(i)             if (c < 128) {                 string += String.fromCharCode(c)                 i++             } else if ((c > 191) && (c < 224)) {                 c1 = utftext.charCodeAt(i + 1)                 string += String.fromCharCode(((c & 31) << 6) | (c1 & 63))                 i += 2             } else {                 c1 = utftext.charCodeAt(i + 1)                 c2 = utftext.charCodeAt(i + 2)                 string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63))                 i += 3             }         }         return string     },     encode: function(input:string) {         let output = ''         let chr1, chr2, chr3, enc1, enc2, enc3, enc4         let i = 0         input = this._utf8_encode(input)         while (i < input.length) {             chr1 = input.charCodeAt(i++)             chr2 = input.charCodeAt(i++)             chr3 = input.charCodeAt(i++)             enc1 = chr1 >> 2             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)             enc4 = chr3 & 63             if (isNaN(chr2)) {                 enc3 = enc4 = 64             } else if (isNaN(chr3)) {                 enc4 = 64             }             output = output +             this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +             this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4)         }         return output     },     decode: function(input:string) {         let output = ''         let chr1, chr2, chr3         let enc1, enc2, enc3, enc4         let i = 0         // eslint-disable-next-line no-useless-escape         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')         while (i < input.length) {             enc1 = this._keyStr.indexOf(input.charAt(i++))             enc2 = this._keyStr.indexOf(input.charAt(i++))             enc3 = this._keyStr.indexOf(input.charAt(i++))             enc4 = this._keyStr.indexOf(input.charAt(i++))             chr1 = (enc1 << 2) | (enc2 >> 4)             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)             chr3 = ((enc3 & 3) << 6) | enc4             output = output + String.fromCharCode(chr1)             if (enc3 !== 64) {                 output = output + String.fromCharCode(chr2)             }             if (enc4 !== 64) {                 output = output + String.fromCharCode(chr3)             }         }         output = this._utf8_decode(output)         return output     } }
const base64 = {     encode: Base64.encode.bind(Base64),     decode: Base64.decode.bind(Base64) }
export default base64

标签:TypeScript,keyStr,string,解码,Base64,utftext,let,._,fromCharCode
From: https://www.cnblogs.com/jiangyuhu/p/17709122.html

相关文章

  • 【前端攻略】:玩转图片Base64编码
    【前端攻略】:玩转图片Base64编码    引言图片处理在前端工作中可谓占据了很重要的一壁江山。而图片的base64编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的base64编码。标题略大,不过只是希望通过一些浅显的论述,让你知道什么是图片的base64......
  • Qt和ffmpeg结合制作全能解码播放器
    #include<QCoreApplication>#include<QApplication>#include<QWidget>#include<QVBoxLayout>#include<QVideoWidget>#include<QAudioOutput>#include<QDebug>extern"C"{#include<libavformat/avfor......
  • typescript使用记录
    一、第三方包没有声明文件时:https://blog.csdn.net/xiebaochun/article/details/122458063 解决方法1:从 @types/上下载对应的包解决方法2:自己写一个声明文件(.d.ts文件)在项目根目录新建types文件夹。在tsconfig.json里的include添加上types在typings文件夹......
  • 在nodejs中使用Typescript 金字教程
    1.首先新建一个项目npminit-yes2.开启Typescript依赖npminstalltypescript--save-dev安装typescript,现在我们可以通过命令行来使用tsc命令3.安装nodejs类型npminstall@types/node--save-dev4.使用命令创建一个tsconfig.json文件npxtsc--init--rootDir......
  • js:上传图片,通过file对象或blob对象获取用于显示的url,获取图片的base64编码;限制文件类
    1//URL.createObjectUrl(file):同步执行;创建一个指向file/blob对象的url,可用于元素的src/href属性23//fileReader.readAsDataURL():异步执行;读取file/blob对象的内容,result属性将返回一个包含data:URL格式的base64字符串45//fileReader.readAsDataURL()是异步的,所......
  • 七牛云存储____图片转Base64串上传
    packagecom.scdn.qnscdn;/***七牛常理配置**@authorGod待整理好配到config文件里*ACCESSKEY这是我个人申请的一个测试的号everyonecanuseingit*SECRETKEY*/publicclassQnConstant{ //设置好账号的ACCESS_KEY和SECRET_KEY publicstaticfinalS......
  • java安全架构____java Base64加密
    packagecom.security.base64;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;/***@infojavabase64最简单的理解和使用*@authorGodbase64算法是java最基础的加密算法将字符串类型密码转为加密后的字符串理解原理后可自己配置算法规则到更坚固的......
  • 【chatgpt】base64计算快 还是md5计算快
    在计算速度方面,Base64和MD5有不同的应用场景。Base64是一种编码算法,用于将二进制数据转换为文本格式,常用于在网络传输中传递二进制数据。Base64的计算速度通常很快,因为它只需要进行简单的位运算和查表操作,没有复杂的运算过程。MD5是一种哈希算法,用于计算数据的哈希值。它将任意......
  • 技术解码 | GB28181/SIP/SDP 协议--EasyGBS国标GB28181平台国标视频技术SIP解析
    EasyGBS国标视频云服务是基于国标GB/T28181协议的视频能力平台,可实现的视频功能包括:实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。平台部署简单、可拓展性强,支持将接入的视频流进行全终端、全平台分发,分发的视频流包括RTSP、RTMP、FLV、HLS、WebRTC等......
  • DOMPurify 使用方法,如何安全地操作DOM |.sanitize()|.innerHTML|TypeScript TS
    DOMPurify是一个仅限DOM的,超快速的,超级宽容的XSS清理器,用于HTML,MathML和SVG。它也非常简单易用和入门。DOMPurify于2014年2月启动,同时已达到v3.0.5版本。DOMPurify是用JavaScript编写的,适用于所有现代浏览器(Safari(10+),Opera(15+),Edge,Firefox和Chrome-以及几乎所有使用Blink,Gecko......