Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to convert a php function in delphi function ?

Posted on 2015-01-22
3
Medium Priority
?
628 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 800 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

972 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