Solved

How to convert a php function in delphi function ?

Posted on 2015-01-22
3
535 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

756 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