简易编码本地数据
顿觉了。如来禅。 六度万行体中圆。 梦里明明有六趣。觉后空空无大千。
想要对数据进行简易的本地存储,但不想让人一眼看出的 base64 编码的结构。
核心是将每一位字符的 charCode 加一个偏移量,同时加了一些随机长度的数据进行混淆。
const char = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789/+=";
const setLocStr = (username, password ) => {
console.log("setLocStr");
const genChar = (length, num) => Array.from({ length }, () => char[Math.round(Math.random() * num) % char.length]).join("");
const str1 = btoa(encodeURIComponent([genChar(17, 1e10), btoa(encodeURIComponent(username)), genChar(46, 1e8), btoa(encodeURIComponent(password)), genChar(5, 1e16)].join("#")));
const str2 = str1
.split("")
.map((x) => String.fromCharCode(x.charCodeAt() + 5))
.join("");
console.log(str1, str2);
uni.setStorageSync(
"LOC__INDEX_STR",
JSON.stringify({
t: new Date().getTime(),
s: str2.split("").reverse().join(""),
})
);
};
const getLocStr = () => {
const locStr = uni.getStorageSync("LOC__INDEX_STR");
if (locStr) {
const { t, s } = JSON.parse(locStr);
const now = new Date().getTime();
if (now - t < 1000 *60* 60 *24* 30) {
const str2 = s
.split("")
.reverse()
.map((x) => String.fromCharCode(Math.round(x.charCodeAt() - 5)))
.join("");
const [, u, , p] = decodeURIComponent(atob(str2)).split("#");
if (u) {
username = decodeURIComponent(atob(u));
password = decodeURIComponent(atob(p));
}
}
}
};
getLocStr();