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

com.ecwid.apiclient.v3.util.MaskUtils.kt Maven / Gradle / Ivy

There is a newer version: 0.347.0
Show newest version
package com.ecwid.apiclient.v3.util

import kotlin.math.max
import kotlin.math.min

private const val FULL_MASKING_THRESHOLD = 4

data class SecurePattern(
	val regex: Regex,
	val unmaskedLength: Int
)

fun String.maskLogString(securePatterns: List): String {
	var result = this
	for ((regex, unmaskedLength) in securePatterns) {
		result = result.replace(regex) { matchResult ->
			val patternValue = matchResult.groupValues[0]
			val secureValue = matchResult.groupValues[1]
			val maskedSecureValue = secureValue.maskSensitive(unmaskedLength)
			return@replace patternValue.replace(secureValue, maskedSecureValue)
		}
	}
	return result
}

fun String?.maskSensitive(unmaskedLength: Int): String {
	if (this == null) {
		return ""
	}

	if (length - unmaskedLength < FULL_MASKING_THRESHOLD) {
		return "***"
	}

	var maskLength = length - min(unmaskedLength, length)
	maskLength = max(maskLength, length / 2)

	val maskedFirst = (length - maskLength) / 2
	val maskedLast = maskedFirst + maskLength
	val prefix = substring(0, maskedFirst)
	val suffix = substring(maskedLast)
	return "$prefix***$suffix"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy