export function cryptoPassword(password) {
  const Salt = "6%dkszcU*&lk@#$T#(lknkj.359";

  const encryptedPassword = calcEncryptedPassword(password);

  return encryptedPassword;

  function calcEncryptedPassword(password) {
    if (!password || password.length <= 7) {
      console.log("Invalid input");
      return;
    }

    const payload = password + Salt;
    const len = payload.length;

    const a1 = payload.substring(0, 3);
    const a2 = payload.substring(3, len - 3);
    const a3 = payload.substring(len - 3);

    const crypto = require("crypto");
    const md5Hash = crypto.createHash("md5");
    md5Hash.update(a1 + a3 + a2);
    const encryptedPassword = md5Hash.digest("hex");

    return encryptedPassword;
  }
}

export function generateSignature(body) {
  const Salt = "6%dkszcU*&lk@#$T#(lknkj.359";

  const jsonBody = JSON.stringify(body);

  const signature = calcSignature(jsonBody);

  return signature;

  function calcSignature(s) {
    if (!s || s.length <= 20) {
      console.log("Invalid input");
      return;
    }

    const payload = s + Salt;
    const len = payload.length;

    const a1 = payload.substring(0, 10);
    const a2 = payload.substring(10, len - 10);
    const a3 = payload.substring(len - 10);

    const crypto = require("crypto");
    const md5Hash = crypto.createHash("md5");
    md5Hash.update(a1 + a3 + a2);
    const signature = md5Hash.digest("hex");

    return signature;
  }
}