Skip to content

Commit 168f3f1

Browse files
author
Manish Baxi
committed
Modified the code to generate unique, cryptographically secure tokens for every request.
1 parent f97e756 commit 168f3f1

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package org.example.security
22

3+
import java.security.SecureRandom
4+
35
/**
46
* Generates string tokens.
57
*/
6-
final object TokenGenerator {
8+
object TokenGenerator {
9+
val LENGTH = 32
10+
11+
private[this] val ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
12+
private[this] val RANDOM = new SecureRandom
13+
714
/**
815
* Generates a string token.
916
*/
10-
def generateToken = "ABCD1234"
17+
def generateToken: String = {
18+
val builder = new StringBuilder
19+
20+
for (i <- 1 to LENGTH) {
21+
builder.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length)))
22+
}
23+
24+
builder.toString()
25+
}
1126
}

0 commit comments

Comments
 (0)