package mobvista.dmp.utils.common

import mobvista.dmp.common.MobvistaConstant

import java.io.UnsupportedEncodingException
import java.math.BigInteger
import java.security.{MessageDigest, NoSuchAlgorithmException}

/**
 * @package: mobvista.dmp.utils.common
 * @author: wangjf
 * @date: 2021/12/7
 * @time: 4:13 下午
 * @email: jinfeng.wang@mobvista.com
 */
object MD5Util {
  def getMD5Long(str: String): Long = {
    val id_map: Map[String, String] = Map("a" -> "1", "b" -> "2", "c" -> "3", "d" -> "4", "e" -> "5", "f" -> "6", "g" -> "7")
    var strm = ""
    try {
      //  第一步，获取MessageDigest对象，参数为MD5表示这是一个MD5算法
      val md5 = MessageDigest.getInstance("MD5")
      //  第二步跳过，输入源数据，参数类型为byte[]
      //  第三步，计算MD5值
      val array = md5.digest(str.getBytes("UTF-8"))
      //  第四步，结果转换并返回
      val bigInt = new BigInteger(1, array).toString(16).substring(8, 24)
      for (c <- bigInt.toCharArray) {
        if (id_map.contains(String.valueOf(c))) strm += id_map(String.valueOf(c))
        else strm += String.valueOf(c)
      }
    } catch {
      case e@(_: NoSuchAlgorithmException | _: UnsupportedEncodingException) =>
        e.printStackTrace()
    }
    strm.toLong
  }

  def main(args: Array[String]): Unit = {
    val data =
      """
        |{ "@timestamp": "07/Dec/2021:23:13:36 +0800", "@fields": { "remote_addr": "100.122.18.27", "remote_user": "-", "body_bytes_sent": "0", "status": "200", "http_host": "analytics-http-ab.mintegral.net", "request": "POST / HTTP/1.1", "request_method": "POST", "request_body": "platform=1&package_name=com.Upperpik.HairChallenge&os_version=11&brand=samsung&model=SM-T510&gaid=6b4e83cb-b21b-41e4-a187-ed1c2c56197e&network_type=9&network_str=&language=es-ES&timezone=GMT%252B01%253A00&ua=Mozilla%252F5.0%2B%2528Linux%253B%2BAndroid%2B11%253B%2BSM-T510
        |""".stripMargin

    println(MobvistaConstant.String2JSONObject(data))
  }
}
