home *** CD-ROM | disk | FTP | other *** search
Visual Basic class definition | 2012-06-12 | 17.6 KB | 354 lines |
- VERSION 1.0 CLASS
- BEGIN
- MultiUse = -1 'True
- Persistable = 0 'NotPersistable
- DataBindingBehavior = 0 'vbNone
- DataSourceBehavior = 0 'vbNone
- MTSTransactionMode = 0 'NotAnMTSObject
- END
- Attribute VB_Name = "cPrng"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = False
- Attribute VB_Exposed = False
- Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
- Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
- ' ***************************************************************************
- ' Module: clsRandom - a cryptographic random number generator
- ' (Pseudo Random Number Generator using Microsoft's CryptoAPI)
- '
- ' Description: CryptGenRandom gets its randomness, also known as
- ' entropy, from many sources in Windows, including
- ' the following:
- '
- ' - The current process ID (GetCurrentProcessID).
- ' - The current thread ID (GetCurrentThreadID).
- ' - The ticks since boot (GetTickCount).
- ' - The current time (GetLocalTime).
- ' - Various high-precision performance counters
- ' (QueryPerformanceCounter).
- ' - A Message Digest 4 (MD4) hash of the userÆs environment
- ' block, which includes username, computer name, and
- ' search path. MD4 is a hashing algorithm that creates
- ' a 128-bit message digest (16 bytes) from input data
- ' to verify data integrity.
- ' - High-precision internal CPU counters, such as RDTSC,
- ' RDMSR, RDPMC.
- ' - Low-level system information, such as idle time, kernel
- ' time, interrupt times, commit limit, page read count,
- ' cache read count, nonpaged pool allocations, alignment
- ' fixup count, operating system lookaside information.
- ' - [Optional] User defined data as extra seed data. I created
- ' a routine named CreateExtraSeed() to generate a unique
- ' hex data string as my optional data. A good example
- ' is used in BuildRndData() routine.
- '
- ' Such information is added to a buffer, which is hashed using
- ' MD4 and used as the key to modify the user-provided buffer
- ' using RC4. (Refer to the CryptGenRandom() documentation in
- ' the Platform SDK) The result is a cryptographic random
- ' number value.
- '
- ' References: Randomize Statement Doesn't Re-initialize Rnd Function
- ' "To re-initialize the random-number generator, use the Rnd
- ' function with a value of -1 and then use the Randomize
- ' statement with the value you want to use as the seed value
- ' for the Rnd function."
- ' http://support.microsoft.com/default.aspx?scid=kb;en-us;120587
- '
- ' VBA's Pseudo Random Number Generator
- ' http://www.noesis.net.au/prng.php
- '
- ' Visual Basic Language Reference Rnd Function
- ' http://msdn2.microsoft.com/en-us/library/f7s023d2(VS.71).aspx
- '
- ' Mark Hutchinson article:
- ' An Examination of Visual Basic's Random Number Generation
- ' http://www.15seconds.com/issue/051110.htm
- '
- ' INFO: How Visual Basic Generates Pseudo-Random Numbers for
- ' the RND Function
- ' http://support.microsoft.com/kb/231847/en-us
- '
- ' RND and RANDOMIZE Alternatives for Generating Random Numbers
- ' http://support.microsoft.com/kb/28150/EN-US/
- '
- ' ===========================================================================
- ' DATE NAME / eMAIL
- ' DESCRIPTION
- ' ----------- --------------------------------------------------------------
- ' 15-FEB-2004 Kenneth Ives kenaso@tx.rr.com
- ' 10-Sep-2008 Kenneth Ives kenaso@tx.rr.com
- ' Updated CreateExtraSeed() in determining the toggle method.
- ' 25-Sep-2008 Kenneth Ives kenaso@tx.rr.com
- ' Rewrote GetRndValue() routine. Thanks to Alfred Hellmⁿller
- ' for seeing the shortcomings of using the Visual Basic RND()
- ' function.
- ' 03-Oct-2008 Kenneth Ives kenaso@tx.rr.com
- ' Removed some obsolete data.
- ' 12-Oct-2008 Kenneth Ives kenaso@tx.rr.com
- ' Added additional range testing in GetRndValue() routine.
- ' 01-Nov-2008 Kenneth Ives kenaso@tx.rr.com
- ' - Modified logic for obtaining a valid value in GetRndValue()
- ' routine.
- ' - Added static variable in CreateExtraSeed() routine to hold
- ' a carryover data string.
- ' - Fixed bug in BuildWithinRange() routine in loading a hex
- ' array and testing for valid return formats.
- ' 17-Mar-2009 Kenneth Ives kenaso@tx.rr.com
- ' - Added EmptyCollection() routine to properly empty a
- ' collection object.
- ' - Updated CombSort() routine.
- ' - Updated documentation in ReshuffleData() and NonRepeatingNbrs()
- ' routines.
- ' 14-Apr-2009 Kenneth Ives kenaso@tx.rr.com
- ' Updated CreateExtraSeed() and RndSeed() routines.
- ' 02-Nov-2009 Kenneth Ives kenaso@tx.rr.com
- ' Corrected a potential overflow in alternate calculation in
- ' CreateExtraSeed() routine.
- ' 10-Feb-2010 Kenneth Ives kenaso@tx.rr.com
- ' Thanks to Alfred Hellmⁿller for bringing to my attention the
- ' need to update GetProviderHandle() routine.
- ' - Rewrote GetProviderHandle() routine to test for availability
- ' of Advanced Encryption Standard (AES) hash functionality.
- ' - Added HashSelection() routine.
- ' - Updated CreateHash() routine to access SHA2 hash family.
- ' 03-Mar-2010 Kenneth Ives kenaso@tx.rr.com
- ' - Updated CreateHash() and HashSelection() routines.
- ' - Updated documentation in this module and associated text
- ' files.
- ' 08-May-2010 Kenneth Ives kenaso@tx.rr.com
- ' - Rewrote ReshuffleData() and CreateExtraSeed() routines.
- ' - Updated CombSort(), RndSeed() and RemoveDupes() routines.
- ' - Added ReverseArrayData() routine. Called by CombSort().
- ' - Removed some obsolete code.
- ' 24-Jul-2010 Kenneth Ives kenaso@tx.rr.com
- ' - Modifed and documented ConvertDataToHex(), HashSelection(),
- ' CreateHash(), BuildRndData() routines
- ' - Added boolean property AES_Ready()
- ' 10-Dec-2010 Kenneth Ives kenaso@tx.rr.com
- ' - Updated selection of data to seed VB random number generator
- ' in CreateExtraSeed() routine.
- ' - Updated evaluation of input data in NonRepeatingNbrs()
- ' routine.
- ' 02-Mar-2011 Kenneth Ives kenaso@tx.rr.com
- ' - Added boolean flag parameter to RndSeed() routine.
- ' - Updated CreateExtraSeed() routine. Added reference
- ' to API GetTickCount()
- ' 18-May-2011 Kenneth Ives kenaso@tx.rr.com
- ' Rewrote RndSeed() routine.
- ' 25-Aug-2011 Kenneth Ives kenaso@tx.rr.com
- ' - Added property CompareMethod() to determine type of data
- ' comparison.
- ' - Updated RemoveDupes() routine to use CompareMethod()
- ' property.
- ' 20-Oct-2011 Kenneth Ives kenaso@tx.rr.com
- ' Increased maximum number of mixing iterations in ReshuffleData()
- ' routine.
- ' 30-Dec-2011 Kenneth Ives kenaso@tx.rr.com
- ' - Updated CreateExtraSeed() routine.
- ' - Bug fix in GetRndValue() routine.
- ' - Added optional parameter, blnCreateExtraSeed, to BuildRndData()
- ' and BuildWithinRange() routines.
- ' 26-Mar-2012 Kenneth Ives kenaso@tx.rr.com
- ' Deleted RemoveTrailingNulls() routine from this module.
- ' 10-May-2012 Kenneth Ives kenaso@tx.rr.com
- ' Updated ReshuffleData() and CreateExtraSeed() routines.
- ' ***************************************************************************
- Option Explicit
-
- ' ***************************************************************************
- ' Constants
- ' ***************************************************************************
- Private Const MODULE_NAME As String = "clsRandom"
- Private Const KB_4 As Long = &H1000& ' 4096
- Private Const KB_64 As Long = &H10000 ' 65536
- Private Const MAX_INT As Long = &H7FFF& ' 32767
- Private Const MAX_LONG As Long = &H7FFFFFFF ' 2147483647
- Private Const MIN_LONG As Long = &H80000000 ' -2147483648
- Private Const GB_4 As Double = (2# ^ 32) ' 4294967296 (== 4.2 Gig)
- Private Const MAX_DWORD As Double = (2# ^ 32) - 1 ' 4294967295 (unsigned long int)
- Private Const DBL_LOW As Double = 0.000000000001
- Private Const DBL_HIGH As Double = (1.999999999998 / MAX_DWORD) ' 0.000000000465661287415694
-
- ' Microsoft Hash constants
- Private Const HP_HASHVAL As Long = 2
- Private Const ALG_CLASS_HASH As Long = &H8000& ' Used by all hashes (32768)
- Private Const ALG_TYPE_ANY As Long = 0 ' Used by all hashes
- Private Const ALG_SID_MD2 As Long = 1 ' 16 byte hashed return length
- Private Const ALG_SID_MD4 As Long = 2 ' 16 byte hashed return length
- Private Const ALG_SID_MD5 As Long = 3 ' 16 byte hashed return length
- Private Const ALG_SID_SHA1 As Long = 4 ' 20 byte hashed return length
- Private Const ALG_SID_SHA_256 As Long = 12 ' 32 byte hashed return length
- Private Const ALG_SID_SHA_384 As Long = 13 ' 48 byte hashed return length
- Private Const ALG_SID_SHA_512 As Long = 14 ' 64 byte hashed return length
- Private Const CALG_MD2 As Long = &H8001& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2 (32769)
- Private Const CALG_MD4 As Long = &H8002& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4 (32770)
- Private Const CALG_MD5 As Long = &H8003& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5 (32771)
- Private Const CALG_SHA1 As Long = &H8004& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1 (32772)
- Private Const CALG_SHA_256 As Long = &H800C& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_256 (32780)
- Private Const CALG_SHA_384 As Long = &H800D& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_384 (32781)
- Private Const CALG_SHA_512 As Long = &H800E& ' ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_512 (32782)
- ' Microsoft Provider type constants
- Private Const PROV_RSA_FULL As Long = 1 ' Provider type ID
- Private Const PROV_RSA_AES As Long = 24 ' AES Provider type ID
- Private Const CRYPT_NEWKEYSET As Long = &H8 ' For creating a generic provider handle
- Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000 ' -268435456
- ' Verify provider names
- ' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\Defaults\Provider\
- Private Const MS_DEFAULT_PROV As String = "Microsoft Base Cryptographic Provider v1.0"
- Private Const MS_ENHANCED_PROV As String = "Microsoft Enhanced Cryptographic Provider v1.0"
- Private Const MS_ENH_RSA_AES_PROV As String = "Microsoft Enhanced RSA and AES Cryptographic Provider"
- Private Const MS_ENH_RSA_AES_PROV_XP As String = "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
-
- ' ***************************************************************************
- ' Type Structures
- ' ***************************************************************************
- Private Type CARRYOVER_DATA
- Value1 As Long
- Value2 As Long
- End Type
-
- ' ***************************************************************************
- ' Enumerations
- ' ***************************************************************************
- Public Enum enumPRNG_ReturnFormat
- ePRNG_ASCII ' 0
- ePRNG_HEX ' 1
- ePRNG_HEX_ARRAY ' 2
- ePRNG_BYTE_ARRAY ' 3
- ePRNG_LONG_ARRAY ' 4
- ePRNG_DBL_ARRAY ' 5
- End Enum
-
- Public Enum enumPRNG_HashAlgorithm
- ePRNG_MD2 ' 0
- ePRNG_MD4 ' 1
- ePRNG_MD5 ' 2
- ePRNG_SHA1 ' 3
- ePRNG_SHA256 ' 4
- ePRNG_SHA384 ' 5
- ePRNG_SHA512 ' 6
- End Enum
-
- Public Enum enumPRNG_Compare
- ePRNG_CaseSensitive ' 0 - Exact byte match
- ePRNG_IgnoreCase ' 1 - Uppercase/Lowercase considered same
- End Enum
-
- ' ***************************************************************************
- ' API Declares
- ' ***************************************************************************
- ' This is a rough translation of the GetTickCount API. The
- ' tick count of a PC is only valid for the first 49.7 days
- ' since the last reboot. When you capture the tick count,
- ' you are capturing the total number of milliseconds elapsed
- ' since the last reboot. The elapsed time is stored as a
- ' DWORD value. Therefore, the time will wrap around to zero
- ' if the system is run continuously for 49.7 days.
- Private Declare Function GetTickCount Lib "kernel32" () As Long
-
- ' The CryptCreateHash function initiates the hashing of a stream of
- ' data. It creates and returns to the calling application a handle
- ' to a CSP hash object. This handle is used in subsequent calls to
- ' CryptHashData and CryptHashSessionKey to hash session keys and
- ' other streams of data.
- Private Declare Function CryptCreateHash Lib "advapi32.dll" _
- (ByVal hProv As Long, ByVal algid As Long, _
- ByVal hKey As Long, ByVal dwFlags As Long, _
- ByRef phHash As Long) As Long
-
- ' The CryptHashData function adds data to a specified hash object.
- ' This function and CryptHashSessionKey can be called multiple
- ' times to compute the hash of long or discontinuous data streams.
- Private Declare Function CryptHashData Lib "advapi32.dll" _
- (ByVal hhash As Long, ByVal pbData As String, _
- ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
-
- ' The CryptGetHashParam function retrieves data that governs the
- ' operations of a hash object. The actual hash value can be
- ' retrieved by using this function.
- Private Declare Function CryptGetHashParam Lib "advapi32.dll" _
- (ByVal hhash As Long, ByVal dwParam As Long, _
- ByVal pbData As String, pdwDataLen As Long, _
- ByVal dwFlags As Long) As Long
-
- 'The CryptDestroyHash function destroys the hash object referenced
- ' by the hHash parameter. After a hash object has been destroyed,
- ' it can no longer be used. The destrt governs Long) A31847/en-us
- '
- ' RND and RANDOMIZECo The a ha h object ulLes
- an be called multiple
- t0(.e A,
- '
- 'aong
- 847/en-us
- '
- Pc_I3i r, _
- pBP object ulLes
- an be 5,
- '
- 'ao *******
- '
- 'aong
- 847/en-us
- '
- Pc_I3i r, _
- pBP object ulLes
- an be 5,
- '
- 'ao
- 11,g
-
- ' The CryT ng th4 ' 3
- puuuuuuuu
- 11,g
-
- ' The CryT ng th4 ' 3
- puuuuuuuu
- 11,g
-
- 847s Long
- aongEn,g
- 4"
- puuuuuuuu
- 11,g
- pC1,g
- pC1,g
- p http://s ' 0
- = &H8000&
- ' since the last reL since the last reL since the last reL ulLes
- ansf-- htdo-BisGE ID
-
- ' rg' si
- iple
- t0(.e A,
- '
- s 7/eA================/A opm fNcreL since tollowing:
- '
- ' -aso@tx. ===============.governs Long) A31847/en-us
- '
- nreL gbh.
- ' s
- ' i s
- rg' si
- iple
- t0(.e A,
- '
- s 7/eA================/A =G=========/A opm fNcreL uguaC1-Flagso object referenreRpe7HashParapoAa
- pryptG rg' si
- iple
- t0(.e A,
- '
- s 7/eA=========== objectM+m5
- ' opm fNcre ppm coarapoAa opm f
- 1re ppm coaram
- '
- Pc_I3i lebmlue can be
- + 7 ' by the hHash paramereaual Ba7HashP the hHash paramereaual Ba7HashP the hHash CIfong, ByVal dwParam As Long, _
- ByVal pbDAa
- es Xctiple
- oe ' dat ePRNG_h ByVr-aco ppm coaram
- '
- iTunction uÆ As LoaAa t
- iTunctioP hash o ulLesEe aongEn ro5rt
- o7 7/eA=======n8mmhe CryT ngCesEe Ee aongEn ro5rt
- o7 7/eA=======n8mmhe CryT ngCesEe EyecTbp rg'mhe.e A,
- (v===n8mmh7/eA===nrnwsh paramereaual Ba7HashP the shP,'
- Pg,'ao *******
- '
- 'tb/en-us
- es Xctn/en(aHPn-us
- ns - [Othe hHmNon(aHPn6dGm0Nh
- Ivecs
- rg'.tg(aHPn-us
- ns - [OthepuGm0NNcre ppm coarapoAa
- ' tick count of a PC is only valid for the first 49.7 days
- ' since the last reboM7ne
- t0(.e cy8continuo'
- s 7/e====c,g
- 4"
- puuuuuuuu.e A,\0 -p0l.sms.scre ====c,g
- =/A opm fNcreL since tollowing:
- '
- ' -aso@tx. ===============.governs LonwNXctn/en(aHPn-us
- ns =/A RAYh1.ntation in this mC '( sincteHash(),pcop ' by 44p0l.sms.screa be 5,20r-nyXRqas eououx,CA 1yVrcYOg' si
- iple
- t0(. Thid as a
- eVrcYOg' si
- iple
- t0(. Thintation in this mC '( in thif3r2cl.ys
- Cyaeani' TiNG_MD5odi://www.1i' TiNG_MD5odturing a== 4.2o re' to hash sessionosi
- iple
- t0(. Thid as a
- eVrcYOg' si
- iple
- t0(. v=/A opm fNcreL hs< specified2O,dCombSort(se /nxKennetyT ngCes|HPI_is ppm coaram
- '
- Pc_I3i lebmlue can be
- + specified2O,d opm0ombSonu.s -p0l.sms.scre ====c,p3pSeL hsDDsu dara
- eVrces|Hare ===arA2sIes2P