SQL Server
SQL Server
Adding Entropy to a PRNG
See more PRNG Examples
Demonstrates how system entropy is automatically added when the application does not explicitly add entropy prior to generating the 1st random data. Also demonstrates how additional entropy can be added at anytime during the lifetime of the PRNG object.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
SELECT @success = 0
DECLARE @fortuna int
-- The simplest way to use the Chilkat PRNG is to instantiate the object
-- and generate random data. This skips the explicit step of getting system entropy
-- and adding it (via get AddEntropy method). When no entropy is explicitly added,
-- Chilkat will automatically add 32-bytes of system entropy on the first request
-- for random data.
-- Generate some random data.
-- The 1st call to GenRandom implicitly adds 32 bytes of system entropy to seed the PRNG.
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
-- Note: An application can get additional system entropy and add it at any point
-- during the lifetime of the PRNG. For example:
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GetEntropy', @sTmp0 OUT, 16, 'hex'
EXEC sp_OAMethod @fortuna, 'AddEntropy', @success OUT, @sTmp0, 'hex'
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GetEntropy', @sTmp0 OUT, 16, 'hex'
EXEC sp_OAMethod @fortuna, 'AddEntropy', @success OUT, @sTmp0, 'hex'
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GetEntropy', @sTmp0 OUT, 16, 'hex'
EXEC sp_OAMethod @fortuna, 'AddEntropy', @success OUT, @sTmp0, 'hex'
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
EXEC sp_OAMethod @fortuna, 'GenRandom', @sTmp0 OUT, 16, 'hex'
PRINT @sTmp0
END
GO