0

I got a PHP script to generate an encoded URL. I need to translate this script into Apex code to generate the same URL. However, the PHP encode functions seem to work differently from Apex.

The relevant PHP encoding functions are:

  1. hash("MD5", microtime())
  2. $key = sha1(sha1($var1 . $var2) . $var2);
  3. bin2hex($var1)

The code I used to generate them in Apex:

  1. String microTimeTemp = String.valueOf(DateTime.now().getTime()); Blob microtime = EncodingUtil.base64Decode(microTimeTemp); Blob hashMicrotime = Crypto.generateDigest('MD5', microtime); hashTime = EncodingUtil.base64Encode(hashMicrotime);

The closest thing I can think of to PHP microtime() is DateTime.now().getTime(). However, it seems they return different values:

in PHP microtime(): 0.85386700 1457657994

in Apex DateTime.now().getTime(): 1457658064186

Salesforce explained that the DateTime.now().getTime() returns the value of the time count since 1970.

  1. This one is the most complicated one. I actually made meaningful variables. Here I just use 1,2,3,4 for not giving real names:

String var1 = 'The String value 1'; String var2 = 'The String value 2';

String var3 = var1 + var2; Blob blob1 = Blob.valueOf(var3); Blob hash1 = Crypto.generateDigest('SHA1', blob1);

String var4 = EncodingUtil.base64Encode(hash1) + var2; Blob blob2 = Blob.valueOf(var4); Blob hash2 = Crypto.generateDigest('SHA1', blob2); String key = EncodingUtil.base64Encode(hash2);

  1. I think this one might generate the same thing as PHP code? I am not sure:

String hex = EncodingUtil.convertToHex(Blob.valueOf(var1));

When I combined them together, I am pretty sure it was not the same URL generated by the PHP script. Is there a way for Apex to generate the same URL as PHP? Also need to do something encoding as "ASCII"?

2
  • All you do is generating unique random string. Whether it's milliseconds in PHP or APEX, why does it matter? The result will always be a unique random string. Also you can never compare the two and get the same value because X milliseconds have passed between executing one and another code. Mar 11, 2016 at 2:03
  • Well. The milliseconds does not matter. How about the SHA1 method?
    – Yifei Pei
    Mar 11, 2016 at 5:10

1 Answer 1

1

This is how it would look like in APEX:

String md5 = EncodingUtil.base64Encode(Crypto.generateDigest('MD5', Blob.valueOf(String.valueOf(DateTime.now().getTime()))));
system.debug(md5);

String var1 = 'The String value 1'; 
String var2 = 'The String value 2';
String sha1 = EncodingUtil.base64Encode(Crypto.generateDigest('SHA1', Blob.valueOf(EncodingUtil.base64Encode(Crypto.generateDigest('SHA1', Blob.valueOf(var1 + var2))) + var2)));
system.debug(sha1);

String bin2hex = EncodingUtil.base64Encode(Blob.valueOf(var1));
system.debug(bin2hex);
1
  • 1
    Sorry, I haven't got time to test your code. I used another way to sync the data. I will now give this answer the best answer. Later, I will have a test to see if this code works.
    – Yifei Pei
    Mar 17, 2016 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .