Browse Source

Minor code tidying in ScramMechanism

wip-ping-timeouts
Chris Smith 5 years ago
parent
commit
562dfbd952
1 changed files with 18 additions and 23 deletions
  1. 18
    23
      src/main/kotlin/com/dmdirc/ktirc/sasl/ScramMechanism.kt

+ 18
- 23
src/main/kotlin/com/dmdirc/ktirc/sasl/ScramMechanism.kt View File

94
         sendAuthenticationMessage("*")
94
         sendAuthenticationMessage("*")
95
     }
95
     }
96
 
96
 
97
-    private fun IrcClient.sendScramMessage(prefix: String = "", vararg entries: Pair<ScramMessageType, String>) =
98
-            sendAuthenticationData("$prefix${buildScramMessage(*entries)}")
99
-
100
-    private fun buildScramMessage(vararg entries: Pair<ScramMessageType, String>) = entries.joinToString(",") { (k, v) -> "${k.prefix}=$v" }
101
-
102
-    private fun ByteArray?.parse(): Map<ScramMessageType, String> {
103
-        return if (this == null || this.isEmpty())
104
-            emptyMap()
105
-        else
106
-            String(this).split(',').map {
107
-                getMessageType(it[0]) to it.substring(2).unescape()
108
-            }.toMap()
109
-    }
97
+    private fun IrcClient.sendScramMessage(prefix: String = "", vararg entries: Pair<ScramMessageType, String>)
98
+            = sendAuthenticationData("$prefix${buildScramMessage(*entries)}")
99
+
100
+    private fun buildScramMessage(vararg entries: Pair<ScramMessageType, String>)
101
+            = entries.joinToString(",") { (k, v) -> "${k.prefix}=$v" }
102
+
103
+    private fun ByteArray?.parse() = if (this == null || this.isEmpty())
104
+        emptyMap()
105
+    else
106
+        String(this).split(',').map {
107
+            getMessageType(it[0]) to it.substring(2).unescape()
108
+        }.toMap()
110
 
109
 
111
     private fun String.escape() = replace("=", "=3D").replace(",", "=2C")
110
     private fun String.escape() = replace("=", "=3D").replace(",", "=2C")
112
     private fun String.unescape() = replace("=2C", ",").replace("=3D", "=")
111
     private fun String.unescape() = replace("=2C", ",").replace("=3D", "=")
113
 
112
 
114
-    private fun hmac(keyMaterial: ByteArray, input: ByteArray): ByteArray {
115
-        return with(Mac.getInstance("hmac${algorithm.replace("-", "")}")) {
116
-            init(SecretKeySpec(keyMaterial, algorithm))
117
-            doFinal(input)
118
-        }
113
+    private fun hmac(keyMaterial: ByteArray, input: ByteArray) = with(Mac.getInstance("hmac${algorithm.replace("-", "")}")) {
114
+        init(SecretKeySpec(keyMaterial, algorithm))
115
+        doFinal(input)
119
     }
116
     }
120
 
117
 
121
-    private fun hash(input: ByteArray): ByteArray {
122
-        return with(MessageDigest.getInstance(algorithm.replace("-", ""))) {
123
-            digest(input)
124
-        }
118
+    private fun hash(input: ByteArray) = with(MessageDigest.getInstance(algorithm.replace("-", ""))) {
119
+        digest(input)
125
     }
120
     }
126
 
121
 
127
     private fun pbkdf2(keyMaterial: ByteArray, initialSalt: ByteArray, iterations: Int): ByteArray {
122
     private fun pbkdf2(keyMaterial: ByteArray, initialSalt: ByteArray, iterations: Int): ByteArray {
150
 private fun newNonce(): String {
145
 private fun newNonce(): String {
151
     val charPool: List<Char> = (' '..'~') - ',' - '='
146
     val charPool: List<Char> = (' '..'~') - ',' - '='
152
     val random = SecureRandom.getInstanceStrong().asKotlinRandom()
147
     val random = SecureRandom.getInstanceStrong().asKotlinRandom()
153
-    return (0..31).map { charPool.random(random) }.joinToString("")
148
+    return CharArray(32) { charPool.random(random) }.joinToString("")
154
 }
149
 }
155
 
150
 
156
 internal class ScramState(
151
 internal class ScramState(

Loading…
Cancel
Save