mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-31 14:15:14 +00:00
Migrate RandomHelper to Kotlin
This commit is contained in:
committed by
Albert Vaca Cintora
parent
5f18cb571d
commit
69495136da
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Albert Vaca Cintora <albertvaka@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
*/
|
||||
|
||||
package org.kde.kdeconnect.Helpers;
|
||||
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class RandomHelper {
|
||||
public static final SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
private static final char[] symbols = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||||
"abcdefghijklmnopqrstuvwxyz" +
|
||||
"1234567890").toCharArray();
|
||||
|
||||
|
||||
public static String randomString(int length) {
|
||||
char[] buffer = new char[length];
|
||||
for (int idx = 0; idx < length; ++idx) {
|
||||
buffer[idx] = symbols[secureRandom.nextInt(symbols.length)];
|
||||
}
|
||||
return new String(buffer);
|
||||
}
|
||||
|
||||
}
|
22
src/org/kde/kdeconnect/Helpers/RandomHelper.kt
Normal file
22
src/org/kde/kdeconnect/Helpers/RandomHelper.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 Albert Vaca Cintora <albertvaka@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
*/
|
||||
package org.kde.kdeconnect.Helpers
|
||||
|
||||
import java.security.SecureRandom
|
||||
|
||||
object RandomHelper {
|
||||
@JvmField
|
||||
val secureRandom: SecureRandom = SecureRandom()
|
||||
|
||||
private val symbols = (('A'..'Z').toList() + ('a'..'z').toList() + ('0'..'9').toList()).toCharArray()
|
||||
fun randomString(length: Int): String {
|
||||
val buffer = CharArray(length)
|
||||
for (i in 0 until length) {
|
||||
buffer[i] = symbols[secureRandom.nextInt(symbols.size)]
|
||||
}
|
||||
return String(buffer)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user