Merge remote-tracking branch 'origin/master'

# Conflicts:
#	utils/src/main/kotlin/cn/tursom/utils/TokenUtil.kt
This commit is contained in:
tursom 2019-10-26 09:15:30 +08:00
commit 75dedbc837

View File

@ -16,16 +16,16 @@ open class TokenUtil {
val digestBase64: String = digest.base64()
}
fun <T> generate(secretKey: String, data: T, timeout: Long = 30 * 60 * 1000, type: DigestType = DigestType.MD5): String {
fun <T : Token> generate(secretKey: String, data: T, type: DigestType = DigestType.MD5): String {
val head = type.digestBase64
val body = toJson(TokenBody(System.currentTimeMillis(), timeout, data)).base64()
val body = toJson(data).base64()
val encryptSource = "$head.$body".toByteArray()
val encrypt = encrypt(secretKey, encryptSource, type.digest)
return "$head.$body.$encrypt"
}
@Throws(TokenException::class)
fun <T : Any> decode(secretKey: String, token: String, dataClazz: Class<T>): T {
fun <T : Token> decode(secretKey: String, token: String, dataClazz: Class<T>): T {
val splitToken = token.split(".")
if (splitToken.size != 3) {
throw WrongTokenSyntaxException()
@ -34,14 +34,14 @@ open class TokenUtil {
if (signature != splitToken[2]) {
throw WrongSignatureException()
}
val decode = fromJson<TokenBody<T>>(splitToken[1].base64decode())
val decode = fromJson(splitToken[1].base64decode(), dataClazz)
if (decode.tim + decode.exp < System.currentTimeMillis()) {
throw TokenTimeoutException()
}
return fromJson(toJson(decode.dat!!), dataClazz)
return decode
}
open fun encrypt(secretKey: String, encryptSource: ByteArray, type: String): String {
protected open fun encrypt(secretKey: String, encryptSource: ByteArray, type: String): String {
val inner = secretKey.toByteArray().digest(type)!!
encryptSource.forEachIndexed { index, _ ->
encryptSource[index] = encryptSource[index] xor inner[index % inner.size]
@ -56,9 +56,10 @@ open class TokenUtil {
open fun toJson(bean: Any): String = gson.toJson(bean)
open fun <T> fromJson(json: String, clazz: Class<T>): T = gson.fromJson(json, clazz)
private inline fun <reified T : Any> fromJson(json: String): T = fromJson(json, T::class.java)
data class TokenBody<T>(val tim: Long = System.currentTimeMillis(), val exp: Long = 0L, val dat: T? = null)
interface Token {
val tim: Long
val exp: Long
}
open class TokenException : Exception()
class WrongTokenSyntaxException : TokenException()
@ -67,6 +68,6 @@ open class TokenUtil {
companion object {
@JvmStatic
val instance = TokenUtil()
val gsonTokenUtil = TokenUtil()
}
}