scoop:scoop.platform:0.1.0
src/random/system_random.scoop
Selected production source · src/random/system_random.scoop
1
package scoop.platform.random3
import scoop.platform.io.requireTransfer4
import scoop.platform.memory.NativeByteBuffer5
import scoop.platform.system.*6
import scoop.random.Random8
/**9
* A random source backed by the operating system, independent of deterministic seeded generators.10
*/11
public object SystemRandom: Random {12
/**13
* Returns the requested number of random bits from the operating-system source.14
*/15
public override fun nextBits(bitCount: Int): Int {16
if (bitCount < 0 || bitCount > 32)17
throw IllegalArgumentException(Some("invalid random bit count"))18
if (bitCount == 0)19
return 020
val bytes = nextBytes(4L)21
var result = 0u22
for (byte in bytes)23
result = (result shl 8) or byte.toUByte().toUInt()24
return (result shr (32L - bitCount.toLong())).toInt()25
}27
/**28
* Returns new random bytes or fills the specified mutable-array range, depending on the29
* overload.30
*/31
public override fun nextBytes(size: Long): Array<Byte> {32
if (size < 0L)33
throw IllegalArgumentException(Some("negative byte count"))34
val array = MutableArray<Byte>(size) { 0.toByte() }35
nextBytes(array)36
return array.toArray()37
}39
/**40
* Returns new random bytes or fills the specified mutable-array range, depending on the41
* overload.42
*/43
public override fun nextBytes(44
array: MutableArray<Byte>,45
fromIndex: Long,46
toIndex: Long47
): MutableArray<Byte> {48
if (fromIndex < 0L || toIndex < 0L || fromIndex > array.size || toIndex > array.size)49
throw IndexOutOfBoundsException()50
if (fromIndex > toIndex)51
throw IllegalArgumentException(Some("reversed byte range"))52
if (fromIndex == toIndex)53
return array54
@Unsafe55
{56
array.withDataPointer {57
pointer,58
_ -> @Unsafe59
{60
var offset = fromIndex61
while (offset < toIndex)62
offset += entropy(pointer + offset, toIndex - offset)63
}64
}65
}66
return array67
}69
/**70
* Appends count operating-system random bytes to the buffer's writable region.71
*/72
public fun fill(buffer: NativeByteBuffer, count: Long = buffer.writableBytes): Long {73
requireTransfer(count, buffer.writableBytes)74
var remaining = count75
while (remaining > 0L) {76
val requested = remaining77
@Unsafe78
{79
val written =80
buffer.withWritableBytes {81
pointer,82
_ -> @Unsafe83
{84
return@withWritableBytes entropy(pointer!!, requested)85
}86
}87
remaining -= written88
}89
}90
return count91
}92
}94
@Unsafe95
private fun entropy(pointer: Ptr<Byte>, count: Long): Long {96
val result = nativeEntropy(pointer, count)97
if (result.count < 0L) {98
val domain =99
if (operatingSystem == OperatingSystem.Windows)100
NativeErrorDomain.NtStatus101
else102
NativeErrorDomain.PosixErrno103
val error = NativeError(domain, result.error)104
throw PlatformException("system entropy", error.kind, Some(error))105
}106
return result.count107
}109
@CLayout110
private struct NativeEntropy(val count: Long, val error: Int)112
@Extern(name = "scoop_platform_entropy")113
private fun nativeEntropy(pointer: Ptr<Byte>, count: Long): NativeEntropy