Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@cnwhy/base64
Advanced tools
Base64
编码
,解码
库;
npm i @cnwhy/base64
Buffer
对像的方法出局)btoa
, atob
只支持 Latin1);Base64
编/解码本该与字符串无关, 但现有库几乎只支持字符串;Tree-shaking
, 项目一般只用需要(encode
或 decode
), 我可不想copy代码;Base64
方案;通用, 对于不支持ArrayBuffer
的环境将会用Array
代替Uint8Array
.
const { encode, decode, createEncode, createDecode } = require('@cnwhy/base64');
// import { encode, decode, createEncode, createDecode } from '@cnwhy/base64';
// 1. 字符串
let str = '中国𐄡美国';
let b64 = encode(str);
console.log(b64);
let _str = decode(b64).toString(); // 必须调用toString()方法还原为字符串.
console.log('string:', str === _str); // true
// 2. 字节数组
// let buffer = fs.readFileSync('./test.js');
let buffer = new Uint8Array([0,255,127,33,0,5]);
let fb64 = encode(buffer); // encode支持 Buffer , Stirng, Array<number>
let fu8arr = decode(fb64); // decode 返回Uint8Array对像
console.log('buffer:', Array.from(buffer).join() == Array.from(fu8arr).join());
// 3. 自定义 Base64 转换方法
// 自定义码表与补位符
const TABLE = 'xQh}s7*y~A|nkj4Bf%z1R,P+)mMS{(&EWCKegp6r!OX</LuY-l9^ZJ#cTU[vHda$';
const PAD = '.';
// 自定义字符串编码/解码方法
const Utf16Encode = function(str) {
let cods = str.split('').map(s => s.charCodeAt(0));
return new Uint8Array(new Uint16Array(cods).buffer);
}
const Utf16Decode = function(arr) {
let u16 = Array.from(new Uint16Array(arr.buffer));
return u16.map(c => String.fromCharCode(c)).join('');
}
//创建自定义转码函数
const myEncode = createEncode(TABLE,PAD,Utf16Encode);
const myDecode = createDecode(TABLE,PAD,Utf16Decode);
console.log('\n自定义转码:');
let myb64 = myEncode(str);
console.log(myb64);
let my_str = myDecode(myb64).toString(); // 调用toString() 会用 Utf16Decode 方法将字节数组转为字符串
console.log('my:',str == my_str);
//output:
/*
5Lit5Zu98JCEoee+juWbvQ==
string: true
buffer: true
自定义转码:
nRvd,W})~(#4E$JP
my: true
*/
更多使用例子可以参看这篇;
Base64 = {
BASE64_TABLE: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
BASE64_URL_TABLE: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
PAD: "=";
// UTF8 编码 解码; 可做为 strEncode strDecode 参数;
utf8Encode(str:string):Uint8Array|number[];
utf8Decode(utf8arr:Uint8Array|number[]):string;
//Base64 编码 解码
encode(input:string|ArrayBuffer|Uint8Array|number[]):string;
decode(base64str: string) => number[]|Uint8Array;
//适用于URL的Base64 编码 解码( "_" "-" 替换 "/" "+");
encodeURL(input:string|ArrayBuffer|Uint8Array|number[]):string;
decodeURL(base64str: string) => number[]|Uint8Array;
//创建自定义Base64 encode , decode 函数
createEncode(strEncode: Function): (input: any) => string;
createEncode(table?: string[] | string, pad?: string, strEncode?: Function): (input: any) => string;
createDecode(strDecode: Function): (base64str: string) => Uint8Array | number[];
createDecode(table?: string[] | string, pad?: string, strDecode?: Function): (base64str: string) => Uint8Array | number[];
}
FAQs
Base64 library, lossless transcoding.
The npm package @cnwhy/base64 receives a total of 78 weekly downloads. As such, @cnwhy/base64 popularity was classified as not popular.
We found that @cnwhy/base64 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.