Skip to content

Commit c895053

Browse files
committed
Add constant-time operation examples
1 parent 78b64ff commit c895053

5 files changed

Lines changed: 134 additions & 1 deletion

File tree

  • library
    • base16/src/commonMain/kotlin/io/matthewnelson/encoding/base16
    • base32/src/commonMain/kotlin/io/matthewnelson/encoding/base32
    • base64/src/commonMain/kotlin/io/matthewnelson/encoding/base64
    • core/src/commonMain/kotlin/io/matthewnelson/encoding/core/util

library/base16/src/commonMain/kotlin/io/matthewnelson/encoding/base16/Builders.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ public class Base16ConfigBuilder {
8484
* (such as private key material).
8585
*
8686
* If false, will not use constant time operations.
87+
*
88+
* e.g. (NOT constant-time operation)
89+
*
90+
* fun String.containsChar(char: Char): Boolean {
91+
* for (c in this) {
92+
* if (c == char) return true
93+
* }
94+
* return false
95+
* }
96+
*
97+
* e.g. (YES constant-time operation)
98+
*
99+
* fun String.containsChar(char: Char): Boolean {
100+
* var result = false
101+
* for (c in this) {
102+
* result = if (c == char) true else result
103+
* }
104+
* return result
105+
* }
87106
* */
88107
@JvmField
89108
public var isConstantTime: Boolean = false

library/base32/src/commonMain/kotlin/io/matthewnelson/encoding/base32/Builders.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ public class Base32CrockfordConfigBuilder {
161161
* (such as private key material).
162162
*
163163
* If false, will not use constant time operations.
164+
*
165+
* e.g. (NOT constant-time operation)
166+
*
167+
* fun String.containsChar(char: Char): Boolean {
168+
* for (c in this) {
169+
* if (c == char) return true
170+
* }
171+
* return false
172+
* }
173+
*
174+
* e.g. (YES constant-time operation)
175+
*
176+
* fun String.containsChar(char: Char): Boolean {
177+
* var result = false
178+
* for (c in this) {
179+
* result = if (c == char) true else result
180+
* }
181+
* return result
182+
* }
164183
* */
165184
@JvmField
166185
public var isConstantTime: Boolean = false
@@ -336,6 +355,25 @@ public class Base32DefaultConfigBuilder {
336355
* (such as private key material).
337356
*
338357
* If false, will not use constant time operations.
358+
*
359+
* e.g. (NOT constant-time operation)
360+
*
361+
* fun String.containsChar(char: Char): Boolean {
362+
* for (c in this) {
363+
* if (c == char) return true
364+
* }
365+
* return false
366+
* }
367+
*
368+
* e.g. (YES constant-time operation)
369+
*
370+
* fun String.containsChar(char: Char): Boolean {
371+
* var result = false
372+
* for (c in this) {
373+
* result = if (c == char) true else result
374+
* }
375+
* return result
376+
* }
339377
* */
340378
@JvmField
341379
public var isConstantTime: Boolean = false
@@ -443,6 +481,25 @@ public class Base32HexConfigBuilder {
443481
* (such as private key material).
444482
*
445483
* If false, will not use constant time operations.
484+
*
485+
* e.g. (NOT constant-time operation)
486+
*
487+
* fun String.containsChar(char: Char): Boolean {
488+
* for (c in this) {
489+
* if (c == char) return true
490+
* }
491+
* return false
492+
* }
493+
*
494+
* e.g. (YES constant-time operation)
495+
*
496+
* fun String.containsChar(char: Char): Boolean {
497+
* var result = false
498+
* for (c in this) {
499+
* result = if (c == char) true else result
500+
* }
501+
* return result
502+
* }
446503
* */
447504
@JvmField
448505
public var isConstantTime: Boolean = false

library/base64/src/commonMain/kotlin/io/matthewnelson/encoding/base64/Builders.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ public class Base64ConfigBuilder {
8181
* (such as private key material).
8282
*
8383
* If false, will not use constant time operations.
84+
*
85+
* e.g. (NOT constant-time operation)
86+
*
87+
* fun String.containsChar(char: Char): Boolean {
88+
* for (c in this) {
89+
* if (c == char) return true
90+
* }
91+
* return false
92+
* }
93+
*
94+
* e.g. (YES constant-time operation)
95+
*
96+
* fun String.containsChar(char: Char): Boolean {
97+
* var result = false
98+
* for (c in this) {
99+
* result = if (c == char) true else result
100+
* }
101+
* return result
102+
* }
84103
* */
85104
@JvmField
86105
public var isConstantTime: Boolean = false

library/core/src/commonMain/kotlin/io/matthewnelson/encoding/core/util/CTCase.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ import kotlin.jvm.JvmField
2828
* **NOTE:** Only characters where [Char.isLetter] is true are taken from
2929
* the provided table.
3030
*
31+
* e.g. (NOT constant-time operation)
32+
*
33+
* fun String.containsChar(char: Char): Boolean {
34+
* for (c in this) {
35+
* if (c == char) return true
36+
* }
37+
* return false
38+
* }
39+
*
40+
* e.g. (YES constant-time operation)
41+
*
42+
* fun String.containsChar(char: Char): Boolean {
43+
* var result = false
44+
* for (c in this) {
45+
* result = if (c == char) true else result
46+
* }
47+
* return result
48+
* }
49+
*
3150
* @param [table] The decoding table (e.g. `ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`)
3251
* @throws [IllegalArgumentException] if table contains a letter that has no
3352
* corresponding lowercase value.

library/core/src/commonMain/kotlin/io/matthewnelson/encoding/core/util/DecoderAction.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ public fun interface DecoderAction {
3636
* with support for constant-time operations (in terms of work performed
3737
* for provided input).
3838
*
39+
* e.g. (NOT constant-time operation)
40+
*
41+
* fun String.containsChar(char: Char): Boolean {
42+
* for (c in this) {
43+
* if (c == char) return true
44+
* }
45+
* return false
46+
* }
47+
*
48+
* e.g. (YES constant-time operation)
49+
*
50+
* fun String.containsChar(char: Char): Boolean {
51+
* var result = false
52+
* for (c in this) {
53+
* result = if (c == char) true else result
54+
* }
55+
* return result
56+
* }
57+
*
3958
* e.g. (Base16)
4059
*
4160
* val parser = DecoderAction.Parser(
@@ -65,7 +84,7 @@ public fun interface DecoderAction {
6584
*
6685
* @param [input] the character to parse [actions] with
6786
* @param [isConstantTime] If `true`, will **not** jump out of
68-
* loops early in the event a hit occurs.
87+
* loops early in the event a match is found.
6988
* @return The result of [DecoderAction.convert], or `null` if no
7089
* match was found.
7190
* */

0 commit comments

Comments
 (0)