Created
August 24, 2024 13:56
-
-
Save ArrayIterator/759f4f8e4f805eec6a32c0ea09464bbd to your computer and use it in GitHub Desktop.
Fix base64 encode (btoa incompatibility)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fix base64 encode for javascript | |
*/ | |
const base64_encode = (param) => { | |
const convert_binary_str = (str) => { | |
if (str.length < 1) { | |
return ''; | |
} | |
let ord = str[0].charCodeAt(0); | |
if (ord < 0) { | |
ord = 0xFFFFFFFF + ord + 1; | |
} | |
let tmp = ord.toString(2); | |
tmp = "0".repeat(8 - tmp.length) + tmp; | |
return tmp + convert_binary_str(str.substring(1)); | |
}; | |
param = convert_binary_str(param); | |
let final = ""; | |
let start = 0; | |
let tmp; | |
while (start < param.length) { | |
if (param.substring(start).length < 6) { | |
param += "0".repeat(6 - param.substring(start).length); | |
} | |
let bin = param.slice(start, start + 6); | |
tmp = parseInt(bin.replace(/[^01]/gi, ''), 2); | |
if (tmp < 26) { | |
final += String.fromCharCode(tmp + 65); | |
} | |
else if (tmp > 25 && tmp < 52) { | |
final += String.fromCharCode(tmp + 71); | |
} | |
else if (tmp === 62) { | |
final += "+"; | |
} | |
else if (tmp === 63) { | |
final += "/"; | |
} | |
else if (!tmp) { | |
final += "A"; | |
} | |
else { | |
final += String.fromCharCode(tmp - 4); | |
} | |
start += 6; | |
} | |
if ((final.length % 4) > 0) { | |
final += "=".repeat(4 - final.length % 4); | |
} | |
return final; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fix base64 encode for javascript | |
*/ | |
const base64_encode = (param: any): string => { | |
const convert_binary_str = (str: string): string => { | |
if (str.length < 1) { | |
return ''; | |
} | |
let ord: number = str[0].charCodeAt(0); | |
if (ord < 0) { | |
ord = 0xFFFFFFFF + ord + 1; | |
} | |
let tmp = ord.toString(2); | |
tmp = "0".repeat(8 - tmp.length) + tmp; | |
return tmp + convert_binary_str(str.substring(1)); | |
} | |
param = convert_binary_str(param); | |
let final = ""; | |
let start = 0; | |
let tmp: number; | |
while (start < param.length) { | |
if (param.substring(start).length < 6) { | |
param += "0".repeat(6 - param.substring(start).length); | |
} | |
let bin = (param as string).slice(start, start + 6); | |
tmp = parseInt(bin.replace(/[^01]/gi, ''), 2); | |
if (tmp < 26) { | |
final += String.fromCharCode(tmp + 65); | |
} else if (tmp > 25 && tmp < 52) { | |
final += String.fromCharCode(tmp + 71); | |
} else if (tmp === 62) { | |
final += "+"; | |
} else if (tmp === 63) { | |
final += "/"; | |
} else if (!tmp) { | |
final += "A"; | |
} else { | |
final += String.fromCharCode(tmp - 4); | |
} | |
start += 6; | |
} | |
if ((final.length % 4) > 0) { | |
final += "=".repeat(4 - final.length % 4); | |
} | |
return final; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment