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

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

There is a newer version: 3.0.5
Show newest version
package com.xml.guard.model

import com.xml.guard.entensions.GuardExtension
import com.xml.guard.utils.getRandomString
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter

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

    internal val customStringMapping by lazy { mutableMapOf() }

    /**
     * 混淆自定义字符
     */
    fun obfuscateCustomString(): Map {
        return customStringMap.mapValues { (rawName, isLowercase) ->
            obfuscateCustomString(rawName, isLowercase)
        }
    }

    /**
     * 将映射写入文件
     */
    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, isLowercase: Boolean): String {
        var obfuscateName = customStringMapping[rawName]
        if (obfuscateName == null) {
            val value = MappingParser.checkObfuscate(rawName, isLowercase = isLowercase)
            obfuscateName = value.ifBlank {
                getRandomString {
                    val result = if (isLowercase) it.lowercase() else it
                    MappingParser.verifyObfuscateName(result)
                }
            }
            customStringMapping[rawName] = obfuscateName
        }
        return obfuscateName
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy