VB icon

Cryptographically secure random number generator

Email
Submitted on: 1/2/2015 12:31:00 AM
By: Y2K Software (from psc cd)  
Level: Advanced
User Rating: By 3 Users
Compatibility: Delphi 6
Views: 3914
 
     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.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
!**************************************
! for :Cryptographically secure random number generator
!**************************************
(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:
Subject: SecureRandom
http://www.y2ksw.com/vbulletin/sendmessage.php
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
!**************************************
! Name: Cryptographically secure random number generator
! Description: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.
! By: Y2K Software (from psc cd)
!
! Returns:Returns a scrambled TSC unsigned long integer value.
!
! Assumes:Function ready to copy and paste.
!**************************************

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.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.