All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.xml.guard.model.CustomStringMapping.kt Maven / Gradle / Ivy

package com.xml.guard.model

import com.xml.guard.entensions.GuardExtension
import com.xml.guard.utils.generateNumbers
import com.xml.guard.utils.inPackageNameBlackList
import com.xml.guard.utils.randomizeCase
import com.xml.guard.utils.to26Long
import com.xml.guard.utils.toLetterStr
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.random.Random

class CustomStringMapping(private val guardExtension: GuardExtension) {
    private val customStringMap by lazy { guardExtension.customStringMap }

    internal val customStringMapping by lazy { mutableMapOf() }

    // 混淆名索引
    internal var obfuscateIndex = -1L

    fun obfuscateCustomString(): Map {
        val obfuscateMap = mutableMapOf()
        customStringMap.forEach { (rawName, onlyLowercase) ->
            val obfuscateName = obfuscateCustomString(rawName, onlyLowercase)
            obfuscateMap[rawName] = obfuscateName
        }
        return obfuscateMap
    }

    fun writeMappingToFile(mappingFile: File) {
        val writer = BufferedWriter(FileWriter(mappingFile, false))

        for ((key, value) in customStringMapping) {
            writer.write(String.format("%s -> %s\n", key, value))
        }
        writer.flush()
        writer.close()
    }

    private fun obfuscateCustomString(rawName: String, onlyLowercase: Boolean): String {
        var obfuscateName = customStringMapping[rawName]
        if (obfuscateName == null) {
            obfuscateName = generateObfuscateName(onlyLowercase)

            val len = (Random.nextDouble() * 3).roundToInt()
            for (i in 0 until len) {
                val index = (generateNumbers().toDouble()
                    .pow(Random.nextDouble(1.0, 3.0))).toLong()
                var randomName = index.toLetterStr()
                if (!onlyLowercase) {
                    randomName = randomName.randomizeCase()
                }
                obfuscateName += "_${randomName}"
            }
            customStringMapping[rawName] = obfuscateName
        }
        return obfuscateName
    }

    private fun generateObfuscateName(onlyLowercase: Boolean): String {
        while (true) {
            obfuscateIndex += (generateNumbers().toDouble()
                .pow(Random.nextDouble(1.0, 3.0))).toLong()
            var obfuscateIdName = obfuscateIndex.toLetterStr()
            if (!onlyLowercase) {
                obfuscateIdName = obfuscateIdName.randomizeCase()
            }
            if (!obfuscateIdName.inPackageNameBlackList()) { // 过滤黑名单
                val minimumValue = obfuscateIdName.to26Long()
                obfuscateIndex = obfuscateIndex.coerceAtLeast(minimumValue)
                return obfuscateIdName
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy