This commit is contained in:
2026-06-21 10:41:28 +08:00
parent e00f8ad7a1
commit 60ec0e2883
38 changed files with 23987 additions and 614 deletions
@@ -16,73 +16,292 @@
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile RandomBytes.kt
* LastUpdate 2026-01-28 10:47:13
* LastUpdate 2026-03-27 14:18:30
* UpdateUser MingLiPro
*/
@file:JvmName("RandomBytes")
package com.mingliqiye.utils.random
import java.security.SecureRandom
import kotlin.random.Random
import kotlin.random.nextInt
import kotlin.random.nextLong
/**
* 生成指定长度的随机字节数组
* @param length 数组长度
* @return 包含随机字节的数组
* Random对象 - 提供各种随机数据生成方法
* 使用SecureRandom确保密码学安全,同时提供便捷的随机数生成接口
*/
fun randomBytes(length: Int): ByteArray {
val bytes = ByteArray(length)
for (i in 0..<length) {
bytes[i] = randomByte(0x00.toByte(), 0xff.toByte())
object Random {
/**
* 安全随机数生成器 - 使用系统强随机算法
* 通过lazy委托延迟初始化,提高性能
*/
private val secureRandom: SecureRandom by lazy {
SecureRandom.getInstanceStrong()
}
return bytes
}
/**
* 生成指定长度的随机字节数组
* 从给定的字节数组中随机选择字节来填充新数组
*
* @param length 要生成的随机字节数组的长度
* @param bytes 用于随机选择的源字节数组
* @return 包含随机字节的新数组
*/
fun randomBytes(length: Int, bytes: ByteArray): ByteArray {
val rbytes = ByteArray(length)
// 从源数组中随机选择字节填充到结果数组中
for (i in 0..<length) {
rbytes[i] = bytes[randomInt(i, bytes.size - 1)]
/**
* 标准随机数生成器 - Kotlin标准库提供的随机数生成器
* 通过lazy委托延迟初始化
*/
private val random: Random by lazy {
Random
}
return rbytes
}
/**
* 生成指定范围内的随机字节
* @param from 起始字节值(包含)
* @param to 结束字节值(包含)
* @return 指定范围内的随机字节
*/
fun randomByte(from: Byte, to: Byte): Byte {
val fromInt = from.toInt() and 0xFF
val toInt = to.toInt() and 0xFF
val randomValue = randomInt(fromInt, toInt)
return (randomValue and 0xFF).toByte()
}
/**
* 生成指定长度的随机字节数组
* @param size 要生成的字节数组长度
* @return 包含随机字节的ByteArray
*/
@JvmStatic
fun nextBytes(size: Int): ByteArray = ByteArray(size).also { secureRandom.nextBytes(it) }
/**
* 生成指定范围内的随机字节(不包含边界值)
* @param from 起始字节值(不包含)
* @param to 结束字节值(不包含)
* @return 指定范围内的随机字节
*/
fun randomByteNoHave(from: Byte, to: Byte): Byte {
val fromInt = from.toInt() and 0xFF
val toInt = to.toInt() and 0xFF
val randomValue = randomIntNoHave(fromInt, toInt)
return (randomValue and 0xFF).toByte()
}
/**
* 生成单个随机字节
* @return 随机字节值(0-255)
*/
@JvmStatic
fun nextByte(): Byte = nextInt(0, 256).toByte()
/**
* 生成无符号随机字节
* @return 无符号随机字节值(0-255)
*/
@JvmStatic
fun nextUByte(): UByte = nextInt(0, 256).toUByte()
fun randomByte(size: Int): ByteArray {
val bytes = ByteArray(size)
secureRandom.nextBytes(bytes)
return bytes
/**
* 生成小于指定最大值的随机字节
* @param max 最大值(包含)
* @return 在Byte.MIN_VALUE到max范围内的随机字节
*/
@JvmStatic
fun nextByte(max: Byte): Byte = nextInt(Byte.MIN_VALUE.toInt(), max.toInt()).toByte()
/**
* 生成指定范围内的随机字节
* @param min 最小值(包含)
* @param max 最大值(包含)
* @return 在min到max范围内的随机字节
*/
@JvmStatic
fun nextByte(min: Byte, max: Byte): Byte = nextInt(min.toInt(), max.toInt() + 1).toByte()
/**
* 生成指定范围内的无符号随机字节
* @param min 最小值(包含)
* @param max 最大值(包含)
* @return 在min到max范围内的无符号随机字节
*/
@JvmStatic
fun nextByte(min: UByte, max: UByte): UByte = nextInt(min.toInt(), max.toInt() + 1).toUByte()
/**
* 生成指定IntRange范围内的随机字节
* @param range 指定的整数范围
* @return 在范围内转换为字节的随机值
*/
@JvmStatic
fun nextByte(range: IntRange): Byte = nextByte(range.first.toByte(), range.last.toByte())
/**
* 生成指定UIntRange范围内的无符号随机字节
* @param range 指定的无符号整数范围
* @return 在范围内转换为无符号字节的随机值
*/
@JvmStatic
fun nextByte(range: UIntRange): UByte = nextByte(range.first.toUByte(), range.last.toUByte())
/**
* 生成随机整数
* @return 随机整数值
*/
@JvmStatic
fun nextInt(): Int = secureRandom.nextInt()
/**
* 生成小于指定最大值的随机整数
* @param max 最大值(不包含)
* @return 0到max-1范围内的随机整数
*/
@JvmStatic
fun nextInt(max: Int): Int = secureRandom.nextInt(max)
/**
* 生成指定范围内的随机整数
* @param min 最小值(包含)
* @param max 最大值(包含)
* @return 在min到max范围内的随机整数
*/
@JvmStatic
fun nextInt(min: Int, max: Int): Int = random.nextInt(min..max)
/**
* 生成指定IntRange范围内的随机整数
* @param range 指定的整数范围
* @return 在范围内的随机整数
*/
@JvmStatic
fun nextInt(range: IntRange): Int = random.nextInt(range)
/**
* 生成随机短整数
* @return 在Short范围内的随机短整数
*/
@JvmStatic
fun nextShort(): Short = secureRandom.nextInt(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()).toShort()
/**
* 生成小于指定最大值的随机短整数
* @param max 最大值(包含)
* @return 在Short.MIN_VALUE到max范围内的随机短整数
*/
@JvmStatic
fun nextShort(max: Short): Short = secureRandom.nextInt(Short.MIN_VALUE.toInt(), max.toInt()).toShort()
/**
* 生成指定范围内的随机短整数
* @param min 最小值(包含)
* @param max 最大值(包含)
* @return 在min到max范围内的随机短整数
*/
@JvmStatic
fun nextShort(min: Short, max: Short): Short = secureRandom.nextInt(min.toInt(), max.toInt()).toShort()
/**
* 生成指定IntRange范围内的随机短整数
* @param range 指定的整数范围
* @return 在范围内转换为短整数的随机值
*/
@JvmStatic
fun nextShort(range: IntRange): Short = secureRandom.nextInt(range.first, range.last).toShort()
/**
* 生成随机长整数
* @return 随机长整数值
*/
@JvmStatic
fun nextLong(): Long = secureRandom.nextLong()
/**
* 生成小于指定最大值的随机长整数
* @param max 最大值(不包含)
* @return 0到max-1范围内的随机长整数
*/
@JvmStatic
fun nextLong(max: Long): Long = secureRandom.nextLong(max)
/**
* 生成指定范围内的随机长整数
* @param min 最小值(包含)
* @param max 最大值(包含)
* @return 在min到max范围内的随机长整数
*/
@JvmStatic
fun nextLong(min: Long, max: Long): Long = random.nextLong(min..max)
/**
* 生成指定LongRange范围内的随机长整数
* @param range 指定的长整数范围
* @return 在范围内的随机长整数
*/
@JvmStatic
fun nextLong(range: LongRange): Long = random.nextLong(range)
/**
* 生成随机布尔值
* @return 随机true或false
*/
@JvmStatic
fun nextBoolean(): Boolean = secureRandom.nextBoolean()
/**
* String类型的扩展函数 - 从字符串中随机选择字符生成指定长度的新字符串
* @param length 要生成的字符串长度
* @return 由原字符串中随机字符组成的新字符串
*/
@JvmStatic
fun String.randomString(length: Int): String = String(CharArray(length) {
this[nextInt(0..<this.length)]
})
/**
* 生成包含数字和字母的随机字符串
* @param length 要生成的字符串长度
* @return 包含0-9、a-z、A-Z字符的随机字符串
*/
@JvmStatic
fun randomString(length: Int): String =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".randomString(length)
/**
* Array类型的扩展函数 - 从数组中随机选择一个元素
* @param T 数组元素类型
* @return 数组中的随机元素
*/
@JvmStatic
fun <T> Array<T>.randomItem(): T = this[nextInt(0..<this.size)]
/**
* ByteArray类型的扩展函数 - 从字节数组中随机选择一个元素
* @return 字节数组中的随机字节
*/
@JvmStatic
fun ByteArray.randomItem(): Byte = this[nextInt(0..<this.size)]
/**
* IntArray类型的扩展函数 - 从整数数组中随机选择一个元素
* @return 整数数组中的随机整数
*/
@JvmStatic
fun IntArray.randomItem(): Int = this[nextInt(0..<this.size)]
/**
* LongArray类型的扩展函数 - 从长整数数组中随机选择一个元素
* @return 长整数数组中的随机长整数
*/
@JvmStatic
fun LongArray.randomItem(): Long = this[nextInt(0..<this.size)]
/**
* FloatArray类型的扩展函数 - 从浮点数数组中随机选择一个元素
* @return 浮点数数组中的随机浮点数
*/
@JvmStatic
fun FloatArray.randomItem(): Float = this[nextInt(0..<this.size)]
/**
* DoubleArray类型的扩展函数 - 从双精度浮点数数组中随机选择一个元素
* @return 双精度浮点数数组中的随机双精度浮点数
*/
@JvmStatic
fun DoubleArray.randomItem(): Double = this[nextInt(0..<this.size)]
/**
* BooleanArray类型的扩展函数 - 从布尔数组中随机选择一个元素
* @return 布尔数组中的随机布尔值
*/
@JvmStatic
fun BooleanArray.randomItem(): Boolean = this[nextInt(0..<this.size)]
/**
* CharArray类型的扩展函数 - 从字符数组中随机选择一个元素
* @return 字符数组中的随机字符
*/
@JvmStatic
fun CharArray.randomItem(): Char = this[nextInt(0..<this.size)]
/**
* ShortArray类型的扩展函数 - 从短整数数组中随机选择一个元素
* @return 短整数数组中的随机短整数
*/
@JvmStatic
fun ShortArray.randomItem(): Short = this[nextInt(0..<this.size)]
/**
* Collection类型的扩展函数 - 从集合中随机选择一个元素
* @param T 集合元素类型
* @return 集合中的随机元素
*/
@JvmStatic
fun <T> Collection<T>.randomItem(): T = this.elementAt(nextInt(0..<this.size))
}
@@ -1,67 +0,0 @@
/*
* Copyright 2026 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile RandomInt.kt
* LastUpdate 2026-01-28 10:46:02
* UpdateUser MingLiPro
*/
@file:JvmName("RandomInt")
package com.mingliqiye.utils.random
import java.security.SecureRandom
val secureRandom: SecureRandom by lazy {
SecureRandom.getInstanceStrong()
}
fun SecureRandom.nextInt(min: Int, max: Int): Int {
return min + nextInt(max - min)
}
/**
* 生成指定范围内的随机整数
* @param min 最小值(包含)
* @param max 最大值(不包含)
* @return 随机整数
*/
fun randomIntNoHave(min: Int, max: Int): Int {
var min = min
var max = max
if (min > max) {
val t = min
min = max
max = t
}
if (min == max) {
return min
}
return secureRandom.nextInt(min, max)
}
/**
* 生成指定范围内的随机整数
* @param min 最小值(包含)
* @param max 最大值(包含)
* @return 随机整数
*/
fun randomInt(min: Int, max: Int): Int {
var max = max
return randomIntNoHave(min, ++max)
}
@@ -1,54 +0,0 @@
/*
* Copyright 2026 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile RandomString.kt
* LastUpdate 2026-02-05 11:12:36
* UpdateUser MingLiPro
*/
@file:JvmName("RandomString")
package com.mingliqiye.utils.random
/**
* 生成指定长度和字符集的随机字符串
*
* @param length 要生成的随机字符串长度
* @param chars 用于生成随机字符串的字符集
* @return 指定长度的随机字符串
*/
fun randomString(length: Int, chars: String): String {
val charsd: Array<String> = chars.split("".toRegex()).filter { !it.isEmpty() }.toTypedArray().println()
val sb = StringBuilder(length)
// 循环生成随机字符并拼接
for (i in 0..<length) {
val index = randomInt(0, charsd.size - 1)
sb.append(charsd[index])
}
return sb.toString()
}
/**
* 生成指定长度的随机字符串,使用默认字符集(数字+大小写字母)
*
* @param length 要生成的随机字符串长度
* @return 指定长度的随机字符串
*/
fun randomString(length: Int): String {
return randomString(
length, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
}