Solved

How to convert a php function in delphi function ?

Posted on 2015-01-22
3
474 Views
Last Modified: 2015-01-25
Hi Experts,

I have this function in php:
$bytes = openssl_random_pseudo_bytes(20)

I need to convert this function to Delphi Xe6.
Some idea ?

Thanks
Alexandre
0
Comment
Question by:alpires
3 Comments
 
LVL 13

Assisted Solution

by:akb
akb earned 200 total points
ID: 40565126
The following may do. Or you can modify it to include all possible characters.

function RandomString(SLen: Integer): string;
var
  str: string;
begin
  Randomize;
  //string with all possible chars
  str    := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+`{}\|[];:,./<>?1234567890';
  Result := '';
  repeat
    Result := Result + str[Random(Length(str)) + 1];
  until (Length(Result) = SLen)
end;
0
 

Author Comment

by:alpires
ID: 40566882
Ok, but in php I have this code:

<?php
        $bytes = openssl_random_pseudo_bytes(20);
      echo(strtolower(urlencode($bytes)))
?>

The output is something like this: %2a%be%c0i%8f%a9%8e%99j%7fy%40f%b9%97%d2%f2%adb%9c

Can you help me do this ?
0
 
LVL 24

Accepted Solution

by:
jimyX earned 300 total points
ID: 40568912
The output is something like this: %2a%be%c0i%8f%a9%8e%99j%7fy%40f%b9%97%d2%f2%adb%9c
That's because you used URL encode: echo(strtolower(urlencode($bytes)))

The output you got first was, something like: "*¾Ài©Ž™j y@f¹—Òò­bœ" which has length of 20 as stipulated.

Your next question will be, does delphi has urlencode function?
Yes, see this link for variety of options.

The openssl_random_pseudo_bytes has encryption ability by passing a second boolean parameter that would provide encrypted bytes, if needed.

You can use: Char(random(Integer)):
function random_pseudo(Len: Integer): string;
begin
  Randomize;
  Result:= '';
  while Length(Result) < Len do begin
    Result := Result + Char(Random(255) + 1);
  end;
end;

Open in new window



@akb:
What happens when calling "RandomString(0);"?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now