123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package com.gxzc.zen.umps.util
- import com.baomidou.kisso.common.encrypt.MD5
- import org.slf4j.LoggerFactory
- import com.baomidou.kisso.SSOConfig
- import com.baomidou.kisso.common.encrypt.Byte2Hex
- import com.sun.xml.internal.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory.getAlgorithm
- import java.security.MessageDigest
- import com.baomidou.kisso.SSOConfig.CUT_SYMBOL
- import java.nio.charset.Charset
- /**
- *
- * @author NorthLan
- * @date 2018/3/5
- * @url https://noahlan.com
- */
- class MD5Salt {
- companion object {
- private val logger = LoggerFactory.getLogger(MD5Salt::class.java)
- /**
- * md5 盐值加密字符串
- * @param salt
- * 盐值
- * @param rawText
- * 需要加密的字符串
- * @return
- */
- fun md5SaltEncode(salt: String, rawText: String): String? {
- return MD5Salt(salt, MD5.ALGORITHM).encode(rawText)
- }
- /**
- * 判断md5 盐值加密内容是否正确
- * @param salt
- * 盐值
- * @param encodeText
- * 加密后的文本内容
- * @param rawText
- * 加密前的文本内容
- * @return
- */
- fun md5SaltValid(salt: String, encodeText: String, rawText: String): Boolean {
- return MD5Salt(salt, MD5.ALGORITHM).isValid(encodeText, rawText)
- }
- }
- private var salt: String? = null
- private var algorithm: String? = null
- private constructor()
- constructor(salt: String, algorithm: String) {
- this.salt = salt
- this.algorithm = algorithm
- }
- /**
- * 字符串盐值加密
- * @param rawText
- * 需要加密的字符串
- * @return
- */
- fun encode(rawText: String): String? {
- try {
- val md = MessageDigest.getInstance(this.algorithm)
- //加密后的字符串
- return Byte2Hex.byte2Hex(md.digest(mergeRawTextAndSalt(rawText).toByteArray(Charset.forName(SSOConfig.getSSOEncoding()))))
- } catch (e: Exception) {
- logger.error(" MD5Salt encode exception.")
- e.printStackTrace()
- }
- return null
- }
- /**
- *
- *
- *
- * 判断加密内容是否正确
- *
- *
- * @param encodeText
- * 加密后的文本内容
- * @param rawText
- * 加密前的文本内容
- * @return
- */
- fun isValid(encodeText: String, rawText: String): Boolean {
- return this.encode(rawText).equals(encodeText)
- }
- /**
- *
- *
- *
- * 合并混淆盐值至加密内容
- *
- *
- * @param rawText
- * 需要加密的字符串
- * @return
- */
- private fun mergeRawTextAndSalt(rawText: String?): String {
- var rawText = rawText
- if (rawText == null) {
- rawText = ""
- }
- return if (this.salt == null || "" == this.salt) {
- rawText
- } else {
- val mt = StringBuffer()
- mt.append(rawText)
- mt.append(SSOConfig.CUT_SYMBOL)
- mt.append(this.salt)
- mt.toString()
- }
- }
- }
|