?
Solved

How to convert a php function in delphi function ?

Posted on 2015-01-22
3
Medium Priority
?
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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