Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

A classy cryptographic PRNG

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • A classy cryptographic PRNG

    What I mean, of course, is a Cryptographic PRNG as a Class.

    The internal function RtlGenRandom is used and since it flies below the CrytoAPI radar we can avoid all the usual initialization provided, of course, that we don't need the CryptoAPI for anything else. More details in an earlier Source Code thread post #3 here. The error checking there is PBWin based and stripped out here. The function coded there is fine but multiple calls will see a library being loaded in and out like a yoyo. We can code to avoid that but a Class comes into its own for multiple calls.

    Usage: RandCrypto.Bytes( n, flag ) where n is the number of bytes required and flag = 0 will generate a binary string of length n. With flag <> 0 a hex string of length 2n will be generated.
    Code:
    ' CRandCrypto.inc
     
    Declare Function RtlGenRandom( RandomBuffer As Byte, ByVal RandomBufferLength As Dword ) As Long
     
    Class CRandCrypto
     
      Instance hLib, hProc As Dword
     
      Class Method Create
        hLib = LoadLibrary( "advapi32.dll")
        hProc = GetProcAddress(hLib, "SystemFunction036")
      End Method
     
      Class Method Destroy
        FreeLibrary hLib
      End Method
     
      Interface IRandCrypto: Inherit IUnknown
     
        Method Bytes( ByVal BinarySize As Dword, ByVal Flag As Long ) As String
          Local i As Long
          Dim BinaryByte( )	As Byte
          Dim HexByte( ) As String * 2
          Local hexString	As String
     
          ReDim BinaryByte( BinarySize - 1 ) As Byte
     
          Call Dword hProc Using RtlGenRandom( BinaryByte(0), ByVal BinarySize )
     
          If Flag = 0 Then
            Method = Peek$( VarPtr( BinaryByte(0) ), BinarySize )
          Else
          	hexString = Space$( 2 * BinarySize )
          	ReDim HexByte( BinarySize - 1 ) As String * 2 At StrPtr( hexString )
          	' Overlay hexString with HexByte()
          	For i = 0 To BinarySize - 1
          		HexByte( i ) = Hex$( BinaryByte(i), 2 )
          	Next
          	Method = hexString
          End If
        End Method
     
      End Interface
     
    End Class
    Example use.
    Code:
    #Compile Exe
    #Optimize speed
    #Dim All
    #Tools Off
    #Register None
    
    #Include "WIN32API.INC"
    
    %NOMMIDS  = 1
    %NOGDI    = 1
    
    #Include "CRandCrypto.inc"
    
    Global RandCrypto As IRandCrypto ' to give access to method anywhere in application
    
    Function PBMain
    
      Local i As Long
    
      Let RandCrypto = Class "CRandCrypto"
      
      ? RandCrypto.Bytes(16,1)
        
      RandCrypto = Nothing
      
      #If %Def(%Pb_Cc32)
        WaitKey$
      #EndIf
    
    End Function
Working...
X