MD5Salt.kt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.gxzc.zen.umps.util
  2. import com.baomidou.kisso.common.encrypt.MD5
  3. import org.slf4j.LoggerFactory
  4. import com.baomidou.kisso.SSOConfig
  5. import com.baomidou.kisso.common.encrypt.Byte2Hex
  6. import com.sun.xml.internal.fastinfoset.algorithm.BuiltInEncodingAlgorithmFactory.getAlgorithm
  7. import java.security.MessageDigest
  8. import com.baomidou.kisso.SSOConfig.CUT_SYMBOL
  9. import java.nio.charset.Charset
  10. /**
  11. *
  12. * @author NorthLan
  13. * @date 2018/3/5
  14. * @url https://noahlan.com
  15. */
  16. class MD5Salt {
  17. companion object {
  18. private val logger = LoggerFactory.getLogger(MD5Salt::class.java)
  19. /**
  20. * md5 盐值加密字符串
  21. * @param salt
  22. * 盐值
  23. * @param rawText
  24. * 需要加密的字符串
  25. * @return
  26. */
  27. fun md5SaltEncode(salt: String, rawText: String): String? {
  28. return MD5Salt(salt, MD5.ALGORITHM).encode(rawText)
  29. }
  30. /**
  31. * 判断md5 盐值加密内容是否正确
  32. * @param salt
  33. * 盐值
  34. * @param encodeText
  35. * 加密后的文本内容
  36. * @param rawText
  37. * 加密前的文本内容
  38. * @return
  39. */
  40. fun md5SaltValid(salt: String, encodeText: String, rawText: String): Boolean {
  41. return MD5Salt(salt, MD5.ALGORITHM).isValid(encodeText, rawText)
  42. }
  43. }
  44. private var salt: String? = null
  45. private var algorithm: String? = null
  46. private constructor()
  47. constructor(salt: String, algorithm: String) {
  48. this.salt = salt
  49. this.algorithm = algorithm
  50. }
  51. /**
  52. * 字符串盐值加密
  53. * @param rawText
  54. * 需要加密的字符串
  55. * @return
  56. */
  57. fun encode(rawText: String): String? {
  58. try {
  59. val md = MessageDigest.getInstance(this.algorithm)
  60. //加密后的字符串
  61. return Byte2Hex.byte2Hex(md.digest(mergeRawTextAndSalt(rawText).toByteArray(Charset.forName(SSOConfig.getSSOEncoding()))))
  62. } catch (e: Exception) {
  63. logger.error(" MD5Salt encode exception.")
  64. e.printStackTrace()
  65. }
  66. return null
  67. }
  68. /**
  69. *
  70. *
  71. *
  72. * 判断加密内容是否正确
  73. *
  74. *
  75. * @param encodeText
  76. * 加密后的文本内容
  77. * @param rawText
  78. * 加密前的文本内容
  79. * @return
  80. */
  81. fun isValid(encodeText: String, rawText: String): Boolean {
  82. return this.encode(rawText).equals(encodeText)
  83. }
  84. /**
  85. *
  86. *
  87. *
  88. * 合并混淆盐值至加密内容
  89. *
  90. *
  91. * @param rawText
  92. * 需要加密的字符串
  93. * @return
  94. */
  95. private fun mergeRawTextAndSalt(rawText: String?): String {
  96. var rawText = rawText
  97. if (rawText == null) {
  98. rawText = ""
  99. }
  100. return if (this.salt == null || "" == this.salt) {
  101. rawText
  102. } else {
  103. val mt = StringBuffer()
  104. mt.append(rawText)
  105. mt.append(SSOConfig.CUT_SYMBOL)
  106. mt.append(this.salt)
  107. mt.toString()
  108. }
  109. }
  110. }