Cryptographically secure random number generator
Generates a cryptographically secure random number. A cryptographically secure random number has to be generated from hardware and its sequence must not be predictable. At least under Windows, which uses preemptive multitasking, the only reliable and always present hardware device which changes its state in that manner, is the inbuilt timestamp counter of Pentium IV+ processors. Instead of the TSC, we also could try to access the Sound Blaster device and read the white noise generated by the microphone/line input, but this could be unreliable, since a plugged and stable sound source could give reproduceable patterns. Since the TSC value could range from 0 to 0FFFFFFFFh in eax, we need to scramble it a bit, to get an uniform distribution of the bits all over. This random number generator returns a scrambled TSC value and does not need to be seeded. Actually, the seed is taken each time from the processor, and the overall speed is noticeably higher than any other known random number generator.
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
unit Random;
{
(C) Copyright Y2K Software 2004
Italy
All Rights Reserved
Copyright Notice:
You may use this code for your own software, both professional and private,
leaving this copyright notice intact.
However, you cannot claim any intellectual property of this code.
For questions please contact:
Y2K Software, Italy
Subject: SecureRandom
http://www.y2ksw.com/vbulletin/sendmessage.php
If you don't like to expose your email address, please subscribe to the forum
at:
http://www.y2ksw.com/vbulletin/register.php
}
interface
function SecureRandom(): Integer; stdcall;
implementation
{
Generates a cryptographically secure random number.
A cryptographically secure random number has to be generated from hardware and
its sequence must not be predictable.
At least under Windows, which uses preemptive multitasking, the only reliable
and always present hardware device which changes its state in that manner, is
the inbuilt timestamp counter of Pentium IV+ processors. Instead of the TSC, we
also could try to access the Sound Blaster device and read the white noise
generated by the microphone/line input, but this could be unreliable, since a
plugged and stable sound source could give reproduceable patterns.
Since the TSC value could range from 0 to 0FFFFFFFFh in eax, we need to scramble
it a bit, to get an uniform distribution of the bits all over.
This random number generator returns a scrambled TSC value and does not need to
be seeded. Actually, the seed is taken each time from the processor, and the
overall speed is noticeably higher than any other known random number generator.
}
function SecureRandom(): Integer; stdcall;
asm
push ebx
rdtsc // read timestamp counter (seed in eax:edx)
mov ebx,127773
xor edx,edx
div ebx
mov ecx,eax
mov eax,16807
mul edx
mov edx,ecx
mov ecx,eax
mov eax,2836
mul edx
sub ecx,eax
mov eax,ecx
pop ebx
end;
end.
Upload