Solved

How to convert a php function in delphi function ?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: ForceDirectory plain function 7 29
Zip Folders Using Chilkat Routines 1 36
Dynamic varibles 5 32
mysql date time 14 28
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article discusses how to create an extensible mechanism for linked drop downs.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

786 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