Sample code for 30+ languages & platforms
DataFlex

Example: Crypt2.GenRandomBytesENC method

Shows how to use the GenRandomBytesENC method to generate a specified number of random bytes and return them as a binary encoded string, such as in base64 or hex format.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoCrypt
    String s

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End

    // Example of a some of the binary encodings supported by Chilkat.

    // Internally uses the Fortuna cryptographically secure random number generation algorithm
    // auto-seeded from a secure source of entropy that depends on the OS.

    Set ComEncodingMode Of hoCrypt To "hex"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Uppercase Hex: " s

    Set ComEncodingMode Of hoCrypt To "hex_lower"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Lowercase Hex: " s

    Set ComEncodingMode Of hoCrypt To "base64"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Base64: " s

    Set ComEncodingMode Of hoCrypt To "base64url"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Base64Url: " s

    Set ComEncodingMode Of hoCrypt To "base58"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Base58: " s

    Set ComEncodingMode Of hoCrypt To "base32"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Base32: " s

    Set ComEncodingMode Of hoCrypt To "decList"
    Get ComGenRandomBytesENC Of hoCrypt 8 To s
    Showln "Decimal List: " s

    // Sample Output:

    // Uppercase Hex: 1BA0E87915C3B429
    // Lowercase Hex: 1e1638a1c7996c58
    // Base64: 1lV0I/su5lg=
    // Base64Url: Zq7DU0nukLk
    // Base58: foUVWUjAT6e
    // Base32: LHV5RV4HRCLW2===
    // Decimal List: 168,55,214,82,93,154,191,164


End_Procedure