Skip to content

Instantly share code, notes, and snippets.

@chentingjuan
Last active September 23, 2022 11:37
Show Gist options
  • Save chentingjuan/9d0673acaa98f9eaeedc631ed89e86b6 to your computer and use it in GitHub Desktop.
Save chentingjuan/9d0673acaa98f9eaeedc631ed89e86b6 to your computer and use it in GitHub Desktop.
const data = {
// 已登入:使用者 line id
// 未登入:會產 uuid(字元數要和 line id 統一字元數 33 個)
userId: 'U3045e7519d146d551937a1a8532f0857',
// 所有使用者 任務紀錄顏色比例
// R, G, B 加總為 1
// 沒有任務資料時,使用初始狀態 R: 1/3、G: 1/3、B: 1/3
all: {
R: 0.33,
G: 0.46,
B: 0.21
},
// 使用者個人 任務紀錄顏色比例
// R, G, B 加總為 1
// 未登入,或沒有任務資料時,使用初始狀態 R: 1/3、G: 1/3、B: 1/3
user: {
R: 0.08,
G: 0.67,
B: 0.25
},
// 當下在線人數
numberOfPeople: 449,
// 當下時間戳(此項目取消)
// timestamp: 1661872588004,
// 第幾小時
// 0-11
hour: 0
}
@chentingjuan
Copy link
Author

0915_note
若尚無任何任務紀錄,RGB 會以平均值 1/3 呈現

@yiwenl
Copy link

yiwenl commented Sep 15, 2022

我找到一個可以用的encode / decode 程式:

const cipher = (salt) => {
  const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
  const byteHex = (n) => ("0" + Number(n).toString(16)).substr(-2);
  const applySaltToChar = (code) =>
    textToChars(salt).reduce((a, b) => a ^ b, code);

  return (text) =>
    text
      .split("")
      .map(textToChars)
      .map(applySaltToChar)
      .map(byteHex)
      .join("");
};

@yiwenl
Copy link

yiwenl commented Sep 15, 2022

const decipher = (salt) => {
  const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
  const applySaltToChar = (code) =>
    textToChars(salt).reduce((a, b) => a ^ b, code);
  return (encoded) =>
    encoded
      .match(/.{1,2}/g)
      .map((hex) => parseInt(hex, 16))
      .map(applySaltToChar)
      .map((charCode) => String.fromCharCode(charCode))
      .join("");
};

@yiwenl
Copy link

yiwenl commented Sep 15, 2022

const myCipher = cipher("lanuitblanche");
const myDecipher = decipher("lanuitblanche");

const strData = JSON.stringify(data);
console.log(strData);
const encodedHash = myCipher(strData);
console.log("hash", encodedHash);
const decodedData = myDecipher(encodedHash);
console.log(decodedData);
console.log(JSON.parse(decodedData));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment